Skip to content

Commit d8439a5

Browse files
authored
Use communicate() with Python subprocess (#795)
Using `poll()` on a Popen object will return `None` if the child process has not terminated and that can cause trouble with the `get_command_output()` function in the Docker building script. The function assumes that `poll()` always returns the exit code of the child process and compares it to 0 to check if the child process executed successfully. If the child process has not terminated by the time `poll()` is called, then the comparison will always be false and the child process will appear to have failed.
1 parent 2592008 commit d8439a5

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

scripts/docker/build_docker.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -672,10 +672,10 @@ def get_command_output(
672672
Execute shell command. Raise exception if unsuccessful, otherwise return string with output
673673
"""
674674
sub_p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
675-
with sub_p.stdout as pipe_in, sub_p.stderr as pipe_err:
676-
output = pipe_in.read().decode(encoding)
677-
stderr = pipe_err.read().decode(encoding)
678-
return_code = sub_p.poll()
675+
byte_stdout, byte_stderr = sub_p.communicate()
676+
output = byte_stdout.decode(encoding)
677+
stderr = byte_stderr.decode(encoding)
678+
return_code = sub_p.returncode
679679
if raise_on_error and return_code != 0:
680680
raise RuntimeError("Error executing %s:\n%s" % (command, stderr[:-1]))
681681
return (output, stderr, return_code) if return_error_info else output

0 commit comments

Comments
 (0)