[issue35537] use os.posix_spawn in subprocess

STINNER Victor report at bugs.python.org
Thu Dec 20 16:45:36 EST 2018


STINNER Victor <vstinner at redhat.com> added the comment:

I wasn't sure how posix_spawn() handles file descriptors of standard streams with O_CLOEXEC flag set.

I wrote a test. Result: _posixsubprocess and os.posix_spawn() have the same behavior! If fd 2 (stderr) is marked with O_CLOEXEC, the fd 2 is closed in the child process, as expected. (There is no black magic to keep it open.)

$ cat x.py 
import subprocess, sys, os
args = [sys.executable, 'y.py']
os.set_inheritable(2, False)
subprocess.run(args, close_fds=False, restore_signals=False)

$ cat y.py 
import os
def fd_valid(fd):
    try:
        os.fstat(fd)
        return True
    except OSError:
        return False
for fd in (0, 1, 2):
    print("fd %s valid? %s" % (fd, fd_valid(fd)))

$ python3 x.py  # unpatched
fd 0 valid? True
fd 1 valid? True
fd 2 valid? False

$ ./python x.py # patched, use posix_spawn()
fd 0 valid? True
fd 1 valid? True
fd 2 valid? False

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35537>
_______________________________________


More information about the Python-bugs-list mailing list