ensure write_fd.close() isn't called when sys.std* cannot be redirected

This commit is contained in:
Harmen Stoppels 2024-10-24 11:29:05 +02:00 committed by Todd Gamblin
parent ae306b73c3
commit 7d86670826

View File

@ -503,7 +503,7 @@ def __enter__(self):
self._saved_debug = tty._debug
# Pipe for redirecting output to logger
read_fd, write_fd = multiprocessing.Pipe(duplex=False)
read_fd, self.write_fd = multiprocessing.Pipe(duplex=False)
# Pipe for communication back from the daemon
# Currently only used to save echo value between uses
@ -526,7 +526,7 @@ def __enter__(self):
args=(
input_fd,
read_fd,
write_fd,
self.write_fd,
self.echo,
self.log_file,
child_pipe,
@ -557,9 +557,9 @@ def __enter__(self):
self._saved_stderr = os.dup(sys.stderr.fileno())
# redirect to the pipe we created above
os.dup2(write_fd.fileno(), sys.stdout.fileno())
os.dup2(write_fd.fileno(), sys.stderr.fileno())
write_fd.close()
os.dup2(self.write_fd.fileno(), sys.stdout.fileno())
os.dup2(self.write_fd.fileno(), sys.stderr.fileno())
self.write_fd.close()
else:
# Handle I/O the Python way. This won't redirect lower-level
@ -572,7 +572,7 @@ def __enter__(self):
self._saved_stderr = sys.stderr
# create a file object for the pipe; redirect to it.
pipe_fd_out = os.fdopen(write_fd.fileno(), "w", closefd=False)
pipe_fd_out = os.fdopen(self.write_fd.fileno(), "w", closefd=False)
sys.stdout = pipe_fd_out
sys.stderr = pipe_fd_out
@ -608,6 +608,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
else:
sys.stdout = self._saved_stdout
sys.stderr = self._saved_stderr
self.write_fd.close()
# print log contents in parent if needed.
if self.log_file.write_in_parent: