mirror of
https://github.com/python/cpython.git
synced 2025-08-27 20:25:18 +00:00
Fixes issue #16327: The subprocess module no longer leaks file descriptors
used for stdin/stdout/stderr pipes to the child when fork() fails.
This commit is contained in:
parent
6f62b58134
commit
3d8e776cd9
3 changed files with 53 additions and 3 deletions
|
@ -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
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue