classification
Title: subprocess may incorrectly redirect a low fd to stderr if another low fd is closed
Type: behavior Stage: resolved
Components: Extension Modules, Library (Lib) Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: gregory.p.smith, izbyshev, miss-islington
Priority: normal Keywords: patch

Created on 2018-02-14 21:10 by izbyshev, last changed 2018-03-26 21:03 by izbyshev. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 5689 merged izbyshev, 2018-02-14 21:16
PR 6262 merged miss-islington, 2018-03-26 19:49
PR 6263 merged miss-islington, 2018-03-26 19:50
Messages (5)
msg312181 - (view) Author: Alexey Izbyshev (izbyshev) * (Python triager) Date: 2018-02-14 21:10
When redirecting, subprocess attempts to achieve the following state: each fd to be redirected to is less than or equal to the fd it is redirected from, which is necessary because redirection occurs in the ascending order of destination descriptors. It fails to do so if a low fd (< 2) is redirected to stderr and another low fd is closed, which may lead to an incorrect redirection, for example:

$ cat test.py
import os
import subprocess
import sys

os.close(0)

subprocess.call([sys.executable, '-c',
                 'import sys; print("Hello", file=sys.stderr)'],
                stdin=2,
                stderr=1)

$ python3 test.py 2>/dev/null
$ python3 test.py >/dev/null
Hello

Expected behavior:
$ python3 test.py >/dev/null
$ python3 test.py 2>/dev/null
Hello
msg312182 - (view) Author: Alexey Izbyshev (izbyshev) * (Python triager) Date: 2018-02-14 21:22
Note that the PR doesn't attempt to fix leaking of low dup'ed fds to the child. I'll file a separate report for that in a while.
msg314478 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2018-03-26 19:49
New changeset 0e7144b064a19493a146af94175a087b3888c37b by Gregory P. Smith (Alexey Izbyshev) in branch 'master':
bpo-32844: Fix a subprocess misredirection of a low fd (GH5689)
https://github.com/python/cpython/commit/0e7144b064a19493a146af94175a087b3888c37b
msg314479 - (view) Author: miss-islington (miss-islington) Date: 2018-03-26 20:14
New changeset 05455637f3ba9bacd459700f4feab783e5967d69 by Miss Islington (bot) in branch '3.7':
bpo-32844: Fix a subprocess misredirection of a low fd (GH5689)
https://github.com/python/cpython/commit/05455637f3ba9bacd459700f4feab783e5967d69
msg314481 - (view) Author: miss-islington (miss-islington) Date: 2018-03-26 20:43
New changeset 57db13e582ad269d6e067fe934122207cc992739 by Miss Islington (bot) in branch '3.6':
bpo-32844: Fix a subprocess misredirection of a low fd (GH5689)
https://github.com/python/cpython/commit/57db13e582ad269d6e067fe934122207cc992739
History
Date User Action Args
2018-03-26 21:03:05izbyshevsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-03-26 20:43:48miss-islingtonsetmessages: + msg314481
2018-03-26 20:14:12miss-islingtonsetnosy: + miss-islington
messages: + msg314479
2018-03-26 19:50:46miss-islingtonsetpull_requests: + pull_request5989
2018-03-26 19:49:52miss-islingtonsetpull_requests: + pull_request5988
2018-03-26 19:49:38gregory.p.smithsetmessages: + msg314478
2018-02-14 21:22:06izbyshevsetmessages: + msg312182
2018-02-14 21:16:07izbyshevsetkeywords: + patch
stage: patch review
pull_requests: + pull_request5482
2018-02-14 21:10:48izbyshevcreate