[Python-checkins] r63881 - in python/branches/release25-maint: Lib/subprocess.py Lib/test/test_subprocess.py Misc/NEWS

gregory.p.smith python-checkins at python.org
Mon Jun 2 01:44:47 CEST 2008


Author: gregory.p.smith
Date: Mon Jun  2 01:44:46 2008
New Revision: 63881

Log:
Backport r62724 from trunk.  Fixes issue 2791.  subprocess.Popen.communicate
now closes its stdout and stderr fds as soon as it is finished with them.


Modified:
   python/branches/release25-maint/Lib/subprocess.py
   python/branches/release25-maint/Lib/test/test_subprocess.py
   python/branches/release25-maint/Misc/NEWS

Modified: python/branches/release25-maint/Lib/subprocess.py
==============================================================================
--- python/branches/release25-maint/Lib/subprocess.py	(original)
+++ python/branches/release25-maint/Lib/subprocess.py	Mon Jun  2 01:44:46 2008
@@ -660,8 +660,10 @@
                 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)
 

Modified: python/branches/release25-maint/Lib/test/test_subprocess.py
==============================================================================
--- python/branches/release25-maint/Lib/test/test_subprocess.py	(original)
+++ python/branches/release25-maint/Lib/test/test_subprocess.py	Mon Jun  2 01:44:46 2008
@@ -304,6 +304,22 @@
         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",

Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Mon Jun  2 01:44:46 2008
@@ -82,6 +82,10 @@
 - Bug #1433694: minidom's .normalize() failed to set .nextSibling for
   last child element.
 
+- Issue #2791: subprocess.Popen.communicate explicitly closes its
+  stdout and stderr fds rather than leaving them open until the
+  instance is destroyed.
+
 
 Extension Modules
 -----------------


More information about the Python-checkins mailing list