You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
subprocess closes redirected fds even if they are in pass_fds #76451
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/gpshead'closed_at=<Date2018-09-11.22:52:35.695>created_at=<Date2017-12-10.17:14:06.586>labels= ['3.7', '3.8', 'type-bug', 'library']
title='subprocess closes redirected fds even if they are in pass_fds'updated_at=<Date2018-09-11.22:52:35.694>user='https://github.com/izbyshev'
$ python3 test.py
Hello stdout
Traceback (most recent call last):
File "<string>", line 1, in <module>OSError: [Errno 9] Bad file descriptor
I see two issues here:
The fact that file descriptors specified for redirection are closed (even with close_fds=False) unless in range [0, 2] is not documented and not tested. If I change the corresponding code in _posixsubprocess.c to close them only if they are ends of pipes created by subprocess itself, all tests still pass. Also, shells behave differently: e.g., in command 'echo 3>&1' fd 3 is duplicated but not closed in the child, so subprocess behavior may be not what people expect.
pass_fds interaction with (1) should be fixed and documented. We may either raise ValueError if pass_fds contains a redirected fd or don't close such a redirected fd.
The fact that file descriptors specified for redirection are closed (even with close_fds=False) unless in range [0, 2] is not documented and not tested.
That sounds like a bug to me, so we should fix it if decently possible.
pass_fds interaction with (1) should be fixed and documented. We may either raise ValueError if pass_fds contains a redirected fd or don't close such a redirected fd.
Same here, it seems like a bug which deserves fixing.
Do you want to submit a PR for this?
izbyshev commented on Dec 10, 2017
izbyshevmannequin
MannequinAuthor
More actions
Regarding fixing (1), I'm worrying about backward compatibility a bit. Some people who discovered that behavior might rely on such "move" semantics and expect that the redirected descriptor is not leaked into the child. OTOH, since descriptors are non-inheritable by default since 3.4, it might be less of an issue now. What do you think?
Otherwise, yes, I'd like to fix this, but fixing is a bit trickier than it may seem because sometimes descriptors are duplicated in _posixsubprocess.c (in case of low fds), so we need to track the ownership properly. I've already performed some work and discovered another issue: in a corner case involving closed standard fds, pipes created by subprocess may leak into the child and/or an incorrect redirection may occur. Since I can't completely fix this issue without affecting a discovered one, I think I'll do the opposite: file a new issue, fix it, and then go back to this one.
When stdout=fd is passed, that results in c2pwrite=fd. The stdin/stdout/stderr pipe fds are closed when > 2 without checking to see if they are listed in pass_fds.
Actually I've started to work on this with bpo-32844, but got no feedback. This issue may of course be fixed independently, but the problems with descriptor ownership for fds <= 2 will remain unless all ownership problems are fixed at once.
New changeset 9c4a63f by Gregory P. Smith (Miss Islington (bot)) in branch '3.7': bpo-32270: Don't close stdin/out/err in pass_fds (GH-6242) (GH-9148) 9c4a63f
New changeset 2173bb8 by Gregory P. Smith (Miss Islington (bot)) in branch '3.6': bpo-32270: Don't close stdin/out/err in pass_fds (GH-6242) (GH-9149) 2173bb8
Activity
izbyshev commentedon Dec 10, 2017
Demonstration:
I see two issues here:
The fact that file descriptors specified for redirection are closed (even with close_fds=False) unless in range [0, 2] is not documented and not tested. If I change the corresponding code in _posixsubprocess.c to close them only if they are ends of pipes created by subprocess itself, all tests still pass. Also, shells behave differently: e.g., in command 'echo 3>&1' fd 3 is duplicated but not closed in the child, so subprocess behavior may be not what people expect.
pass_fds interaction with (1) should be fixed and documented. We may either raise ValueError if pass_fds contains a redirected fd or don't close such a redirected fd.
Please provide your thoughts.
pitrou commentedon Dec 10, 2017
Thanks for the report and the investigation!
That sounds like a bug to me, so we should fix it if decently possible.
Same here, it seems like a bug which deserves fixing.
Do you want to submit a PR for this?
izbyshev commentedon Dec 10, 2017
Regarding fixing (1), I'm worrying about backward compatibility a bit. Some people who discovered that behavior might rely on such "move" semantics and expect that the redirected descriptor is not leaked into the child. OTOH, since descriptors are non-inheritable by default since 3.4, it might be less of an issue now. What do you think?
Otherwise, yes, I'd like to fix this, but fixing is a bit trickier than it may seem because sometimes descriptors are duplicated in _posixsubprocess.c (in case of low fds), so we need to track the ownership properly. I've already performed some work and discovered another issue: in a corner case involving closed standard fds, pipes created by subprocess may leak into the child and/or an incorrect redirection may occur. Since I can't completely fix this issue without affecting a discovered one, I think I'll do the opposite: file a new issue, fix it, and then go back to this one.
pitrou commentedon Dec 12, 2017
Well, people shouldn't rely on bugs. Otherwise we would never be able to fix bugs, lest someone relies on it.
gpshead commentedon Mar 25, 2018
This bug stems from:
https://github.com/python/cpython/blob/master/Modules/_posixsubprocess.c#L454
When stdout=fd is passed, that results in c2pwrite=fd. The stdin/stdout/stderr pipe fds are closed when > 2 without checking to see if they are listed in pass_fds.
https://github.com/python/cpython/blob/master/Lib/subprocess.py#L1306 maps the following:
stdin -> p2cread
stdout -> c2pwrite
stderr -> errwrite
izbyshev commentedon Mar 25, 2018
Actually I've started to work on this with bpo-32844, but got no feedback. This issue may of course be fixed independently, but the problems with descriptor ownership for fds <= 2 will remain unless all ownership problems are fixed at once.
izbyshev commentedon Apr 11, 2018
An alternative fix is here: izbyshev@b89b52f
Feel free to use the test if you don't like the approach :)
gpshead commentedon Sep 11, 2018
New changeset ce34410 by Gregory P. Smith in branch 'master':
bpo-32270: Don't close stdin/out/err in pass_fds (GH-6242)
ce34410
gpshead commentedon Sep 11, 2018
New changeset 9c4a63f by Gregory P. Smith (Miss Islington (bot)) in branch '3.7':
bpo-32270: Don't close stdin/out/err in pass_fds (GH-6242) (GH-9148)
9c4a63f
gpshead commentedon Sep 11, 2018
New changeset 2173bb8 by Gregory P. Smith (Miss Islington (bot)) in branch '3.6':
bpo-32270: Don't close stdin/out/err in pass_fds (GH-6242) (GH-9149)
2173bb8
izbyshev commentedon Sep 11, 2018
Thank you, Gregory!