Java ShutDownHook not run
See original GitHub issueEven with “stoptimeout” set, the ShutDownHook is never run when the service is stopped. The ShutDownHook does run if the Java application throws an uncaught exception.
Log is
2018-07-01 19:33:23,134 INFO - Stopping DRS IDB DEV Test 2018-07-01 19:33:23,149 DEBUG - ProcessKill 8732 2018-07-01 19:33:23,250 INFO - Found child process: 7420 Name: conhost.exe 2018-07-01 19:33:23,290 INFO - Stopping process 7420 2018-07-01 19:33:23,309 INFO - Send SIGINT 7420 2018-07-01 19:33:23,314 WARN - SIGINT to 7420 failed - Killing as fallback 2018-07-01 19:33:23,324 INFO - Stopping process 8732 2018-07-01 19:33:23,334 INFO - Send SIGINT 8732 2018-07-01 19:33:23,339 WARN - SIGINT to 8732 failed - Killing as fallback 2018-07-01 19:33:23,344 INFO - Finished DRS IDB DEV Test 2018-07-01 19:33:23,354 DEBUG - Completed. Exit code is 0
Code is
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
I spent some more time looking at this. Given interrupt works when running the Java application from the command line but it doesn’t when running as a Windows service, I feel like this must be isolated to some protections/differences of launching the application as a sub process of a Windows service. The weird thing though is:
<stopparentprocessfirst>true</stopparentprocessfirst>
makes it so it looks likeSIGINT
works but then shutdown hooks still do not run. Here’s example output of this startup/shutdown when I have configured this:The process is infact killed just the shutdown hooks don’t run.
Possibly related a bit to https://github.com/kohsuke/winsw/issues/95
On Windows 8 (just VM I had available, could try other versions from free dev VM downloads Microsoft provides) - I tried setting:
While it does report the
SIGINT
didn’t fail in this case, the shutdown hooks don’t end up running still. Different variations I’ve tried so far:-Xrs
passed to java launchjavaw
(doesn’t use a console) instead ofjava
I grabbed the
sendctrlc
executable fromwinp
here - https://github.com/kohsuke/winp/blob/master/native/sendctrlc/main.cpp#L17 - which is basically doing the same thing this code appears to be doing. My results here are:sendctrlc
the process cannot be interrupted and it ends with error code 5 (ERROR_ACCESS_DENIED)sendctrlc
it interrupts correctly (and runs shutdown hooks)The documentation states:
So, I tried with
javaw
but it failed the same way. Not sure what’s going on as I’m not really familiar with the quirks of these APIs. I did end up findingwinp
in the first place because I was looking at how IntelliJ handles this as I believe it works correctly (although I use OSX primarily so need to verify that!). IntelliJ does delegate its graceful install to thewinp
library though which then does the same thing essentially that thewinsw
C# code is doing.