[Python-checkins] r69620 - in python/trunk/Lib: subprocess.py test/test_subprocess.py

georg.brandl python-checkins at python.org
Sat Feb 14 18:01:37 CET 2009


Author: georg.brandl
Date: Sat Feb 14 18:01:36 2009
New Revision: 69620

Log:
#5179: don't leak PIPE fds when child execution fails.

Modified:
   python/trunk/Lib/subprocess.py
   python/trunk/Lib/test/test_subprocess.py

Modified: python/trunk/Lib/subprocess.py
==============================================================================
--- python/trunk/Lib/subprocess.py	(original)
+++ python/trunk/Lib/subprocess.py	Sat Feb 14 18:01:36 2009
@@ -1151,6 +1151,9 @@
             if data != "":
                 os.waitpid(self.pid, 0)
                 child_exception = pickle.loads(data)
+                for fd in (p2cwrite, c2pread, errread):
+                    if fd is not None:
+                        os.close(fd)
                 raise child_exception
 
 

Modified: python/trunk/Lib/test/test_subprocess.py
==============================================================================
--- python/trunk/Lib/test/test_subprocess.py	(original)
+++ python/trunk/Lib/test/test_subprocess.py	Sat Feb 14 18:01:36 2009
@@ -520,6 +520,22 @@
         else:
             self.fail("Expected TypeError")
 
+    def test_leaking_fds_on_error(self):
+        # see bug #5179: Popen leaks file descriptors to PIPEs if
+        # the child fails to execute; this will eventually exhaust
+        # the maximum number of open fds. 1024 seems a very common
+        # value for that limit, but Windows has 2048, so we loop
+        # 1024 times (each call leaked two fds).
+        for i in range(1024):
+            try:
+                subprocess.Popen(['nonexisting_i_hope'],
+                                 stdout=subprocess.PIPE,
+                                 stderr=subprocess.PIPE)
+            # Windows raises IOError
+            except (IOError, OSError), err:
+                if err.errno != 2:  # ignore "no such file"
+                    raise
+
     #
     # POSIX tests
     #


More information about the Python-checkins mailing list