ensure write_fd.close() isn't called when sys.std* cannot be redirected
This commit is contained in:
		
				
					committed by
					
						
						Todd Gamblin
					
				
			
			
				
	
			
			
			
						parent
						
							ae306b73c3
						
					
				
				
					commit
					7d86670826
				
			@@ -503,7 +503,7 @@ def __enter__(self):
 | 
				
			|||||||
        self._saved_debug = tty._debug
 | 
					        self._saved_debug = tty._debug
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Pipe for redirecting output to logger
 | 
					        # 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
 | 
					        # Pipe for communication back from the daemon
 | 
				
			||||||
        # Currently only used to save echo value between uses
 | 
					        # Currently only used to save echo value between uses
 | 
				
			||||||
@@ -526,7 +526,7 @@ def __enter__(self):
 | 
				
			|||||||
                    args=(
 | 
					                    args=(
 | 
				
			||||||
                        input_fd,
 | 
					                        input_fd,
 | 
				
			||||||
                        read_fd,
 | 
					                        read_fd,
 | 
				
			||||||
                        write_fd,
 | 
					                        self.write_fd,
 | 
				
			||||||
                        self.echo,
 | 
					                        self.echo,
 | 
				
			||||||
                        self.log_file,
 | 
					                        self.log_file,
 | 
				
			||||||
                        child_pipe,
 | 
					                        child_pipe,
 | 
				
			||||||
@@ -557,9 +557,9 @@ def __enter__(self):
 | 
				
			|||||||
            self._saved_stderr = os.dup(sys.stderr.fileno())
 | 
					            self._saved_stderr = os.dup(sys.stderr.fileno())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # redirect to the pipe we created above
 | 
					            # redirect to the pipe we created above
 | 
				
			||||||
            os.dup2(write_fd.fileno(), sys.stdout.fileno())
 | 
					            os.dup2(self.write_fd.fileno(), sys.stdout.fileno())
 | 
				
			||||||
            os.dup2(write_fd.fileno(), sys.stderr.fileno())
 | 
					            os.dup2(self.write_fd.fileno(), sys.stderr.fileno())
 | 
				
			||||||
            write_fd.close()
 | 
					            self.write_fd.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            # Handle I/O the Python way. This won't redirect lower-level
 | 
					            # Handle I/O the Python way. This won't redirect lower-level
 | 
				
			||||||
@@ -572,7 +572,7 @@ def __enter__(self):
 | 
				
			|||||||
            self._saved_stderr = sys.stderr
 | 
					            self._saved_stderr = sys.stderr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # create a file object for the pipe; redirect to it.
 | 
					            # 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.stdout = pipe_fd_out
 | 
				
			||||||
            sys.stderr = pipe_fd_out
 | 
					            sys.stderr = pipe_fd_out
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -608,6 +608,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
 | 
				
			|||||||
        else:
 | 
					        else:
 | 
				
			||||||
            sys.stdout = self._saved_stdout
 | 
					            sys.stdout = self._saved_stdout
 | 
				
			||||||
            sys.stderr = self._saved_stderr
 | 
					            sys.stderr = self._saved_stderr
 | 
				
			||||||
 | 
					            self.write_fd.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # print log contents in parent if needed.
 | 
					        # print log contents in parent if needed.
 | 
				
			||||||
        if self.log_file.write_in_parent:
 | 
					        if self.log_file.write_in_parent:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user