Skip to content

fix(ci): a hanging test fails at a per-test timeout, and a narrowed devops ci test sizes its workers and stops its whole process group - #1717

Merged
dan-petty merged 1 commit into
release/v0.2.37from
fix/1669-test-runner-bounds
Oct 20, 2026
Merged

dan-petty merged 1 commit into
release/v0.2.37from
fix/1669-test-runner-bounds

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

The first of #1669's two pull requests. A hanging test fails at a per-test timeout, and a narrowed devops ci test sizes its workers and stops its whole process group. Before this, nothing bounded a test: on 2026-10-18 one stuck child process held a devops ci run, and every xdist worker it owned, for more than an hour.

Per-test timeout.

  • The plugin: pytest-timeout 2.4.0 joins the dev group. 2.4.0 is the current release; 2.5.0 is yanked.
  • The bound: [tool.pytest.ini_options] sets timeout = 600 and timeout_method = "signal". A test that hangs fails in its own process with pytest-timeout's report naming it, and the run and its worker carry on.
  • Sizing: it is a hang bound, not a speed target. Across 185 cached gate runs the slowest test took 139 s, at load average 30-50. It stays under half the run's 1800 s timeout, so a hung test is reported by name before the whole run is stopped.
  • No per-call timeout= is added under tests/.

Workers for a narrowed run. Given no -n, a narrowed devops ci test runs serially for up to 12 test modules (DEFAULT_CI_TEST_SERIAL_MAX_FILES) and on 4 workers above that (DEFAULT_CI_TEST_NARROWED_WORKERS). The full suite, the fallback to it, and devops ci keep -n logical, and an explicit -n is kept.

Test modules Wall, serial vs 4 workers CPU, serial vs 4 workers
up to 8 serial fastest or within noise about a third (29-37%)
12 79 s vs 82 s 84 vs 193 CPU-s
19 4 workers 2.8x faster about the same

The commit hook's run for a one-module change took 10.6 s and 12 CPU-seconds, against 17.7 s and 183 CPU-seconds with -n logical.

The whole process group stops.

  • The orphan: devops ci test ran pytest through subprocess.run, whose timeout killed only uv and left pytest running.
  • The fix: every pytest run it starts now goes through run_subprocess_async in its own session. A timeout stops the whole group, and the run fails naming its command and timeout.
  • Cancellation: Ctrl-C, or SIGTERM or SIGHUP to devops ci test, cancels the run. The group first gets the SIGINT a terminal would have sent, so pytest runs its teardown and finally blocks, then SIGTERM, then SIGKILL.
  • A second signal cuts the grace short but can't skip the SIGKILL, which is sent from a finally.
  • The start is shielded, so one cancellation while the child starts still stops its group. A second Ctrl-C in that window leaves asyncio's own clean-up to kill the child alone; this limit is recorded.
  • devops ci's other checks: a timeout there still starts at SIGTERM and reports as before.
  • Handlers: they are set only from the main thread, leave an ignored SIGHUP ignored for nohup, and are handed back after the run.
  • Guards: every existing process-group guard is kept: never pgid 0 or 1, never the caller's group.

After the summary line. A serial run that hangs after printing its summary is stopped with its group at the run's timeout. That is the only bound there, counted from the start: 1800 s for devops ci test. No serial run hung in 21 measured runs. The tracer's atexit shutdown can't deadlock on its client lock, because no export holds it during I/O.

Tests.

  • The real command: they drive devops ci test through its CLI, with the process edge doubled at asyncio.create_subprocess_exec. The signal tests run the real devops on the tree under test, with a fake uv and telemetry off.
  • No real runs: a guard in the ci, selection, repeat and process tests refuses any real uv or pytest child, except pytester's.
  • Signal dispositions: the tests set the ones they rely on, whatever the run inherited, so they pass under nohup or as a background job.
  • Hung children: tests/support/hung_children.py watches them through pidfds. It fails a test whose child leads no session of its own, and kills whatever a failing test leaves.

