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
Support shutdown hooks with signals #3821
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall LGTM, only 2 things I don't think are correct
This seems to work now. Avoid race without calling shutdown hooks twice and call |
I'll need to fix this but not sure what value to put.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the tools tests it's fine to just update the values to the number of found symbols/classes + alignment to % 50
so small changes won't affect it much. Maybe we should remove this test.
Also, this use cases seems to be good candidate for scripted test. We probably should be able to create a custom sbt task to start compiled program (hopefully using multitple threads running in the lop) and create a task to run Process.destroy
- I guess it should be sending the SIGINT / SIGTERM signal
} | ||
|
||
private def handleSignal(sig: CInt): Unit = { | ||
Runtime.getRuntime().runHooks() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it won't work when we'll have multiple threads running. Starting runHooks
would also start scala.scanative.runtime.JoinNonDaemonThreads
which would wait for all thread to finish. We should at least remove this shutdown hook.
Also signal handler might be run by any of the threads. Which might affect its execution.
Can it help with #2483 ? |
lazy val setupSignalHandler = { | ||
signal.signal(signal.SIGINT, handleSignal(_)) | ||
signal.signal(signal.SIGTERM, handleSignal(_)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lazy val setupSignalHandler = { | |
signal.signal(signal.SIGINT, handleSignal(_)) | |
signal.signal(signal.SIGTERM, handleSignal(_)) | |
} | |
lazy val setupSignalHandler = { | |
signal.signal(signal.SIGHUP, handleSignal(_)) | |
signal.signal(signal.SIGINT, handleSignal(_)) | |
signal.signal(signal.SIGQUIT, handleSignal(_)) | |
signal.signal(signal.SIGTERM, handleSignal(_)) | |
} |
let's add a few more expected reason to quit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I understand "The JVM runs shutdown hooks only in case of normal terminations." - what this seems to mean is Ctrl-C SIGINT
and SIGTERM
. This is also in the current Test comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JVM catches signals to implement shutdown hooks for unexpected termination. The JVM uses SIGHUP, SIGINT, and SIGTERM to initiate the running of shutdown hooks.
Applications embedding the JVM frequently need to trap signals such as SIGINT or SIGTERM, which can lead to interference with the JVM signal handlers. The -Xrs option is available to address this issue. When -Xrs is used, the signal masks for SIGINT, SIGTERM, SIGHUP, and SIGQUIT aren't changed by the JVM, and signal handlers for these signals aren't installed.
https://docs.oracle.com/en/java/javase/21/docs/specs/man/java.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll add SIGHUP
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On second thought, we need to try and get this to work properly this way first as we are using clib
and SIGHUP
is not a member. I second pass could use posixlib
and something special for Windows to make it JVM compliant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep in mind that SIGHUP
is a signal which is send when SSH connection is hand up due network issue.
Without it, shutdown hook at CLI utilite won't work on this case and it's show stoper for real life use case I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem but we have nothing now and to make it cross platform is more complex so it might be better to have something now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM for the single threaded execution, I have prepared a variant handling the multithreaded case including handling on the GC side. I'll add the follow up PR soon
Thanks, I was looking into the multi-threaded version and it was quite a bit more involved. |
Now you can use Ctrl-C or
kill -s SIGTERM <pid>
and shutdown hooks will be called and the process will exit gracefully.Here is the
sandbox
I used - not sure how to test.