Skip to content

Commit

Permalink
remove stdout=subprocess.PIPE from env._call, which can cause poetry …
Browse files Browse the repository at this point in the history
…install to hang (#7699)

Co-authored-by: Randy Döring <30527984+radoering@users.noreply.github.com>
  • Loading branch information
fzahle and radoering authored Mar 23, 2023
1 parent 71b3d6e commit fa5543a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -1537,9 +1537,8 @@ def _run(self, cmd: list[str], **kwargs: Any) -> int | str:
**kwargs,
).stdout
elif call:
return subprocess.call(
cmd, stdout=subprocess.PIPE, stderr=stderr, env=env, **kwargs
)
assert stderr != subprocess.PIPE
return subprocess.call(cmd, stderr=stderr, env=env, **kwargs)
else:
output = subprocess.check_output(cmd, stderr=stderr, env=env, **kwargs)
except CalledProcessError as e:
Expand Down
26 changes: 26 additions & 0 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys

from pathlib import Path
from threading import Thread
from typing import TYPE_CHECKING
from typing import Any

Expand Down Expand Up @@ -1009,6 +1010,31 @@ def test_check_output_with_called_process_error(
assert "some error" in str(error.value)


@pytest.mark.parametrize("out", ["sys.stdout", "sys.stderr"])
def test_call_does_not_block_on_full_pipe(
tmp_path: Path, tmp_venv: VirtualEnv, out: str
):
"""see https://github.com/python-poetry/poetry/issues/7698"""
script = tmp_path / "script.py"
script.write_text(
f"""\
import sys
for i in range(10000):
print('just print a lot of text to fill the buffer', file={out})
"""
)

def target(result: list[int]) -> None:
result.append(tmp_venv.run("python", str(script), call=True))

results = []
# use a separate thread, so that the test does not block in case of error
thread = Thread(target=target, args=(results,))
thread.start()
thread.join(1) # must not block
assert results and results[0] == 0


def test_run_python_script_called_process_error(
tmp_dir: str, tmp_venv: VirtualEnv, mocker: MockerFixture
):
Expand Down

0 comments on commit fa5543a

Please sign in to comment.