Docs.

  • The targeted-test command becomes uv run devops ci test <test-file> in AGENTS.md, CONTRIBUTING.md, docs/SDLC.md, docs/SELF_IMPROVEMENT.md, docs/ROUTINE_TASKS.md, the knowledge base and the generated AGENTS.md block.
  • docs/TESTING.md describes the bound, the workers, the stop and the doubles.
  • The task file, docs/agent/tasks/task-1669-ci-runner-never-stalls.md, records all of it.
  • As a Part of pull request, it adds no changelog fragment. The fail-to-pass replay's could-not-run report is the item's other pull request, which completes it.

Verification

  • Tests first. Each new test failed before its code existed, and each review fix was shown failing against the reviewers' own harnesses or a planted mutant:
    • SIGTERM-first cancellation;
    • a skippable SIGKILL;
    • an unshielded spawn;
    • a late ProcessLookupError;
    • an overridden SIG_IGN;
    • unrestored handlers;
    • an off-thread install;
    • a watcher passing vacuously;
    • a second signal that still escalates.
  • Review. An adversarial review with skeptic verification found four blockers in the first version: Ctrl-C skipped pytest's teardown, a second signal could skip SIGKILL, a cancellation during spawn orphaned the group, and a leaked patch could start the real gate. Two further verification passes followed: one found test-machinery gaps, which are fixed, and a final pass confirmed each fix.
  • Process safety. Reviewers and the implementer signalled only their own scratch children, and no process was left behind.
  • Isolation. The full suite passed under a guard that refuses gh children: 11,070 passed, 1 skipped and 8 xfailed. The slowest test took 20.8 s, against the 600 s bound. The new signal and process tests also pass when the run inherits SIGINT and SIGHUP as ignored.
  • Fail-to-pass. devops ci fail-to-pass --base origin/release/v0.2.37 replays the changed tests on the base: the per-test-timeout and process tests fail there, and the selection tests don't collect. The pull request's own fail-to-pass check runs the same replay.
  • Gate. uv run devops ci passed (pre-push hook).

Part of #1669

…evops ci test sizes its workers and stops its whole process group

Nothing bounded a test, so one stuck child process held a devops ci run, and every xdist worker it owned, for more than an hour. pytest-timeout 2.4.0, the current release (2.5.0 is yanked), joins the dev group, and [tool.pytest.ini_options] sets timeout = 600 with timeout_method = "signal", so a test that hangs fails in its own process with pytest-timeout's report naming it, and the run and its worker carry on. The bound is a hang bound: the slowest test in 185 cached gate runs took 139 s at a load average of 30-50, the latest runs' slowest 21-34 s, and the bound stays under half the run's 1800 s timeout, so a hung test is reported by name before the whole run is stopped. No per-call timeout is added to the subprocess calls under tests/.

A narrowed devops ci test given no -n started 16 workers for a few covering tests. It now runs serially for up to DEFAULT_CI_TEST_SERIAL_MAX_FILES (12) test modules and on DEFAULT_CI_TEST_NARROWED_WORKERS (4) above that; the full suite, the fallback to it and devops ci keep -n logical, and a given -n is kept. Measured at -n 0, 4 and 16 over selections of 1, 3, 5, 8, 12 and 19 test modules: up to 8, serial was the fastest or within the noise at about a third of the CPU of 4 workers; at 12, serial matched 4 workers' 79 against 82 s at 84 against 193 CPU-seconds; at 19, 4 workers ran 2.8 times faster than serial at about the same CPU. The hook's command for a one-module change took 10.6 s and 12 CPU-seconds against 17.7 s and 183 with -n logical.

