Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.078 X-Spam-Evidence: '*H*': 0.85; '*S*': 0.00; 'raised': 0.07; 'finished,': 0.09; 'subprocess': 0.09; '(none': 0.16; '[errno': 0.16; 'header:X-Face:1': 0.16; 'oserror:': 0.16; 'received:lan': 0.16; 'try/except': 0.16; 'thanks.': 0.26; 'skip:" 30': 0.28; 'error': 0.29; 'skip:p 30': 0.29; 'args)': 0.30; 'there': 0.33; 'to:addr:python-list': 0.34; 'all.': 0.34; 'elegant': 0.34; 'issue': 0.35; 'file': 0.36; 'subject:with': 0.36; 'but': 0.37; 'signal': 0.38; 'should': 0.39; 'to:addr:python.org': 0.40; 'more': 0.61; 'type': 0.61; 'received:89': 0.64; 'alive': 0.67; 'race': 0.77; 'processus': 0.84; 'running,': 0.84; 'safer,': 0.84 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on jeftof X-Spam-Level: X-Spam-Status: No, score=-1.0 required=7.0 tests=ALL_TRUSTED autolearn=unavailable version=3.3.1 Date: Tue, 3 Jan 2012 00:09:14 +0100 From: =?UTF-8?B?SsOpcsO0bWU=?= To: python-list@python.org Subject: Avoid race condition with Popen.send_signal X-Mailer: Claws Mail 3.7.10 (GTK+ 2.24.8; x86_64-pc-linux-gnu) X-Face: "kBB1-!wF@,"j_&tJ&7; T,t)PeQkZg5?.:{p,s>/,+?b6pN5!yxZy^nRXA=*?W+|J9OG!W[rdx^VA^Sx`R"g,; +MzhAq"tZFg27W4qX+ZXvLX=%piZ6c.7@oSDHyQ0Mff#HGx<{ Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1325545598 news.xs4all.nl 6937 [2001:888:2000:d::a6]:48735 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18355 Hi all. When a subprocess is running, it can be sent a signal with the send_signal method : process =3D Popen( args) process.send_signal(signal.SIGINT) If the SIGINT is sent while the process has already finished, an error is raised :=20 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 =3D Popen( args) process.poll() if (None =3D=3D 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. --=20 J=C3=A9r=C3=B4me