Avoid race condition with Popen.send_signal

J

Jérôme

Hi all.

When a subprocess is running, it can be sent a signal with the send_signal
method :

process = Popen( args)
process.send_signal(signal.SIGINT)

If the SIGINT is sent while the process has already finished, an error is
raised :

File "/usr/lib/python2.7/subprocess.py", line 1457, in send_signal
os.kill(self.pid, sig)
OSError: [Errno 3] Aucun processus de ce type

To avoid this, I can check that the process is still alive :

process = Popen( args)
process.poll()
if (None == process.returncode):
process.send_signal(signal.SIGINT)

It makes safer, but there is still an issue if the process ends between
poll() and send_signal().

What is the clean way to avoid this race condition ?

Should I use try/except to catch the error or is there a more elegant way to
go ?

Thanks.
 
A

Adam Skutt

Hi all.

When a subprocess is running, it can be sent a signal with the send_signal
method :

process = Popen( args)
process.send_signal(signal.SIGINT)

If the SIGINT is sent while the process has already finished, an error is
raised :

  File "/usr/lib/python2.7/subprocess.py", line 1457, in send_signal
    os.kill(self.pid, sig)
OSError: [Errno 3] Aucun processus de ce type

To avoid this, I can check that the process is still alive :

process = Popen( args)
process.poll()
if (None == process.returncode):
    process.send_signal(signal.SIGINT)

It makes safer, but there is still an issue if the process ends between
poll() and send_signal().

What is the clean way to avoid this race condition ?

The fundamental race condition cannot be removed nor avoided. Ideally,
avoid the need to send the subprocess a signal in the first place. If
it cannot be avoided, then trap the exception.

Adam
 
H

Heiko Wundram

Am 03.01.2012 02:19, schrieb Adam Skutt:
The fundamental race condition cannot be removed nor avoided. Ideally,
avoid the need to send the subprocess a signal in the first place. If
it cannot be avoided, then trap the exception.

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.
 
A

Adam Skutt

Am 03.01.2012 02:19, schrieb Adam Skutt:



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.

Adam
 
H

Heiko Wundram

Am 03.01.2012 14:40, schrieb Adam Skutt:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top