diff options
author | Gregory P. Smith <greg@krypto.org> | 2012-11-10 22:34:18 -0800 |
---|---|---|
committer | Gregory P. Smith <greg@krypto.org> | 2012-11-10 22:34:18 -0800 |
commit | 099717b73b0b1d0c9dd4aacb91f6932235b799d0 (patch) | |
tree | 7e9f771cd612fb18bd6ad70dc288dda69e057bca /Lib/subprocess.py | |
parent | dd0edae1ccef6e0c4ee3338f094ad9d162650360 (diff) | |
parent | 561cbc4e7bdf457bb64acf8ef7e5358795816c88 (diff) | |
download | cpython-git-099717b73b0b1d0c9dd4aacb91f6932235b799d0.tar.gz |
Fixes issue #16327: The subprocess module no longer leaks file descriptors
used for stdin/stdout/stderr pipes to the child when fork() fails.
Diffstat (limited to 'Lib/subprocess.py')
-rw-r--r-- | Lib/subprocess.py | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 296613ac0b..02c722b013 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -817,13 +817,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 |