This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author javabrett
Recipients Geass-LL, JelleZijlstra, eryksun, javabrett, shihai1991, sxt1001
Date 2022-01-25.10:44:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1643107457.8.0.677276879084.issue46454@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2022-01-25 10:44:17javabrettsetrecipients: + javabrett, eryksun, JelleZijlstra, shihai1991, sxt1001, Geass-LL
2022-01-25 10:44:17javabrettsetmessageid: <1643107457.8.0.677276879084.issue46454@roundup.psfhosted.org>
2022-01-25 10:44:17javabrettlinkissue46454 messages
2022-01-25 10:44:17javabrettcreate