subprocess.Popen pipeline bug?

bryanjugglercryptographer at yahoo.com bryanjugglercryptographer at yahoo.com
Thu Mar 13 19:50:47 CET 2008


Marko Rauhamaa wrote:
> This tiny program hangs:
>
> ========================================================================
> #!/usr/bin/env python
> import subprocess
> a = subprocess.Popen('cat',shell = True,stdin = subprocess.PIPE,
>                      stdout = subprocess.PIPE)
> b = subprocess.Popen('cat >/dev/null',shell = True,stdin = a.stdout)
> a.stdin.close()
> b.wait() # hangs
> a.wait() # never reached
> ========================================================================

To make it work, add close_fds=True in the Popen that creates b.

> It shouldn't, should it?

Not sure. I think what's happening is that the second cat subprocess
never gets EOF on its stdin, because there are still processes with
an open file descriptor for the other end of the pipe.

The Python program closes a.stdin, and let's suppose that's file
descriptor 4. That's not enough, because the subshell that ran cat and
the cat process itself inherited the open file descriptor 4 when they
forked off.

It looks like Popen is smart enough to close the extraneous
descriptors for pipes it created in the same Popen call, but that
one was created in a previous call and passed in.


--
--Bryan



More information about the Python-list mailing list