devops ci test ran pytest through subprocess.run, whose timeout kills only uv and left pytest running. Every pytest run it starts now goes through run_subprocess_async in a session of its own, so a timeout stops the whole group, and the run fails naming its command and timeout. No terminal or supervisor signal reaches that session, so a cancellation, which Ctrl-C makes and which SIGTERM and SIGHUP make for a devops ci test run, forwards the SIGINT a terminal would have sent, so pytest runs its teardown and finally blocks, then escalates to SIGTERM and SIGKILL. A second signal cuts the grace short but cannot skip the SIGKILL, which is sent from a finally; the start is shielded, so one cancellation while the child starts still stops its group, though a second Ctrl-C in that window leaves asyncio's clean-up to kill the child alone; and a late cancellation stays a cancellation. A timeout, which devops ci's checks share, still starts at SIGTERM. The handlers are set only from the main thread, leave an ignored SIGHUP ignored for nohup, and are handed back after the run.

A serial run that hangs after its summary line is stopped with its whole group at the run's timeout, the only bound there, counted from the start: 1800 s for devops ci test. No serial run hung in the measured runs: the tail is pytest's unconfigure gc, the capture teardown and interpreter finalization, and the tracer's atexit shutdown cannot deadlock on its client lock, which no export holds during I/O.

The tests drive devops ci test through its CLI, double the process edge at asyncio.create_subprocess_exec, refuse any real uv or pytest child, set the signal dispositions they rely on whatever the run inherited, run the real devops on the tree under test, and watch hung children through pidfds, failing a test whose child leads no session of its own and killing what a failing test leaves. The targeted-test command is uv run devops ci test <test-file> in AGENTS.md, CONTRIBUTING.md, docs/SDLC.md, docs/SELF_IMPROVEMENT.md, docs/ROUTINE_TASKS.md, the knowledge base and the generated AGENTS.md block, and docs/TESTING.md describes the bound, the workers, the stop and the doubles. The fail-to-pass replay's could-not-run report is the pull request that closes the item.

Part of #1669
@dan-petty
dan-petty merged commit d8ae0e5 into release/v0.2.37 Oct 20, 2026
15 checks passed
@dan-petty
dan-petty deleted the fix/1669-test-runner-bounds branch October 20, 2026 11:25
dan-petty added a commit that referenced this pull request Oct 20, 2026
…d-not-run (#1718)

When pytest could not start on the replayed base tree, devops ci fail-to-pass reported "0 failed and 0 passed ...; 0 errored" as an outcome: a feature passed the check with nothing replayed, and a defect failed it saying no changed test failed on base, though none ran. _run_pytest_on_base dropped pytest's exit status and returned no outcomes when the JUnit report was missing.

_run_pytest_on_base now keeps pytest's exit status. An exit of 2 (interrupted), 3 (internal error) or 4 (usage error), or a run that wrote no complete JUnit report, is a ReplayCouldNotRun on the report, with the exit status and the last whole lines of pytest's output that fit in 256 characters. Exit 3 counts with a report too, as pytest writes one holding pytest::internal when an internal error ends a session it had begun to report. A report that is empty or never closes its root testsuites element counts as none: the JUnit parser, still the stdlib HTMLParser, records whether the root closed. Such a replay fails the check for any pull request; the report names the exit status, the replayed test files and pytest's last output, and points to the Fail-to-pass: not applicable line, which still passes the check without replaying. Exit 5, no tests collected, stays an empty replay, and a replay whose tests collect and run reports as before, so a changed test that only fails to collect on base is reported as errored. The command writes an unsatisfied report raw, with markup and emoji codes off, so pytest's output reaches the terminal as pytest wrote it.

AGENTS.md and the instruction generator's line say the check fails any pull request whose replay pytest could not run on base, as when the overlaid tests/ import support the base lacks or the pull request changes dependencies or the test harness; that such a pull request declares the not-applicable line; and that a changed test that only fails to collect on base is reported as errored. The could-not-run reports' text is checked against syrupy snapshots, the suite's first, which docs/TESTING.md now names; the trailing-whitespace hook leaves tests/__snapshots__/ alone, as trimming a blank line's indent breaks a snapshot.

With part 1 (#1717), the per-test timeout, the narrowed run's worker count and its process-group stop, this completes the item. The task file keeps part 1's section and adds this one as PR 2, with every criterion of the item and both amendments delivered, and changelog.d/1669.md is the item's one fragment, covering both parts.

Closes #1669
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant