Avoid race condition with Popen.send_signal

Heiko Wundram modelnine at modelnine.org
Tue Jan 3 08:56:49 EST 2012


Am 03.01.2012 14:40, schrieb Adam Skutt:
> On Jan 3, 7:31 am, Heiko Wundram<modeln... at modelnine.org>  wrote:
>> Yes, it can be avoided, that's what the default SIGCHLD-handling
>> (keeping the process as a zombie until it's explicitly collected by a
>> wait*()) is for, which forces the PID not to be reused by the operating
>> system until the parent has acknowledged (by actively calling wait*())
>> that the child has terminated.
>
> No, you still can see ESRCH when sending signals to a zombie process.
> Code that sends signals to child processes via kill(2) must be
> prepared for the call to fail at anytime since the process can die at
> anytime.  It can't handle the signal, so it's treated as if it doesn't
> exist by kill(2) in this case.  However, you don't have to worry about
> sending the signal to the wrong process.

Getting an error on kill (which you can catch) is not about the race 
that the posters were speculating about (i.e., sending the signal to the 
wrong process), and that's what I was trying to put straight. The only 
advice that I wanted to give is:

1) before calling wait to collect the child, call kill as much as you 
like, and in case it errors, ignore that,

2) after calling wait, never, ever kill, and you don't need to, because 
you already know the process is gone.

There's no race possibility in this, _except_ if you alter handling of 
SIGCHLD away from the default (i.e., to autocollect children), in which 
case you have the possibility of a race and shooting down unrelated 
processes (which the discussion was about).

-- 
--- Heiko.



More information about the Python-list mailing list