How do I cleanly shut down an embedded JRuby in response to a SIGTERM to the JVM process?

痞子三分冷 提交于 2019-12-09 14:06:02

问题


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 and stop the server from inside the JVM, everything works as expected.

But if I send a SIGTERM to the JVM process (for example, by hitting ctrl+C at the command line), the console returns but the JVM process does not terminate - it hangs around indefinitely until I send it a SIGKILL.

I tried registering a JVM shutdown hook to terminate the ScriptingContainer instance, but the hook never fires. I'm not sure why... perhaps JRuby is swallowing the SIGTERM somehow?

How can I get the JVM to shut all the way down, cleanly, even if it contains a running Webrick server?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/29678645/how-do-i-cleanly-shut-down-an-embedded-jruby-in-response-to-a-sigterm-to-the-jvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!