[issue46454] '0 -> /dev/null' is lost

Eryk Sun report at bugs.python.org
Sat Jan 22 14:57:02 EST 2022


Eryk Sun <eryksun at gmail.com> added the comment:

> If some one closes fd 0, then he reopens it. it will not be inherited.

In Windows, when a console process spawns a child console process without enabling handle inheritance -- e.g. subprocess.Popen(['python.exe']) -- the OS will manually duplicate (not inherit) the standard handles to the child. It doesn't matter in this case that a standard handle isn't inheritable. 

The latter also doesn't matter when any of the standard handles is overridden by a Popen() call in Windows -- e.g. subprocess.Popen(['python.exe'], stderr=DEVNULL). All of the standard handle values have to be overridden together. Popen() uses this as an opportunity to duplicate an inheritable handle for each that's not overridden, instead of just copying the handle value.

On the other hand, if subprocess.Popen() is called in Windows with handle inheritance enabled and without overriding the standard handles -- e.g. subprocess.Popen(['python.exe'], close_fds=False) -- then the standard handles should be inheritable. If any of the standard handles isn't inheritable, then at best the standard handle value in the child will be invalid or used by a handle for a kernel object type other than a file (e.g. process, event). By coincidence, however, a file open in the child could reuse the standard handle value, which can lead to buggy behavior.

----------
nosy: +eryksun

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


More information about the Python-bugs-list mailing list