RFR(s): Support graceful application termination on Windows shutdown/logoff

Nikola Grcevski Nikola.Grcevski at microsoft.com
Thu Jul 30 16:25:38 UTC 2020

Hi core-libs-dev,

I've been searching extensively the mailing list archives and JBS, but I cannot
find any more information on this topic, apart from the two linked bug reports [1] and [2].

I apologize for my newbie �� search skills, can someone please help review the email
I sent to hotspot-runtime-dev below. 

Essentially, no shutdown code runs on normal Windows logoff/shutdown since Windows 7, 
which I believe was reported as change in behaviour under [1].

I want to point out that the issue has nothing to do with Virtual Machines, the JVM
simply doesn't receive the correct events, unless there's an AWT window open.
The implementation for WM_ENDSESSION in AWT seems to have a small issue that
the java process will terminate regardless of the user changing their mind about shutting down.


[1] https://bugs.openjdk.java.net/browse/JDK-8079631
[2] https://bugs.openjdk.java.net/browse/JDK-7068835

-----Original Message-----
From: David Holmes <david.holmes at oracle.com> 
Sent: July 23, 2020 11:02 PM
To: Nikola Grcevski <Nikola.Grcevski at microsoft.com>; core-libs-dev Libs <core-libs-dev at openjdk.java.net>
Cc: hotspot-runtime-dev at openjdk.java.net
Subject: Re: RFR(s): Support graceful application termination on Windows shutdown/logoff

Hi Nikola,

I'm redirecting this to the core-libs team initially because this is an issue that has been raised and discussed considerably in the past (possibly with some misunderstanding relating to the WM_ENDSESSION event). The core-libs team need to confirm the intended semantics here and we (runtime) can then implement whatever is determined to be needed. 
Interaction with the client team for AWT interoperability may also be needed.


On 24/07/2020 11:47 am, Nikola Grcevski wrote:
> Hello hotspot-runtime-dev,
> After some recent investigation into stale files remaining after Java 
> process terminates on Windows shutdown, we noticed that there's 
> missing support for detecting Windows shutdown/logoff events for 
> interactive Java applications. Given that Java loads both GDI32.dll 
> and USER32.dll, even for console applications, this means that almost 
> all Java processes launched on Windows don't run any shutdown hooks at the moment on user logoff or system shutdown/restart.
> Since Windows 7, all Windows applications that load (or transitively 
> call) GDI32.dll or USER32.dll will not receive the CTRL_LOGOFF_EVENT 
> and CTRL_SHUTDOWN_EVENT events, but instead they will be sent WM_ENDSESSION.
> This is documented in MSDN under the following article:
> https://docs.microsoft.com/en-us/windows/console/setconsolectrlhandler
> It appears that this issue was logged in JSB at some point, but it was 
> made duplicate of another issue:
> https://bugs.openjdk.java.net/browse/JDK-8079631
> The behaviour changed going from Windows Vista to Windows 7.
> I've made a proposal patch to address this issue under the following webrev:
> http://cr.openjdk.java.net/~adityam/nikola/wm_endsession_handling/
> At the moment only AWT applications would terminate gracefully on 
> shutdown/logoff, because they have support for listening on WM_ENDSESSION.
> There's a bug in the AWT code, it doesn't check for wparam upon 
> receiving the event, but it will work in most cases. If this patch is 
> accepted I can submit a follow-up patch for AWT to resolve the possible issues.
> Finally, there are third set of events for service processes, for 
> example java applications which are started with a Windows Service 
> wrappers. These services work with SERVICE_ACCEPT_SHUTDOWN and SERVICE_CONTROL_SHUTDOWN.
> Once the most common case is resolved, I'd like to submit perhaps a 
> follow-up patch to support graceful termination of Java as Windows service programs.
> We are working to amend the MSDN documentation for 
> SetConsoleCtrlHandler to specify that this behaviour change is also 
> present on server OSs. The documentation only mentions the workstation OS flavours at the moment.
> Thanks in advance for reviewing this.
> Nikola Grcevski
> Microsoft

More information about the hotspot-runtime-dev mailing list