""" Running this script triggers a race condition in the subprocess module. There are 3 distinct behaviours: (1) Terminate correctly (2) Hang indefinitely (3) Terminate, with exceptions such as Exception in thread Thread-47: Traceback (most recent call last): File "/usr/lib/python2.4/threading.py", line 442, in __bootstrap self.run() File "popen_bug.py", line 53, in run pipe.wait() File "/usr/lib/python2.4/subprocess.py", line 1007, in wait pid, sts = os.waitpid(self.pid, 0) OSError: [Errno 10] No child processes I have observed this on the following platforms : Python 2.4.3 (#1, Jan 10 2007, 17:25:58) [GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2 and Python 2.4.4 (#1, Jan 30 2007, 09:52:03) [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-56)] on linux2 The script was originally written by Taale Skogan as a problem in popen2. Test this module instead with the -p option. I have reproduced this once with Python 2.4.4 but it's much harder to reproduce than the subprocess issue. According to him it was observed on Python 2.3.3 (#1, Feb 10 2004, 17:28:29) [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-24)] on linux2 and Python 2.3.5 (#1, Mar 30 2005, 17:01:57) [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-49)] on linux2 and Python 2.4.1 (#1, Apr 15 2005, 12:58:11) [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-34)] on linux2 and Python 2.4.1 (#1, Apr 15 2005, 13:32:19) [GCC 2.95.2 19991024 (release)] on sunos5 He also claims he got no error here on this platform: Python 2.3.5 (#1, Apr 6 2005, 11:30:31) [GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-113)] on linux2 """ import optparse import popen2 import sys import threading class test_popen2(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run (self): for i in range(5): pipe = popen2.Popen4("ls > /dev/null") pipe.wait() class test_subprocess(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run (self): import subprocess for i in range(5): pipe = subprocess.Popen("ls > /dev/null", shell=True) pipe.wait() def main(argv): """ Provoke the Popen bug. """ usage = "usage: %prog" parser = optparse.OptionParser(usage) parser.add_option("-n", action="store",type="int", dest="num_threads", help="start NUM_THREADS threads", default=50) parser.add_option("-p", action="store_false", dest="subprocess", help="Use popen2 instead of subprocess", default=True) (options, args) = parser.parse_args(argv) if options.subprocess: worker = test_subprocess else: worker = test_popen2 for i in range(options.num_threads): t = worker() t.start () print "Started %d threads" % options.num_threads if __name__ == '__main__': main(sys.argv[1:])