8000 Use reflection to install system signal handlers in JVM applications by kyri-petrou · Pull Request #9251 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use reflection to install system signal handlers in JVM applications #9251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 22, 2024

Conversation

kyri-petrou
Copy link
Contributor

/fixes #9240

NOTE: This approach is a port of the cats-effect approach when installing system signal handlers.

In short, we use runtime reflection and a proxy class in order to avoid crashing the application with NoClassDefFoundError in cases that the JVM doesn't contain the sun.misc.Signal and sun.misc.SignalHandler classes. It also adds config to include these classes when compiling the code using GraalVM's native image.

I added a comment / question below, please let me know what you think

HANDLE_STATIC_METHOD_HANDLE = initHandleStaticMethodHandle(lookup, signalClass, signalHandlerClass);
SIGNAL_HANDLER = new SignalHandler.SunMiscSignalHandler(signalHandlerClass);
} else {
System.err.println("WARNING: sun.misc.Signal and sun.misc.SignalHandler are not available on this platform. " +
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of questions here:

  1. Should we use a logger instead of System.err.println?
  2. Should we add some option to disable this warning? Might be annoying in some cases, but I think as a default a user should be aware that the fiber dump functionality won't work as expected

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

java.logging ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the recommendation! I switched to using java.logging to log the warning and changed the code to make it a bit more readable / safe so that we log the warning if any of the reflective methods fail

Comment on lines 127 to 129
} catch (Throwable err) {
// Gracefully degrade to a no-op logging the exception.
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it safe to catch all Throwable?

Also, is there a reason to use Throwable here and Exception above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's mostly because the invoke method can throw any Throwable (depending on the underlying method) whereas the method above only throw exceptions. I think it's relatively safe to catch all Throwable's here because I don't think we want to crash the application in the very unlikely edge-case that we failed to handle a signal

* JVMs.
* </p>
*/
final class Signal {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to write this in java?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, not really. I'm not really sure why I thought we'd need Java for this. I rewrote the implementation in Scala

@kyri-petrou kyri-petrou requested review from hearnadam and ghostdogpr and removed request for hearnadam October 21, 2024 04:44
@kyri-petrou kyri-petrou merged commit cbf3cc8 into zio:series/2.x Oct 22, 2024
18 checks passed
@kyri-petrou kyri-petrou deleted the no-op-signal-handler branch February 10, 2025 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dependency on sun.misc.SignalHandler causing NoClassDefFoundError
3 participants
0