-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
WIP: User-defined signal handling #59147
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
base: master
Are you sure you want to change the base?
Conversation
I've got a working prototype for user-defined signal handling working well on macOS. I'm pushing some of the more basic code first to flush out any platform specific bugs. |
I'm not sure if this is the expected path we want to go here. Specifically because running julia code inside of a signal handler might be very difficult (it's unclear what async signal safe would be in julia) |
Allows reinstalling individual signal handlers.
I haven't pushed any of the signal handling code here yet. The approach taken is to use a |
@jpsamaroo is probably interested in what this means. #49541 |
TODO:
I'll be on holidays for a bit after today so I'll probably not respond until I'm back |
function signal_router(condition::AsyncCondition) | ||
while isopen(condition) | ||
wait(condition) # Wait until notified by `jl_signal_router_condition` | ||
signum = ccall(:jl_consume_user_signal, Cint, ()) | ||
|
||
# Process all queued signals while the thread is active | ||
while signum != -1 | ||
signal_handler = @lock _SIGNAL_HANDLER_LOCK begin | ||
get(_SIGNAL_HANDLER, signum, nothing) | ||
end | ||
|
||
if !isnothing(signal_handler) | ||
invokelatest(signal_handler, signum) | ||
end | ||
|
||
signum = ccall(:jl_consume_user_signal, Cint, ()) | ||
end | ||
end | ||
return nothing | ||
end |
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.
We should probably automatically deregister the signal handler if anything in here throws, so that we don't infinitely accumulate signal messages in the runtime.
This looks awesome! Have you tested at all how this behaves with multiple threads, but a single task on thread 1 (where we do I/O) is blocked in |
Add a signal handler interface to Julia to trigger user defined code. The signals here are only received via an
AsyncCondition
to avoid issues with code which would be signal unsafe.I looked into using libuv's signal handing but ended up not going with that approach as the libuv signal handling resets the signal handler back to
SIG_DFL
when there are no more signal handlers. There was an issue about this (libuv/libuv#2435) which was fixed in libuv/libuv#4216 and later reverted. It turns out that it's hard to restore an old signal handler in a cross platform way (see libuv/libuv#4299) which is why I opted to makejl_install_default_signal_handler
.Fixes:
Related to: