[New-bugs-announce] [issue1236] subprocess is not thread-safe

Kenneth Duda report at bugs.python.org
Thu Oct 4 23:31:58 CEST 2007


New submission from Kenneth Duda:

The following test program crashes:

========================================
import threading, sys, subprocess

# subprocess._cleanup = lambda: None

def doit():
   for i in xrange(0, 1000):
      p = subprocess.Popen( "true" )
      p.wait()

t = threading.Thread( target=doit )
t.start()
doit()

==============================

It crashes because when one thread calls subprocess.Popen(), subprocess
calls this _cleanup() function, which might reap the subprocess started
in another thread !  The other thread might be inside
subprocess.Popen.wait(), just about to call waitpid(), and kill itself.

If you uncomment the commented line, then the program runs with no problems.

I imagine the purpose of _cleanup is to protect users from themselves,
i.e., protect a user who calls subprocess.Popen() a lot without ever
calling wait().  I suggest either:

  (1) eliminating this _cleanup() mechanism completely; people who do
not wait() deserve the zombies they get;
  (2) synchronizing _cleanup() with wait() through a lock; or,
  (3) having wait() simply retry if it gets ECHILD.  On the retry, it
will discover that returncode is set, and return normally.

-Ken

----------
components: Library (Lib)
messages: 56230
nosy: kjd at duda.org
severity: normal
status: open
title: subprocess is not thread-safe
type: crash
versions: Python 2.5

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1236>
__________________________________


More information about the New-bugs-announce mailing list