what happens to Popen()'s parent-side file descriptors?

Chris Torek nospam at torek.net
Sat Oct 16 04:05:35 CEST 2010

>In message <pan.2010. at nowhere.com>, Nobody wrote:
>> Another gotcha regarding pipes: the reader only sees EOF once there are no
>> writers, i.e. when the *last* writer closes their end.

In article <i9atra$j4c$3 at lust.ihug.co.nz>
Lawrence D'Oliveiro  <ldo at geek-central.gen.new_zealand> wrote:
>Been there, been bitten by that.

"Nobody" mentioned the techniques of setting close_fds = True and
passing a preexec_fn that closes the extra pipe descriptors.  You
can also use fcntl.fcntl() to set the fcntl.FD_CLOEXEC flag on the
underlying file descriptors (this of course requires that you are
able to find them).

The subprocess module sets FD_CLOEXEC on the pipe it uses to pass
back a failure to exec, or even to reach the exec, e.g., due to an
exception during preexec_fn.  One could argue that perhaps it should
set FD_CLOEXEC on the parent's remaining pipe descriptors, once
the child is successfully started, if it created them (i.e., if
the corresponding arguments were PIPE).  In fact, thinking about it
now, I *would* argue that.
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)      http://web.torek.net/torek/index.html

More information about the Python-list mailing list