diff options
author | Georg Brandl <georg@python.org> | 2009-02-14 17:01:36 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2009-02-14 17:01:36 (GMT) |
commit | f3715d2fc72e191c58c76e1eba92f6bc0a5a0e9d (patch) | |
tree | 50f867a242a1748dac5af195a41c32d2f0e6a612 /Lib | |
parent | 95777bb019b2b42cef8caaf80723f3bb0409970c (diff) | |
download | cpython-f3715d2fc72e191c58c76e1eba92f6bc0a5a0e9d.zip cpython-f3715d2fc72e191c58c76e1eba92f6bc0a5a0e9d.tar.gz cpython-f3715d2fc72e191c58c76e1eba92f6bc0a5a0e9d.tar.bz2 |
#5179: don't leak PIPE fds when child execution fails.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/subprocess.py | 3 | ||||
-rw-r--r-- | Lib/test/test_subprocess.py | 16 |
2 files changed, 19 insertions, 0 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 309f5d4..d91fbee 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -1151,6 +1151,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 diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index ae8e888..0ddac3d 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -520,6 +520,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 # |