Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ Version 8.4.2

Unreleased

- Fix a race in ``echo_via_pager``'s exception-handling tests so they
pass deterministically under parallel execution and free-threaded
Python 3.14t, removing the workaround skip from :pr:`3470`.
:issue:`2899` :pr:`3499`
- The ``stress`` tox env now collects ``@pytest.mark.stress`` tests
from any test module. :pr:`3499`


Version 8.4.1
-------------
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,11 @@ commands = [[
]]

[tool.tox.env.stress]
description = "stress tests for stream lifecycle race conditions"
description = "high-iteration stress tests for race conditions"
commands = [[
"pytest", "-v", "--tb=short", "-x", "-m", "stress",
"--basetemp={env_tmp_dir}",
"--override-ini=addopts=",
"tests/test_stream_lifecycle.py",
{replace = "posargs", default = [], extend = true},
]]

Expand Down
1 change: 0 additions & 1 deletion src/click/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

CYGWIN = sys.platform.startswith("cygwin")
WIN = sys.platform.startswith("win")
MAC = sys.platform == "darwin"
auto_wrap_for_ansi: t.Callable[[t.TextIO], t.TextIO] | None = None
_ansi_re = re.compile(r"\033\[[;?0-9]*[a-zA-Z]")

Expand Down
4 changes: 3 additions & 1 deletion tests/test_stream_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,9 @@ def cli():
# Category 6: Stress tests - high-iteration reproducers for race conditions
#
# These are marked with ``pytest.mark.stress`` so they can be included or
# excluded independently. The CI workflow runs them in a separate job.
# excluded independently. The ``tox -e stress`` env collects every
# ``pytest.mark.stress`` test across the suite (not just this file), so
# stress regressions for other components live alongside their unit tests.
# ---------------------------------------------------------------------------


Expand Down
79 changes: 74 additions & 5 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import click._termui_impl
import click.utils
from click._compat import MAC
from click._compat import WIN
from click._utils import UNSET

Expand Down Expand Up @@ -286,6 +285,11 @@ def _test_gen_func():


def _test_gen_func_fails():
raise RuntimeError("This is a test.")
yield # unreachable, keeps this a generator function


def _test_gen_func_yields_then_fails():
yield "test"
raise RuntimeError("This is a test.")

Expand Down Expand Up @@ -315,10 +319,6 @@ def _test_simulate_keyboard_interrupt(file=None):


@pytest.mark.skipif(WIN, reason="Different behavior on windows.")
@pytest.mark.skipif(
MAC and sys.version_info >= (3, 13) and not sys._is_gil_enabled(),
reason="Generator exception tests are flaky in Python 3.14t on macOS.",
)
@pytest.mark.parametrize(
"pager_cmd", ["cat", "cat ", " cat ", "less", " less", " less "]
)
Expand Down Expand Up @@ -456,6 +456,75 @@ def test_echo_via_pager(monkeypatch, capfd, pager_cmd, test, tmp_path):
)


@pytest.mark.skipif(WIN, reason="Different behavior on windows.")
def test_echo_via_pager_yields_before_exception(monkeypatch, tmp_path):
"""A generator that yields then raises: click writes the partial output to
the pager stream before propagating the exception.

The pager file content is intentionally NOT asserted: pipe-drain timing
between click and the pager subprocess is outside click's control
(#2899, #3470). Spying on ``MaybeStripAnsi.write`` records what click sent
to the pager, which is deterministic regardless of scheduling.
"""
monkeypatch.setitem(os.environ, "PAGER", "cat")
monkeypatch.setattr(click._termui_impl, "isatty", lambda x: True)

writes: list[str] = []
real_write = click._termui_impl.MaybeStripAnsi.write

def spy(self, text):
writes.append(text)
return real_write(self, text)

monkeypatch.setattr(click._termui_impl.MaybeStripAnsi, "write", spy)

pager_out_tmp = tmp_path / "pager_out.txt"
with (
pager_out_tmp.open("w") as f,
patch.object(subprocess, "Popen", partial(subprocess.Popen, stdout=f)),
pytest.raises(RuntimeError, match="This is a test."),
):
click.echo_via_pager(_test_gen_func_yields_then_fails())

assert "".join(writes) == "test", (
f"click should have written the yielded chunk before exception, got {writes!r}"
)


@pytest.mark.stress
@pytest.mark.skipif(WIN, reason="Different behavior on windows.")
@pytest.mark.parametrize("_", range(1000))
def test_stress_echo_via_pager_exception_cleanup(_, monkeypatch, tmp_path):
"""Repeated exceptions during ``echo_via_pager`` must not leak subprocesses.

Regression coverage for the cleanup path in ``_pipepager``'s exception
handler (issue #2899, PR #3470). Each iteration spawns a real pager
subprocess, raises before any data is written and check there is no leak.
"""
monkeypatch.setitem(os.environ, "PAGER", "cat")
monkeypatch.setattr(click._termui_impl, "isatty", lambda x: True)

spawned: list[subprocess.Popen] = []
real_popen = subprocess.Popen

def tracking_popen(*args, **kwargs):
p = real_popen(*args, **kwargs)
spawned.append(p)
return p

pager_out_tmp = tmp_path / "pager_out.txt"
with (
pager_out_tmp.open("w") as f,
patch.object(subprocess, "Popen", partial(tracking_popen, stdout=f)),
pytest.raises(RuntimeError),
):
click.echo_via_pager(_test_gen_func_fails())

assert spawned, "pager subprocess was never started"
for p in spawned:
assert p.returncode is not None, "pager subprocess not reaped"


def test_echo_color_flag(monkeypatch, capfd):
isatty = True
monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)
Expand Down
Loading