Handling SIGTERM in Julia

Hi, from researching a bit I learned that handling unix signals has been tricky in Julia for a while. Our queuing systems send signals to programs to indicate they should come to an end; and I’d like to do stuff like storing all data in response to that. Did I miss something or is that still impossible in Julia code?

(I could probably achieve the same results by creating and deleting files, and checking their existence periodically - if anyone has code for that please let me know.)

1 Like

Just register an atexit function. That will be called on a SIGTERM.

3 Likes

Can you change the signal to be sent from the queuing system to SIGINT? Then there is a way to use try-catch for this. From How do I catch CTRL-C in a script?:

Running a Julia script using julia file.jl does not throw InterruptException when you try to terminate it with CTRL-C (SIGINT). To run a certain code before terminating a Julia script, which may or may not be caused by CTRL-C, use atexit. Alternatively, you can use julia -e 'include(popfirst!(ARGS))' file.jl to execute a script while being able to catch InterruptException in the try block.

1 Like

I agree that could also be a solution; I’m sure it would be possible to write a bash-wrapper that converts catches SIGTERM and sends SIGINT instead. The atexit() hook seems to be a much more elegant solution though.

Thank you, that’s perfect.

Reviving this thread because the links to the documentation seem dead now. We are looking for a good design for gracefully exiting an iterative procedure in FrankWolfe.jl

We would like not to make normal use cases pay the price for interruptions handling they don’t use, so try-catching the whole inner block is to be avoided. Would there be a recommended way to do it, not too hacky if possible.

Update doc links:

4 Likes

Thanks! This confirms neither of these is suited to our use case. Pinging @cscherrer who found a solution when this is not a hot loop being interrupted

1 Like

This mostly works, but it’s easy to find cases where it doesn’t:

julia> macro cleanbreak(ex)
           quote
               try
                   $(esc(ex))
               catch e
                   if e isa InterruptException
                       @warn "Computation interrupted"
                   else
                       rethrow()
                   end    
               end
           end 
       end
@cleanbreak (macro with 1 method)

Here’s an example:

julia> x = 0
0

julia> @cleanbreak while true
       x += 1
       end
^C┌ Warning: Computation interrupted
└ @ Main REPL[1]:7

julia> x
18298742

And here’s an example from before I had made it into a macro. Much more realistic example, and you could use the macro to simplify it a bit:

3 Likes