Skip to content

Commit

Permalink
[bp/2.2] Make sure we close stdout/stderr in Runner.run()
Browse files Browse the repository at this point in the history
  • Loading branch information
Shrews committed Sep 8, 2022
1 parent 88404ce commit a12121b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ansible_runner/runner.py
Expand Up @@ -275,6 +275,9 @@ def run(self):
if isinstance(stderr_response, bytes):
stderr_response = stderr_response.decode()
stderr_handle.write(stderr_response)

stdout_handle.close()
stderr_handle.close()
else:
try:
child = pexpect.spawn(
Expand Down Expand Up @@ -336,8 +339,8 @@ def _decode(x):
Runner.handle_termination(child.pid, is_cancel=False)
self.timed_out = True

stdout_handle.flush()
stdout_handle.close()
stderr_handle.close()
child.close()
self.rc = child.exitstatus if not (self.timed_out or self.canceled) else 254

Expand Down
16 changes: 16 additions & 0 deletions test/unit/test_runner.py
Expand Up @@ -188,3 +188,19 @@ def test_multiline_blank_write(rc, runner_mode):
assert status == 'successful'
stdout_path = Path(rc.artifact_dir) / 'stdout'
assert stdout_path.read_text() == 'hello_world_marker\n\n\n\n' # one extra newline okay


@pytest.mark.parametrize('runner_mode', ['subprocess'])
@pytest.mark.filterwarnings("error")
def test_no_ResourceWarning_error(rc, runner_mode):
"""
Test that no ResourceWarning error is propogated up with warnings-as-errors enabled.
Not properly closing stdout/stderr in Runner.run() will cause a ResourceWarning
error that is only seen when we treat warnings as an error.
"""
rc.command = ['echo', 'Hello World']
rc.runner_mode = runner_mode
runner = Runner(config=rc)
status, exitcode = runner.run()
assert status == 'successful'

0 comments on commit a12121b

Please sign in to comment.