Brett Randall <javabr...@gmail.com> added the comment:

For the possible benefit of future readers of this issue, the sequence and 
behaviour here which may cause confusion:

- say stdin is closed, perhaps along with stdout and stderr via a call to 
os.closerange(0, 3) or otherwise
- os.open() is called and creates a new fd=0, which will always be 0 since 0 is 
now available and lowest.  Per PEP 446 this fd is non-inheritable.
- a call to os.dup2(fd, 0) is made but is a noop, since fd=0 and dup2() ignores 
copy-to-same.  This behaviour may go unnoticed - it may not be noticed that the 
new fd=0 (it should always be this), or that dup2() has this noop behaviour.  
Per PEP 446, dup2() still creates inheritable by-default, so the caller may 
expect that they have just assigned an inheritable fd to stdin, instead of a 
noop.
- subprocess() is called and the subprocess does not have stdin/0 fd assigned, 
since it was not inheritable.

It seems the main way to avoid this is to os.open() the replacement fd _before_ 
closing stdin/0, in which case it will receive an fd >= 3.  The dup2(fd, 0) 
call will then work and the resulting fd 0 will be inheritable, including 
by-default by subprocess() processes.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue46454>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to