-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix stalling in Pipeline command (#632)
* Fix stalling in Pipeline command When a command (other than the first) in a pipeline wrote more than 64k to the stderr, and the output was consumed with the iter_lines function, the whole pipeline stalled. Fixed by reading the output of all commands where either stdout or stderr was set to PIPE. * disable new tests on windows, increase timeout * black & flake8 * trying to figure out what the extra items are * test_pipelines: ignore empty lines somehow stderr includes an empty line when run on pypy * skip new tests on windows * Apply suggestions from code review --------- Co-authored-by: Henry Schreiner <[email protected]>
- Loading branch information
Showing
3 changed files
with
122 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from typing import List, Tuple | ||
|
||
import pytest | ||
|
||
import plumbum | ||
from plumbum._testtools import skip_on_windows | ||
from plumbum.commands import BaseCommand | ||
|
||
|
||
@skip_on_windows | ||
@pytest.mark.timeout(3) | ||
def test_draining_stderr(generate_cmd, process_cmd): | ||
stdout, stderr = get_output_with_iter_lines( | ||
generate_cmd | process_cmd | process_cmd | ||
) | ||
expected_output = {f"generated {i}" for i in range(5000)} | ||
expected_output.update(f"consumed {i}" for i in range(5000)) | ||
assert set(stderr) - expected_output == set() | ||
assert len(stderr) == 15000 | ||
assert len(stdout) == 5000 | ||
|
||
|
||
@skip_on_windows | ||
@pytest.mark.timeout(3) | ||
def test_draining_stderr_with_stderr_redirect(tmp_path, generate_cmd, process_cmd): | ||
stdout, stderr = get_output_with_iter_lines( | ||
generate_cmd | (process_cmd >= str(tmp_path / "output.txt")) | process_cmd | ||
) | ||
expected_output = {f"generated {i}" for i in range(5000)} | ||
expected_output.update(f"consumed {i}" for i in range(5000)) | ||
assert set(stderr) - expected_output == set() | ||
assert len(stderr) == 10000 | ||
assert len(stdout) == 5000 | ||
|
||
|
||
@skip_on_windows | ||
@pytest.mark.timeout(3) | ||
def test_draining_stderr_with_stdout_redirect(tmp_path, generate_cmd, process_cmd): | ||
stdout, stderr = get_output_with_iter_lines( | ||
generate_cmd | process_cmd | process_cmd > str(tmp_path / "output.txt") | ||
) | ||
expected_output = {f"generated {i}" for i in range(5000)} | ||
expected_output.update(f"consumed {i}" for i in range(5000)) | ||
assert set(stderr) - expected_output == set() | ||
assert len(stderr) == 15000 | ||
assert len(stdout) == 0 | ||
|
||
|
||
@pytest.fixture() | ||
def generate_cmd(tmp_path): | ||
generate = tmp_path / "generate.py" | ||
generate.write_text( | ||
"""\ | ||
import sys | ||
for i in range(5000): | ||
print("generated", i, file=sys.stderr) | ||
print(i) | ||
""" | ||
) | ||
return plumbum.local["python"][generate] | ||
|
||
|
||
@pytest.fixture() | ||
def process_cmd(tmp_path): | ||
process = tmp_path / "process.py" | ||
process.write_text( | ||
"""\ | ||
import sys | ||
for line in sys.stdin: | ||
i = line.strip() | ||
print("consumed", i, file=sys.stderr) | ||
print(i) | ||
""" | ||
) | ||
return plumbum.local["python"][process] | ||
|
||
|
||
def get_output_with_iter_lines(cmd: BaseCommand) -> Tuple[List[str], List[str]]: | ||
stderr, stdout = [], [] | ||
proc = cmd.popen() | ||
for stdout_line, stderr_line in proc.iter_lines(retcode=[0, None]): | ||
if stderr_line: | ||
stderr.append(stderr_line) | ||
if stdout_line: | ||
stdout.append(stdout_line) | ||
proc.wait() | ||
return stdout, stderr |