[External] : Re: Is it possible to disable JavaFX shutdown hook?

Kevin Rushforth kevin.rushforth at oracle.com
Sat Jul 27 13:49:14 UTC 2024


While it might be possible to make the shutdown experience a little more 
graceful, your expectation of being able to use the GUI after the start 
of the shutdown sequence is unlikely to be met. Quoting from the 
shutdown docs [1] :

"Shutdown hooks run at a delicate time in the life cycle of a virtual 
machine ... Attempts to use other thread-based services such as the AWT 
event-dispatch thread, for example, may lead to deadlocks.

Shutdown hooks should also finish their work quickly. When a program 
invokes exit, the expectation is that the virtual machine will promptly 
shut down and exit. When the virtual machine is terminated due to user 
logoff or system shutdown the underlying operating system may only allow 
a limited amount of time in which to shut down and exit. *It is 
therefore inadvisable to attempt any user interaction or to perform a 
long-running computation in a shutdown hook.*"

Note, particularly, that last sentence. Asking JavaFX to ignore the fact 
that there is a shutdown in progress doesn't change that. Having said 
that, I can take another look at this (not right away, though) and see 
what else we can do.

-- Kevin

[1] 
https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)


On 7/26/2024 10:11 AM, PavelTurk wrote:
> Hello, Kevin
>
> Yes, I filed that enhancement request.
>
> When user presses CTRL+C then SIGINT signal is sent to the process. 
> SIGINT is a signal used to request a graceful termination of a 
> process, typically initiated by the user(more details are here 
> https://urldefense.com/v3/__https://stackoverflow.com/a/4047975__;!!ACWV5N9M2RV99hQ!Kubf8U462HbtwSv0HaNgAZNyYpFS94_WfV0T8TAjTcNx56xb2lUr9jPR5ezZHSUjZ1C-tStV9j5KxWUduzx7XjC0BR1D$ 
> ).
>
> Now, we are talking about GUI application (if we didn't develop a GUI 
> application, we wouldn't need JavaFX, would we?). So, our GUI 
> application gets SIGINT or SIGTERM (I didn't test with SIGTERM, but I 
> think result is the same) and after that immediately stops working 
> because of the damned shutdown hook. For example, if our application 
> is a simple text editor, our application can't let user save file. 
> Why? Because GUI platform doesn't work anymore.
>
> That's why I opened that issue. I saw you suggested working with 
> exceptions. But that issue is not about exceptions, but about letting 
> application developers control when to shutdown platform.
>
> Best regards, Pavel
>
>
> On 7/26/24 16:54, Kevin Rushforth wrote:
>> I am not sure we will want to disabling the shutdown hook, since that 
>> is likely to cause problems (they were added to fix bugs that 
>> resulted from applications calling System.exit()).
>>
>> In the bug report for JDK-8320923, you mentioned that Swing doesn't 
>> have this problem, but that's because Swing closes all of its windows 
>> before effecting the shutdown (that's something JavaFX could do as 
>> well).
>>
>> Related to this, I see that someone (perhaps it was you?) just filed 
>> an enhancement request to consider optionally disabling the shutdown 
>> hook:
>>
>> https://bugs.openjdk.org/browse/JDK-8337247
>>
>> I haven't looked at it closely, but my sense is that disabling the 
>> shutdown hooks in a way that tries to keep the JavaFX runtime active 
>> is unlikely to be something we want to do.  I added the following 
>> comment to the above JBS issue with some initial thoughts:
>>
>> --------------------------------------------------
>> “I can take a look, but I do not think we will implement this 
>> Enhancement request -- at least not as described.
>>
>> Instead, we might consider whether this can be done using existing 
>> mechanisms, such as overriding Application::exit. If it turns out 
>> there isn't a suitable existing mechanism, we might consider other 
>> solutions. A couple possibilities:
>>
>> 1. Provide a Platform setting that will cause Platform.runLater to 
>> throw an exception if called after shutdown (this might be require 
>> adding a few try/catch blocks in our own cleanup code)
>> 2. Rather than a setting as described above, create a new overload of 
>> Platform.runLater that throws an exception if the Platform is not 
>> able to queue the request.
>>
>> There might be other solutions.”
>> -------------------------------------------------------
>>
>> -- Kevin
>>
>>
>> On 7/25/2024 8:43 AM, PavelTurk wrote:
>>> Hello all.
>>>
>>> JavaFX adds its own shutdown hook. That gives many problems when it 
>>> is necessary to work with application,
>>> when system is shutting down, for example, if user presses CTRL+C. 
>>> The first problem I described here -
>>> https://bugs.openjdk.org/browse/
>>>
>>> Another problem is that after pressing CTRL+C JavaFX seems not to 
>>> respond anymore. For example I've
>>> observed that if after that we try to do Platform.runLater(() -> 
>>> myCode is here), then myCode will never execute.
>>> So, it is necessary to check in system, how it is shutting down, if 
>>> it is. And this problem creates other problems.
>>>
>>> Before opening a feature request, I decided to ask JavaFX 
>>> developers, if it possible to disable JavaFX shutdown hook.
>>> I mean, if there is no system property (something like 
>>> javafx.shutdownhook.disabled), it will be added, but if I want
>>> to call Platform.exit() manually I want to be able to disable JavaFX 
>>> shutdown hook.
>>>
>>> Best regards, Pavel
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/openjfx-dev/attachments/20240727/b070fedd/attachment-0001.htm>


More information about the openjfx-dev mailing list