summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGregory P. Smith <greg@mad-scientist.com>2008-05-26 20:22:14 (GMT)
committerGregory P. Smith <greg@mad-scientist.com>2008-05-26 20:22:14 (GMT)
commit4036fd4b752f744d455f592a83b4a1e36cd77a86 (patch)
treeb8edac46244c6d057bebeb334bbb3db127ecdccf
parent3aa84a7f28659cfbed312e352775b32d2a58005b (diff)
downloadcpython-4036fd4b752f744d455f592a83b4a1e36cd77a86.zip
cpython-4036fd4b752f744d455f592a83b4a1e36cd77a86.tar.gz
cpython-4036fd4b752f744d455f592a83b4a1e36cd77a86.tar.bz2
Fixes issue2791: subprocess.Popen.communicate leaked a file descripton until
the last reference to the Popen instance was dropped. Adding explicit close() calls fixes it. Candidate for backport to release25-maint.
-rw-r--r--Lib/subprocess.py2
-rw-r--r--Lib/test/test_subprocess.py24
2 files changed, 21 insertions, 5 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index dc639c9..6065594 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -661,8 +661,10 @@ class Popen(object):
self.stdin.close()
elif self.stdout:
stdout = self.stdout.read()
+ self.stdout.close()
elif self.stderr:
stderr = self.stderr.read()
+ self.stderr.close()
self.wait()
return (stdout, stderr)
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
index f31ce75..e7ba26f 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -287,14 +287,12 @@ class ProcessTestCase(unittest.TestCase):
stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
self.assertEqual(stdout, None)
- # When running with a pydebug build, the # of references is outputted
- # to stderr, so just check if stderr at least started with "pinapple"
- self.assert_(stderr.startswith("pineapple"))
+ self.assertEqual(remove_stderr_debug_decorations(stderr), "pineapple")
def test_communicate(self):
p = subprocess.Popen([sys.executable, "-c",
- 'import sys,os;' \
- 'sys.stderr.write("pineapple");' \
+ 'import sys,os;'
+ 'sys.stderr.write("pineapple");'
'sys.stdout.write(sys.stdin.read())'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
@@ -304,6 +302,22 @@ class ProcessTestCase(unittest.TestCase):
self.assertEqual(remove_stderr_debug_decorations(stderr),
"pineapple")
+ # This test is Linux specific for simplicity to at least have
+ # some coverage. It is not a platform specific bug.
+ if os.path.isdir('/proc/%d/fd' % os.getpid()):
+ # Test for the fd leak reported in http://bugs.python.org/issue2791.
+ def test_communicate_pipe_fd_leak(self):
+ fd_directory = '/proc/%d/fd' % os.getpid()
+ num_fds_before_popen = len(os.listdir(fd_directory))
+ p = subprocess.Popen([sys.executable, '-c', 'print()'],
+ stdout=subprocess.PIPE)
+ p.communicate()
+ num_fds_after_communicate = len(os.listdir(fd_directory))
+ del p
+ num_fds_after_destruction = len(os.listdir(fd_directory))
+ self.assertEqual(num_fds_before_popen, num_fds_after_destruction)
+ self.assertEqual(num_fds_before_popen, num_fds_after_communicate)
+
def test_communicate_returns(self):
# communicate() should return None if no redirection is active
p = subprocess.Popen([sys.executable, "-c",