Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'instance,': 0.05; '(self,': 0.09; 'spawn': 0.09; 'subprocess': 0.09; 'exception': 0.12; 'def': 0.13; '(eg': 0.16; '(none': 0.16; '(otherwise,': 0.16; '(out': 0.16; 'called,': 0.16; 'close()': 0.16; 'curiosity,': 0.16; 'e.errno': 0.16; 'header:X-Face:1': 0.16; 'instance)': 0.16; 'narrow': 0.16; 'process)': 0.16; 'received:lan': 0.16; 'returncode': 0.16; 'returned,': 0.16; 'skip:# 60': 0.16; 'terminated.': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'example.': 0.18; 'jan': 0.19; 'cc:no real name:2**0': 0.20; 'header:In-Reply-To:1': 0.22; 'statement': 0.23; 'module,': 0.23; 'cc:2**0': 0.24; "python's": 0.24; 'obviously': 0.25; 'variable': 0.28; 'raise': 0.28; 'pass': 0.29; 'allocated': 0.29; 'looks': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'error': 0.29; 'pm,': 0.29; 'generally': 0.30; 'chris': 0.30; 'thanks': 0.31; 'tue,': 0.32; "can't": 0.32; "won't": 0.33; 'actually': 0.33; 'this.': 0.33; 'there': 0.33; 'checking': 0.34; 'init': 0.34; 'try:': 0.34; 'something': 0.35; 'window': 0.35; 'unless': 0.35; 'however,': 0.36; 'subject:with': 0.36; 'except': 0.37; "there's": 0.37; 'not,': 0.37; 'skip:_ 10': 0.37; 'using': 0.38; 'signal': 0.38; 'some': 0.38; 'hoping': 0.38; 'should': 0.39; 'else': 0.39; 'called': 0.40; 'might': 0.40; 'extremely': 0.40; 'skip:s 40': 0.40; 'within': 0.60; 'your': 0.61; 'happen': 0.61; 'received:89': 0.64; '2012': 0.67; 'alive': 0.67; 'probability': 0.67; 'low': 0.74; 'dead': 0.77; 'race': 0.77; 'protect': 0.78; 'priority,': 0.84; 'window,': 0.84; 'to:none': 0.93 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on jeftof X-Spam-Level: X-Spam-Status: No, score=0.2 required=7.0 tests=ALL_TRUSTED,MISSING_HEADERS autolearn=no version=3.3.1 Date: Tue, 3 Jan 2012 10:38:03 +0100 From: =?UTF-8?B?SsOpcsO0bWU=?= Cc: python-list@python.org Subject: Re: Avoid race condition with Popen.send_signal In-Reply-To: References: <63817f2b-ccf8-4d7d-92a6-c1d622986d8a@j10g2000vbe.googlegroups.com> <20120103094415.072db024@bouzin.lan> 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: 94 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1325583331 news.xs4all.nl 6842 [2001:888:2000:d::a6]:45313 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18389 Tue, 3 Jan 2012 19:58:57 +1100 Chris Angelico a =C3=A9crit: > On Tue, Jan 3, 2012 at 7:44 PM, J=C3=A9r=C3=B4me wro= te: > > If so, I don't see how I can protect myself from that. Checking the > > process is alive and then hoping that the time interval for the race > > condition is so small that there are few chances for that to happen > > (because the OS quarantines PID numbers for a while, for instance) ? >=20 > The probability is extremely small. PIDs are generally allocated > sequentially, and obviously one won't be reallocated until the > previous process has terminated. You're looking at a narrow window of > opportunity between a check and an action; you don't really need to > worry about PID reuse within that window, unless there's a particular > reason to fear it (eg your process is very low priority, or there's a > lot of "process spinning" happening). Under normal circumstances, you > won't see a new process start up with the same PID for some time. >=20 > (I can't make a statement on Python's module, though.) Thanks for clarifying this. (Out of curiosity, what would be the way to be sure when not in "normal circumstances" ?) So I rely on the OS for not allocating a "recently released" PID. However, = if the PID was released long ago, I still need to cover myself up as Popen won= 't do it for me. E.g.: I have an application that can spawn a subprocess to play a beep. I want it to kill the subprocess when exiting. To do so, my close() method must a/ Check if any subprocess has actually been launched (I store the Popen = in a variable called _beep_process. If Popen has not been called, the variab= le is init to 0 and the call to send_signal will fail.) b/ Check if the process is still alive using Popen.poll() and returncode (otherwise, I might kill a new process) c/ Catch the exception in case the process would be dead since the last check (otherwise, I might get an error from send_signal) It looks like this : ##################################################################### # Close ##################################################################### def _close (self, widget): # If self._beep_process !=3D 0, a subprocess was launched at some p= oint if (0 !=3D self._beep_process): print "process launched" self._beep_process.poll() # If process still alive if (None =3D=3D self._beep_process.returncode): print "process stil alive" # Send signal try: self._beep_process.send_signal(signal.SIGINT) except OSError, e: if e.errno =3D=3D errno.ESRCH: print "process just died" pass # process already dead else: raise # something else wrong - raise exception else: print "signal sent" # wait for process to complete self._beep_process.wait() else: print "process already dead" # Close application Gtk.main_quit() ##################################################################### I would have expected something shorter. For instance, Popen.send_signal() should not be able to send a signal to a subprocess that has already returned, should it ? Is there any good reason for allowing it to do so ? If not, it would spare me check b/ in this examp= le. --=20 J=C3=A9r=C3=B4me