mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Prevent a possible double close of parent pipe fds when the subprocess
exec runs into an error. Prevent a regular multi-close of the /dev/null fd when any of stdin, stdout and stderr was set to DEVNULL.
This commit is contained in:
commit
22ba31a3af
2 changed files with 29 additions and 18 deletions
|
@ -828,6 +828,7 @@ class Popen(object):
|
||||||
if universal_newlines:
|
if universal_newlines:
|
||||||
self.stderr = io.TextIOWrapper(self.stderr)
|
self.stderr = io.TextIOWrapper(self.stderr)
|
||||||
|
|
||||||
|
self._closed_child_pipe_fds = False
|
||||||
try:
|
try:
|
||||||
self._execute_child(args, executable, preexec_fn, close_fds,
|
self._execute_child(args, executable, preexec_fn, close_fds,
|
||||||
pass_fds, cwd, env,
|
pass_fds, cwd, env,
|
||||||
|
@ -844,7 +845,7 @@ class Popen(object):
|
||||||
except OSError:
|
except OSError:
|
||||||
pass # Ignore EBADF or other errors.
|
pass # Ignore EBADF or other errors.
|
||||||
|
|
||||||
# Make sure the child pipes are closed as well.
|
if not self._closed_child_pipe_fds:
|
||||||
to_close = []
|
to_close = []
|
||||||
if stdin == PIPE:
|
if stdin == PIPE:
|
||||||
to_close.append(p2cread)
|
to_close.append(p2cread)
|
||||||
|
@ -852,6 +853,8 @@ class Popen(object):
|
||||||
to_close.append(c2pwrite)
|
to_close.append(c2pwrite)
|
||||||
if stderr == PIPE:
|
if stderr == PIPE:
|
||||||
to_close.append(errwrite)
|
to_close.append(errwrite)
|
||||||
|
if hasattr(self, '_devnull'):
|
||||||
|
to_close.append(self._devnull)
|
||||||
for fd in to_close:
|
for fd in to_close:
|
||||||
try:
|
try:
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
|
@ -1363,14 +1366,18 @@ class Popen(object):
|
||||||
# be sure the FD is closed no matter what
|
# be sure the FD is closed no matter what
|
||||||
os.close(errpipe_write)
|
os.close(errpipe_write)
|
||||||
|
|
||||||
if p2cread != -1 and p2cwrite != -1:
|
# self._devnull is not always defined.
|
||||||
|
devnull_fd = getattr(self, '_devnull', None)
|
||||||
|
if p2cread != -1 and p2cwrite != -1 and p2cread != devnull_fd:
|
||||||
os.close(p2cread)
|
os.close(p2cread)
|
||||||
if c2pwrite != -1 and c2pread != -1:
|
if c2pwrite != -1 and c2pread != -1 and c2pwrite != devnull_fd:
|
||||||
os.close(c2pwrite)
|
os.close(c2pwrite)
|
||||||
if errwrite != -1 and errread != -1:
|
if errwrite != -1 and errread != -1 and errwrite != devnull_fd:
|
||||||
os.close(errwrite)
|
os.close(errwrite)
|
||||||
if hasattr(self, '_devnull'):
|
if devnull_fd is not None:
|
||||||
os.close(self._devnull)
|
os.close(devnull_fd)
|
||||||
|
# Prevent a double close of these fds from __init__ on error.
|
||||||
|
self._closed_child_pipe_fds = True
|
||||||
|
|
||||||
# Wait for exec to fail or succeed; possibly raising an
|
# Wait for exec to fail or succeed; possibly raising an
|
||||||
# exception (limited in size)
|
# exception (limited in size)
|
||||||
|
|
|
@ -123,6 +123,10 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- subprocess: Prevent a possible double close of parent pipe fds when the
|
||||||
|
subprocess exec runs into an error. Prevent a regular multi-close of the
|
||||||
|
/dev/null fd when any of stdin, stdout and stderr was set to DEVNULL.
|
||||||
|
|
||||||
- Issue #18194: Introduce importlib.util.cache_from_source() and
|
- Issue #18194: Introduce importlib.util.cache_from_source() and
|
||||||
source_from_cache() while documenting the equivalent functions in imp as
|
source_from_cache() while documenting the equivalent functions in imp as
|
||||||
deprecated.
|
deprecated.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue