Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8204336

Platform.exit() throws ISE when a nested event loop is active

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      java.lang.IllegalStateException is thrown if Platform.exit() is called when there is a "dialog" window open (i.e. a Stage opened via Stage.showAndWait()).

      In typical use-cases, there isn't a reason why an Application would call Platform.exit() when there is an open dialog window. However, in my use-case, the Application has a main Window, which stays open until the Application is closed. The main window is capable of spawning more child windows. Some of the child windows are capable of opening dialog window with window-level modality. Lastly, the main window allows the user to end the whole Application with a single click, regardless of the state of the child windows.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Use the sample code provided. The sample code does not show my use-case, but it does show how the exception can be triggered.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Application would end normally/gracefully.
      ACTUAL -
      An exception is being thrown. This is the stacktrace:

      Exception in thread "JavaFX Application Thread" java.lang.IllegalStateException: This operation is permitted on the event thread only; currentThread = JavaFX Application Thread
          at com.sun.glass.ui.Application.checkEventThread(Application.java:443)
          at com.sun.glass.ui.Application.isNestedLoopRunning(Application.java:544)
          at com.sun.javafx.tk.quantum.QuantumToolkit.isNestedLoopRunning(QuantumToolkit.java:1139)
          at com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(QuantumToolkit.java:585)
          at javafx.stage.Stage.showAndWait(Stage.java:474)
          at test.lambda$0(test.java:43)
          at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
          at java.security.AccessController.doPrivileged(Native Method)
          at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
          at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
          at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
          at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
          at java.lang.Thread.run(Thread.java:745)

      ---------- BEGIN SOURCE ----------
      public class test extends Application {
          @Override
          public void start(final Stage primaryStage) throws Exception {
              final VBox root = new VBox();
              final Scene sc = new Scene(root);
              primaryStage.setScene(sc);
              primaryStage.show();

              // Platform.runLater is necessary because this start() must not be blocked (i.e. start() must finish executing)
              Platform.runLater(() -> {
                  final Button button = new Button("Exit");
                  button.setOnAction(e -> Platform.exit());

                  Stage st = new Stage();
                  st.setScene(new Scene(button));

                  st.initOwner(primaryStage);
                  st.initModality(Modality.WINDOW_MODAL); // Just need window-level modality

                  st.showAndWait(); // Shows the "dialog" window
              });
          }

          public static void main(final String[] args) {
              Application.launch(args);
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        1. NestedEventLoopExitTest.java
          2 kB
        2. platform-exit-tests.patch
          13 kB
        3. SimpleExitTest.java
          0.1 kB
        4. Test.java
          1 kB

            pbansal Pankaj Bansal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: