With the previous implementation, run_command could deadlock.
While stderr stays empty, p.stderr.readline() will block. In the meanwhile, stdout output will accumulate in the pipe buffer. Once this buffer becomes full (the size is io.DEFAULT_BUFFER_SIZE, 8192 on my machine), the program run by subprocess.Popen will block when trying to output to stdout, resulting in a deadlock.
As all this logic was doing was buffering the process output in Python and writing it to a file, we can directly pass the file descriptor to subprocess.Popen and let Popen.communicate handle this directly.
With the previous implementation,
run_command
could deadlock.While
stderr
stays empty,p.stderr.readline()
will block. In the meanwhile,stdout
output will accumulate in the pipe buffer. Once this buffer becomes full (the size isio.DEFAULT_BUFFER_SIZE
,8192
on my machine), the program run bysubprocess.Popen
will block when trying to output tostdout
, resulting in a deadlock.As all this logic was doing was buffering the process output in Python and writing it to a file, we can directly pass the file descriptor to
subprocess.Popen
and letPopen.communicate
handle this directly.