Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python sigkill catching strategies

I was wondering if there was any way to catch the sigkill from the OOM killer. I have a task queue, and every so often a mammoth task is created that is killed by OOM. This:

catch Exception as ex:
    # clean up!

does not work, as SIGKILL can't be caught. So........is there ANY strategy to clean up after a SIGKILL? Can I fork, and watch the child process? If so, any resources opened by the child process would have to be known in advance by the parent? Or could I just do some version of

ps -ef | grep <child pid> | xargs kill -9  (you get the idea...)

Currently, if I don't clean up after an OOM kill, I leave behind plenty of child processes and other things that just make it worse when the task is retried, and soon enough, the server is unreachable.

Finally, is it enough to just do:

kill -9 <process id> 

to test this exact situation?

Thanks very much!

like image 448
Hoopes Avatar asked Jun 09 '15 12:06

Hoopes


People also ask

How do you catch a SIGKILL in Python?

SIGKILL by its very nature cannot be trapped. The SIGKILL signal is sent to a process to cause it to terminate immediately (kill). In contrast to SIGTERM and SIGINT, this signal cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving this signal.

Can SIGKILL be caught?

You can't catch SIGKILL (and SIGSTOP ), so enabling your custom handler for SIGKILL is moot. You can catch all other signals, so perhaps try to make a design around those. be default pkill will send SIGTERM , not SIGKILL , which obviously can be caught.

In what cases does SIGKILL fail?

SIGKILL cannot be blocked or ignored ( SIGSTOP can't either). A process can become unresponsive to the signal if it is blocked "inside" a system call (waiting on I/O is one example - waiting on I/O on a failed NFS filesystem that is hard-mounted without the intr option for example).

What does SIGTERM do vs SIGKILL?

SIGTERM gracefully kills the process whereas SIGKILL kills the process immediately. SIGTERM signal can be handled, ignored, and blocked, but SIGKILL cannot be handled or blocked. SIGTERM doesn't kill the child processes. SIGKILL kills the child processes as well.


Video Answer


2 Answers

SIGKILL by its very nature cannot be trapped.

See http://en.wikipedia.org/wiki/Unix_signal#SIGKILL:

SIGKILL

The SIGKILL signal is sent to a process to cause it to terminate immediately (kill). In contrast to SIGTERM and SIGINT, this signal cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving this signal.

The best thing to do is the next time your process launches, look for anything that needs to be cleaned up.

And yes, kill -9 <pid> will send a SIGKILL to the process. (To be precise, it sends the 9th signal - it just happens that SIGKILL has the number 9 on pretty much every system. You could alternatively write kill -KILL <pid>, which lets you specify the signal by name instead of by number in a portable way.)

like image 188
Mike Shoup Avatar answered Sep 19 '22 16:09

Mike Shoup


The Linux OOM killer works by sending SIGKILL.

To kill the selected process, the OOM killer delivers a SIGKILL signal.

kill -9 <-- Works

like image 33
user3099947 Avatar answered Sep 21 '22 16:09

user3099947