Skip to content

Commit

Permalink
8263322: Calling Application.launch on FX thread should throw Illegal…
Browse files Browse the repository at this point in the history
…StateException, but causes deadlock

Reviewed-by: kcr, arapte
  • Loading branch information
FlorianKirmaier authored and kevinrushforth committed Apr 7, 2021
1 parent 5898858 commit 9796a83
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 0 deletions.
Expand Up @@ -171,6 +171,9 @@ public static void launchApplication(final Class<? extends Application> appClass
final Class<? extends Preloader> preloaderClass,
final String[] args) {

if (com.sun.glass.ui.Application.isEventThread()) {
throw new IllegalStateException("Application launch must not be called on the JavaFX Application Thread");
}
if (launchCalled.getAndSet(true)) {
throw new IllegalStateException("Application launch must not be called more than once");
}
Expand Down
Expand Up @@ -221,6 +221,7 @@ public abstract class Application {
* {@link #getParameters()} method.
*
* @throws IllegalStateException if this method is called more than once.
* @throws IllegalStateException if this method is called from the JavaFX application thread.
* @throws IllegalArgumentException if <code>appClass</code> is not a
* subclass of <code>Application</code>.
* @throws RuntimeException if there is an error launching the
Expand Down Expand Up @@ -264,6 +265,7 @@ public static void launch(Class<? extends Application> appClass, String... args)
* {@link #getParameters()} method.
*
* @throws IllegalStateException if this method is called more than once.
* @throws IllegalStateException if this method is called from the JavaFX application thread.
* @throws RuntimeException if there is an error launching the
* JavaFX runtime, or if the application class cannot be constructed
* (e.g., if the class is not public or is not in an exported package), or
Expand Down
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package test.com.sun.javafx.application;

import javafx.application.Application;
import javafx.stage.Stage;
import junit.framework.Assert;
import test.util.Util;

public class InitializeJavaFXBase {

public static class TestApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
}
}

public void doTestInitializeThenLaunchInFX() throws Exception {
Util.runAndWait(() ->{
try {
Application.launch(TestApp.class);
Assert.fail("Error: No Exception was thrown - expected IllegalStateException");
} catch (IllegalStateException e) {
// This Exception is what we expect!
}
});
}

public void doTestInitializeThenSecondLaunch() throws Exception {
try {
Application.launch(TestApp.class);
Assert.fail("Error: No Exception was thrown - expected IllegalStateException");
} catch (IllegalStateException e) {
// This Exception is what we expect!
}
}
}
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package test.com.sun.javafx.application;

import org.junit.BeforeClass;
import org.junit.Test;

public class InitializeJavaFXLaunch1Test extends InitializeJavaFXLaunchBase {

@BeforeClass
public static void initialize() throws Exception {
InitializeJavaFXLaunchBase.initializeApplicationLaunch();
}

@Test (timeout = 15000)
public void testLaunchThenLaunchInFX() throws Exception {
doTestInitializeThenLaunchInFX();
}
}
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package test.com.sun.javafx.application;

import org.junit.BeforeClass;
import org.junit.Test;

public class InitializeJavaFXLaunch2Test extends InitializeJavaFXLaunchBase {

@BeforeClass
public static void initialize() throws Exception {
InitializeJavaFXLaunchBase.initializeApplicationLaunch();
}

@Test (timeout = 15000)
public void testLaunchThenLaunch() throws Exception {
doTestInitializeThenSecondLaunch();
}
}
@@ -0,0 +1,25 @@
package test.com.sun.javafx.application;

import javafx.application.Application;
import javafx.stage.Stage;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertTrue;

public class InitializeJavaFXLaunchBase extends InitializeJavaFXBase {
public static final CountDownLatch appLatch = new CountDownLatch(1);

public static class InitializeApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
appLatch.countDown();
}
}

public static void initializeApplicationLaunch() throws Exception {
new Thread(() -> {
Application.launch(InitializeApp.class);
}).start();
assertTrue(appLatch.await(5, TimeUnit.SECONDS));
}
}
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package test.com.sun.javafx.application;

import org.junit.BeforeClass;
import org.junit.Test;

public class InitializeJavaFXStartup1Test extends InitializeJavaFXStartupBase {

@BeforeClass
public static void initialize() throws Exception {
InitializeJavaFXStartupBase.initializeStartup();
}

@Test (timeout = 15000)
public void testStartupThenLaunchInFX() throws Exception {
doTestInitializeThenLaunchInFX();
}
}
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package test.com.sun.javafx.application;

import org.junit.BeforeClass;
import org.junit.Test;

public class InitializeJavaFXStartup2Test extends InitializeJavaFXStartupBase {

@BeforeClass
public static void initialize() throws Exception {
InitializeJavaFXStartupBase.initializeStartup();
}

@Test (timeout = 15000)
public void testStartupThenLaunch() throws Exception {

// The first call to Application.launch should work
InitializeJavaFXLaunchBase.initializeApplicationLaunch();

doTestInitializeThenSecondLaunch();
}
}
@@ -0,0 +1,18 @@
package test.com.sun.javafx.application;

import javafx.application.Platform;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertTrue;

public class InitializeJavaFXStartupBase extends InitializeJavaFXBase {

public static void initializeStartup() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
Platform.startup(() -> {
latch.countDown();
});
assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}

1 comment on commit 9796a83

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.