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.

classification
Title: subprocess closes standard file descriptors when it should not
Type: behavior Stage: patch review
Components: IO Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: subprocess.Popen fails with stdout=PIPE, stderr=PIPE if standard descriptors (0, 1, 2) are closed.
View: 9905
Assigned To: Nosy List: astrand, georg.brandl, kr
Priority: normal Keywords: needs review, patch

Created on 2010-06-24 20:12 by kr, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
fd-close.patch kr, 2010-06-24 20:12 Patch against Python 2.6.5 subprocess.py should not close standard fds
Messages (3)
msg108548 - (view) Author: Keith Rarick (kr) Date: 2010-06-24 20:12
Transcript to reproduce in Python 2.6.5:

>>> import subprocess, sys
>>> subprocess.call(('echo', 'foo'), stderr=sys.stdout)
echo: write: Bad file descriptor
1
>>> 

Expected behavior:

>>> import subprocess, sys
>>> subprocess.call(('echo', 'foo'), stderr=sys.stdout)
foo
0
>>> 

This happens because we've asked the child's stderr to be redirected, but not its stdout. So in _execute_child, errwrite is 1 while c2pwrite is None. So fd 1 (errwrite) correctly gets duped to 2. But then, since errwrite is not None and it's not in (p2cread, c2pwrite, 2), the child closes fd 1.

The equivalent thing happens if you supply stdout=sys.stderr and the child attempts to write to its stderr.

I've attached a patch to fix this. It simply adds 2 and 2 to the list of fds not to close for c2pwrite and errwrite, respectively.

This patch is against the 2.6.5 release.

There is also a workaround, in case anyone else is affected by this bug before a fix has been released:

>>> import os, subprocess, sys
>>> subprocess.call(('echo', 'foo'), stderr=os.dup(sys.stdout.fileno()))
foo
0
>>> 

(There could be a similar bug relating to the child's stdin, but I haven't investigated that.)
msg108549 - (view) Author: Keith Rarick (kr) Date: 2010-06-24 20:17
There was a typo in my description of the patch. It should read:

I've attached a patch to fix this. It simply adds 2 and *1* to the list of fds not to close for c2pwrite and errwrite, respectively.
msg125059 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2011-01-02 14:50
#9905 has a similar patch and adds tests as well.
History
Date User Action Args
2022-04-11 14:57:02adminsetgithub: 53320
2011-01-02 14:50:54georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg125059

superseder: subprocess.Popen fails with stdout=PIPE, stderr=PIPE if standard descriptors (0, 1, 2) are closed.
resolution: duplicate
2010-12-22 09:05:01eric.araujosettitle: [includes patch] subprocess module closes standard file descriptors when it should not -> subprocess closes standard file descriptors when it should not
nosy: + astrand

versions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.6
keywords: + needs review
stage: patch review
2010-06-24 20:17:02krsetmessages: + msg108549
2010-06-24 20:12:14krcreate