subprocess.Popen does not close pipe in an error case

Steven K. Wong skwslide at gmail.com
Wed Jan 6 22:05:40 EST 2010


Well, the example code at http://www.python.org/doc/2.6.2/library/subprocess.html#replacing-shell-pipeline
has the same issue:

output=`dmesg | grep hda`
==>
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]

After communicate() returns, if you wait for p1 to finish (by calling
p1.poll() repeatedly or p1.wait()), you can hang if the conditions
described in the original post are true, i.e. p1 wrote lots of data to
the pipe and p2 failed without reading much data from the pipe.

Perhaps the doc can be improved to remind folks to close p1.stdout if
the calling process doesn't need it, unless wait() is changed to close
it and p1.wait() is called.

Am I making any sense here?



More information about the Python-list mailing list