IllegalArgumentException when trying to handle SIGBREAK in Hotspot JVM on Windows

白昼怎懂夜的黑 提交于 2019-12-12 03:46:39

问题


I'm trying to use the signal handling classes in the sun.misc package (as documented here) to handle SIGBREAK in Hotspot JVM on Windows so I can trigger shutdown on Ctrl+Break instead of just dumping threads. However, I'm running into a situation where it throws an IllegalArgumentException saying that SIGBREAK is already being handled by the OS or the VM when I try to set up the handler. This happens even when I run the JVM with the -Xrs flag which supposedly disables handling of the SIGBREAK signal.

Does anyone have any experience handling signals using sun.misc.Signal on Windows? Is there a way around this?


回答1:


This seems to be a quite confusing topic.

The documentation of the java launcher specifies:

When the -Xrs option is used, the JVM does not install a console control handler, implying that it does not watch for or process CTRL_C_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, or CTRL_SHUTDOWN_EVENT.

Note that CTRL_BREAK_EVENT is not among the list. So it’s indeed not affected by this option, but it’s strange, that the documentation claims that specifying -Xrs will imply that “Ctrl + Break thread dumps are not available”, when this feature is entirely unaffected.

Further, the implications are entirely the opposite of what a reader would expect. When the JVM does not listen to these events, due to the -Xrs option, it can’t forward them to a Java side handler, in other words, does not support signal handlers to be registered for them. This can be easily testet by trying to install signal handlers for the signals affected by that option, INT and/or TERM, it works only when -Xrs is not specified.

By looking into the code that tries to install a signal handler:

long oldH = handle0(sig.number, newH);
if (oldH == -1) {
    throw new IllegalArgumentException
        ("Signal already used by VM or OS: " + sig);
}

we see that the generic answer to all kinds of failures is -1, so we should take the exception message “Signal already used by VM or OS” not too literally. At this place, there is no room to report different reasons, like “the handler could not be installed, because the JVM will not install handlers of that kind due to the -Xrs option”.

As far as I understood, the -Xrs option is supposed to allow native code to install handlers for the signals at a lower level, without the JVM interfering, not to make signals available for the Java side sun.misc.Signal handling.

Regarding the issue with SIGTERM, it seems to entire documentation is out of sync with the reality. As of my tests, the last JVM, on which specifying -Xrs did make CTRL+BREAK thread dumps unavailable, was Java 6. But, as explained above, to the outcome that the signal would be available to native code, causing an abort in the absence of such handlers, not to make it available to sun.misc.Signal. Even under that JVM, it was impossible to install a SignalHandler for SIGBREAK, whether the -Xrs option was present or not.



来源:https://stackoverflow.com/questions/40244888/illegalargumentexception-when-trying-to-handle-sigbreak-in-hotspot-jvm-on-window

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