Linux – When a Python process is killed on OS X, why doesn’t it kill the child processes

killlinuxmacosprocesspython

I found myself getting very confused a while back by some changes that I found when moving Python scripts from Linux over to OS X…

On Linux, if a Python script has called os.system(), and the calling process is killed, the called process will be killed at the same time.

On OS X, however, if the main process is killed, anything that it launched is left behind.

Is there something somewhere in OS X/Python where I can change this behaviour?

This is causing problems on our render farm, where the processes can be killed from the management GUI, but the top level process is really just a wrapper, so, while the render farm management might think that the process has gone and the machine is freed up for another task, the actual processor-intensive task is still running, which can lead to huge blockages.

I know that I could write more logic to catch the kill signal and pass it on to the child processes, but I was hoping that it might be something that could be enabled at a lower level.

Best Answer

On Linux, when you kill a parent the child gets sent a SIGHUP which will generally kill it unless it was meant to stay alive as a daemon in which case it will trap sighup. (I think this is why one usually uses SIGHUP to tell a daemon to refresh itself, since it's conveniently always trapped).

On Mac OS X I can't find documentation, but it appears that no SIGHUP gets sent. As a result, the child process is orphaned, and its new parent is the grandparent.

The way you deal with this is you send the kill signal to the process group of the parent, not the parent process itself. This will nuke all the children and grand children as well with one caveat. If any child process does a setpgrp() or setsid() then it escapes the process group membership. It will not get the kill sent to its old process group. Usually one need not worry about the latter since it's usually intentional when used to achieve that purpose.

Related Question