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 Thomas.Claveirole
Recipients Thomas.Claveirole
Date 2010-09-20.17:59:28
SpamBayes Score 6.24821e-08
Marked as misclassified No
Message-id <1285005576.46.0.387895977213.issue9905@psf.upfronthosting.co.za>
In-reply-to
Content
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,
History
Date User Action Args
2010-09-20 17:59:36Thomas.Claveirolesetrecipients: + Thomas.Claveirole
2010-09-20 17:59:36Thomas.Claveirolesetmessageid: <1285005576.46.0.387895977213.issue9905@psf.upfronthosting.co.za>
2010-09-20 17:59:33Thomas.Claveirolelinkissue9905 messages
2010-09-20 17:59:28Thomas.Claveirolecreate