diff options
author | Gregory P. Smith <greg@krypto.org> | 2012-11-11 06:33:23 (GMT) |
---|---|---|
committer | Gregory P. Smith <greg@krypto.org> | 2012-11-11 06:33:23 (GMT) |
commit | 561cbc4e7bdf457bb64acf8ef7e5358795816c88 (patch) | |
tree | 6a9a8a16fdc9b5f69f1698226abff747cf47385a /Lib/subprocess.py | |
parent | a450c5e69ba17bb47e8b3434969218c97cdf1a07 (diff) | |
parent | 3d8e776cd932ff79fe181984cacce9ff8bcf3e4c (diff) | |
download | cpython-561cbc4e7bdf457bb64acf8ef7e5358795816c88.zip cpython-561cbc4e7bdf457bb64acf8ef7e5358795816c88.tar.gz cpython-561cbc4e7bdf457bb64acf8ef7e5358795816c88.tar.bz2 |
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 296613a..02c722b 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 |