From 3e978e01e10deb4d808d2e9bb7e8b8a29d045bf1 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sat, 10 Nov 2012 22:32:22 -0800 Subject: Fixes issue #16327: The subprocess module no longer leaks file descriptors used for stdin/stdout/stderr pipes to the child when fork() fails. --- Lib/subprocess.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'Lib/subprocess.py') diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 35a98c8969..7cd5a571f3 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -744,13 +744,27 @@ class Popen(object): errread, errwrite, restore_signals, start_new_session) except: - # Cleanup if the child failed starting - for f in filter(None, [self.stdin, self.stdout, self.stderr]): + # Cleanup if the child failed starting. + for f in filter(None, (self.stdin, self.stdout, self.stderr)): try: f.close() except EnvironmentError: - # Ignore EBADF or other errors + pass # Ignore EBADF or other errors. + + # Make sure the child pipes are closed as well. + to_close = [] + if stdin == PIPE: + to_close.append(p2cread) + if stdout == PIPE: + to_close.append(c2pwrite) + if stderr == PIPE: + to_close.append(errwrite) + for fd in to_close: + try: + os.close(fd) + except EnvironmentError: pass + raise -- cgit v1.2.1