I am running a Middleman (which uses Webrick) server on JRuby inside a JVM process using the org.jruby.embed.ScriptingContainer
API.
If I shutdown cleanly a
First, a debugging trick: you can send a SIGQUIT
to a Java process to get it to print the current stack traces of all running threads. This can help you diagnose which threads are causing the JVM to not exit.
Sending a SIGINT
, SIGHUP
, or SIGTERM
causes Java to run its shutdown hooks, then exit. You can see this in java.lang.Terminator. Once the signal is received, java.lang.Shutdown handles running all the shutdown hooks before terminating the process. Shutdown.halt()
is not called until after the shutdown hooks have all finished, which suggests you have a shutdown hook that is hanging.
Without actual code or stack traces it's difficult to provide a more precise answer than that, but a JVM that stays alive after you send a SIGINT
is usually doing something strange in its shutdown hooks. If this isn't enough to go on try sending a SIGINT
, waiting a few seconds, then sending a SIGQUIT
, and adding the resulting stack traces to the question.
Looks like kill -9
does not trigger the shutdownHook, but kill -15
does.
This StackOverflow question has a detailed answer to the same problem.
Bottom line is, you're screwed, as there doesn't seem to exist any way to intercept a kill -9
signal and perform some maintenance tasks before the JVM halts.