This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author boye
Recipients boye
Date 2008-10-13.10:36:17
SpamBayes Score 1.6653345e-16
Marked as misclassified No
Message-id <1223894179.31.0.303015562598.issue4112@psf.upfronthosting.co.za>
In-reply-to
Content
subprocess.Popen.wait() hangs if you have spawned multiple child
processes that do not terminate until their standard input is closed
('cat' is example of such a process).  This happens on POSIX platforms.
 I'm using Python 2.5.1, but I believe the issue is present in the SVN
trunk version of subprocess as well.

Here is a test program:
--------------------------
import subprocess, sys

p1 = subprocess.Popen("cat", bufsize=0, stdin=subprocess.PIPE)
p2 = subprocess.Popen("cat", bufsize=0, stdin=subprocess.PIPE)

p1.stdin.close()
ret = p1.wait()
print >>sys.stderr, "Child 1 wait completed with ret", ret

p2.stdin.close()
ret = p2.wait()
print >>sys.stderr, "Child 2 wait completed with ret", ret, "Bye bye"
--------------------------

The call to p1.wait() will never return.  If p2.wait is called first,
the program terminates cleanly.

p1 never terminates because p1.stdin is duplicated in the (second) child
process when the parent process forks as part of the call to p2 =
subprocess.Popen().  The line p1.stdin.close() thus has no effect.

I am not sure whether this is a bug or deliberate design, but the
behavior above seems a bit weird to me, and it took me quite some time
to figure out what happened.  However, the proposed fix below of course
has side effects that may be undesirable.

wait() will not hang if  the "close on exec" flag is set on the
subprocess pipes.  Conveniently, the method _set_cloexec_flag already
exists to do this, so the fix amounts to calling this from
Popen._get_handles:

elif stdin == PIPE:
    p2cread, p2cwrite = os.pipe()
    self._set_cloexec_flag(p2cwrite)

The last line is the added one, similar lines must be added for stdout
and stderr.

Alternatively, the test program above will terminate cleanly if
"p1._set_cloexec_flag(p1.stdin)" is added before the creation of p2.
History
Date User Action Args
2008-10-13 10:36:19boyesetrecipients: + boye
2008-10-13 10:36:19boyesetmessageid: <1223894179.31.0.303015562598.issue4112@psf.upfronthosting.co.za>
2008-10-13 10:36:18boyelinkissue4112 messages
2008-10-13 10:36:17boyecreate