[New-bugs-announce] [issue9905] subprocess.Popen fails with stdout=PIPE, stderr=PIPE if standard descriptors (0, 1, 2) are closed.

Thomas Claveirole report at bugs.python.org
Mon Sep 20 19:59:36 CEST 2010


New submission from Thomas Claveirole <thomas.claveirole at gmail.com>:

Hello,

Here is a code that exhibits an invalid behavior (Python 2.6.6):
---8<---
import subprocess, os

os.close(0) # Works correctly if any of these two are commented out.
os.close(2)

print subprocess.Popen('echo foo>&2', shell=True,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE).communicate()
--->8---

When run, the output is:
('', '')

While it should be:
('', 'foo\n')

When analyzing the code with strace the problem gets clearer:
$ strace -f -e pipe,fork,dup2,close ./Popen-bug.py
[...]
5085  pipe([0, 2])                      = 0  # Creates the pipes.
5085  pipe([3, 4])                      = 0
5085  pipe([5, 6])                      = 0
[...] # In this skipped part Popen() closes useless pipe endpoints.
5086  dup2(2, 1)                        = 1 # stdout setup.
5086  dup2(4, 2)                        = 2 # stderr setup.
5086  close(2)                          = 0
[...]

The last "close(2)" is the error: apparently Popen() tries to close the remaining pipe endpoints (as should theoretically be done) but fails to see that the endpoint created by pipe([0, 2]) has already been closed during the previous dup2(4, 2) and that the file descriptor 2 is now the standard error.  Therefore, Popen incorrectly closes the standard error.

To fix that, Popen should check, before closing the remaining pipe endpoints, that these endpoints are not the one that just get closed by the two previous dup2s.

Best regards,

----------
components: Library (Lib)
messages: 116957
nosy: Thomas.Claveirole
priority: normal
severity: normal
status: open
title: subprocess.Popen fails with stdout=PIPE, stderr=PIPE if standard descriptors (0, 1, 2) are closed.
type: behavior
versions: Python 2.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9905>
_______________________________________


More information about the New-bugs-announce mailing list