]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 69620 via svnmerge from
authorGeorg Brandl <georg@python.org>
Sat, 14 Feb 2009 17:04:26 +0000 (17:04 +0000)
committerGeorg Brandl <georg@python.org>
Sat, 14 Feb 2009 17:04:26 +0000 (17:04 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r69620 | georg.brandl | 2009-02-14 18:01:36 +0100 (Sa, 14 Feb 2009) | 1 line

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

Lib/subprocess.py
Lib/test/test_subprocess.py

index 935827ace901893e7e793f2401d0255e29f26a44..29a3d59ff376fb296b169e3ceab2bbac30219643 100644 (file)
@@ -1103,6 +1103,9 @@ class Popen(object):
             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
 
 
index e7ba26fae1c3c2ba5257f1ce806fb4a6ab517730..99f31b984db32aac0b0b562aeacc1eb14e2237a9 100644 (file)
@@ -486,6 +486,22 @@ class ProcessTestCase(unittest.TestCase):
         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
     #