Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit 67146a4

Browse files
committed
Avoid deadlock with subprocess
Heed Python's subprocess module documentation warnings about deadlock when using PIPE for a Popen object's stderr with Popen.wait() and Popen.stderr.read() and use Popen.communicate() instead. Fixes #126.
1 parent 4a217ae commit 67146a4

2 files changed

Lines changed: 11 additions & 14 deletions

File tree

supernova/supernova.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def execute_executable(nova_args, env_vars):
4040
stdout=sys.stdout,
4141
stderr=subprocess.PIPE,
4242
env=env_vars)
43-
process.wait()
44-
return process
43+
stderr = process.communicate()[1]
44+
return process, stderr
4545

4646

4747
def check_for_debug(supernova_args, nova_args):
@@ -88,17 +88,15 @@ def check_for_bypass_url(raw_creds, nova_args):
8888
return nova_args
8989

9090

91-
def handle_stderr(stderr_pipe):
91+
def handle_stderr(stderr):
9292
"""
9393
Takes stderr from the command's output and displays it AFTER the stdout
9494
is printed by run_command().
9595
"""
96-
stderr_output = stderr_pipe.read()
97-
98-
if len(stderr_output) > 0:
96+
if len(stderr) > 0:
9997
click.secho("\n__ Error Output {0}".format('_'*62), fg='white',
10098
bold=True)
101-
click.echo(stderr_output)
99+
click.echo(stderr)
102100

103101
return True
104102

@@ -141,10 +139,10 @@ def run_command(nova_creds, nova_args, supernova_args):
141139
# In other news, I hate how python 2.6 does unicode.
142140
nova_args.insert(0, supernova_args['executable'])
143141
nova_args = [nova_arg.strip() for nova_arg in nova_args]
144-
process = execute_executable(nova_args, env_vars)
142+
process, stderr = execute_executable(nova_args, env_vars)
145143

146144
# If the user asked us to be quiet, then let's not print stderr
147145
if not supernova_args.get('quiet'):
148-
handle_stderr(process.stderr)
146+
handle_stderr(stderr)
149147

150148
return process.returncode

tests/test_supernova.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,9 @@ def test_debug_check_with_heat(self):
9696
expected_nova_args = ['-d ']
9797
assert result == expected_nova_args
9898

99-
def test_handle_stderr(self, tmpdir, capsys):
100-
p = tmpdir.mkdir("sub").join("stderr.txt")
101-
p.write("This would be in the stderr pipe")
102-
result = supernova.handle_stderr(p)
99+
def test_handle_stderr(self, capsys):
100+
s = "This would be in the stderr pipe"
101+
result = supernova.handle_stderr(s)
103102
out, err = capsys.readouterr()
104-
assert "stderr pipe" in out
103+
assert s in out
105104
assert result

0 commit comments

Comments
 (0)