openjfx / javafx-maven-plugin

Maven plugin to run JavaFX 11+ applications
Apache License 2.0
371 stars 59 forks source link

`async` does not guarantee that JavaFX application is launched #167

Open Marcono1234 opened 1 year ago

Marcono1234 commented 1 year ago

Version

0.0.8

Description

Apache Commons Exec launches the process in a separate thread (slightly related to EXEC-121), so what could happen is that before the JavaFX process is launched Maven exits.

In reality it is probably rather unlikely that this will happen because that executor thread launches the process as first action and on the other hand it takes some time until the JavaFX plugin and Maven finish and the JVM exits. But it is not impossible that this can occur. It can for example be demonstrated by running with mvnDebug clean javafx:run "-Djavafx.async=true" "-Djavafx.asyncDestroyOnShutdown=false" and setting a breakpoint in org.apache.commons.exec.DefaultExecutor.executeInternal to pause execution there.

Potential solution

One potential solution might be to add a CountDownLatch or similar with initial value of 1. Then supply a custom ExecuteStreamHandler to exec whose start() method calls countDown(), and additionally in onProcessFailed also call countDown() (to avoid a dead lock in case of a failure). And then have org.openjfx.JavaFXBaseMojo.executeCommandLine call countDownLatch.await() after calling execute. This should then make sure that the process was launched before the JavaFX plugin (and eventually the JVM) exits. (Have not tested this though)

As side note: Maybe onProcessFailed in org.openjfx.JavaFXBaseMojo.executeCommandLine should also call psh.stop() to be safe?