JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.81k stars 5.49k forks source link

signal handling #14675

Open jtravs opened 8 years ago

jtravs commented 8 years ago

It would be very useful for there to be an official way for users to register signal handlers in julia programmes. In particular I'd like to be able to handle SIGNIT and SIGHUP, but there is no reason it shouldn't be more general, like python's signal module (https://docs.python.org/2/library/signal.html).

I know that signals are used a little in base julia (presumably through libuv?), and can see some code related to internal uses of signals, but what do people think about user facing mechanism?

My need is that on our compute servers the management software informs simulation runs that they need to save state and temporarily shut down (to be moved to other nodes or for maintenance etc.) using SIGINT and SIGHUP. Currently my code is in python and C++ but for the last couple of years I have been slowly migrating modules to julia. This code often runs for more than a month continuously and needs to sometimes be suspended. I know I could hack my own version with ccall, but think this might be more generally useful.

I'm happy to have a go at this.

I raided the same question on the julia-user list: https://groups.google.com/d/topic/julia-users/l0HJVCYriko/discussion

Keno commented 8 years ago

The primary question is just really how you want this exposed. If you come up with an API design that you like, implementing it is (probably) the easy part.

jtravs commented 8 years ago

I was thinking something similar to the python interface I mentioned:

function myhandler(signum, context)
    # handle signal, with some julia specific context
end

sighandler(:SIGINT, myhandler)  # register handler
vtjnash commented 8 years ago

related: https://github.com/JuliaLang/julia/pull/14032

vtjnash commented 8 years ago

implementing this is probably dependent on threaded gc-statepoints (the way python implements this is extremely expensive in that it slows down the execution of all code)

elextr commented 8 years ago

Could it not be exposed as a condition created from the signum that a task could wait on and be woken when the signal occurred?

randy3k commented 8 years ago

So how 's the process of the signal handler (in particular SIGINT). It is needed in RCall.jl when running expensive R code.

Once it is implemented, RCall REPL mode could completely substitute the old and sloppy R console. Thanks to the work of LineEdit.jl, it works much much better than GNU readline.