Skip to content

fix(ci): a test that hangs fails at a per-test timeout, so devops ci never stalls on a stuck child process #1669

Closed

Description

@dan-petty

Friction

  • On 2026-10-18 one uv run devops ci sat for more than 68 minutes on a single stuck test and never finished. An xdist worker was blocked in tests/test_release.py::test_release_cut_pushes_from_remote_release_tip_and_leaves_main_untouched, in the git_release_repo fixture's git push -u origin main to a temporary local bare repository.
  • The child git-receive-pack was single-threaded and parked in a futex wait with no CPU time used (utime 0, stime 0, two voluntary context switches). The host load average was about 30 to 50, and another worktree's run of the same test pushed at the same time. Its stack could not be read: the container does not allow ptrace.
  • Nothing bounds a test. pytest has no per-test timeout, so a hung child process holds the pre-push gate, and every 16-worker xdist process it owns, until a person kills it. The owner's rule is that CI gate speed is P0, with a 5-minute budget.

Evidence

Checked at release/v0.2.37 25e2cda.

  • [tool.pytest.ini_options] (pyproject.toml:149-156) sets addopts = "-n logical -p no:logfire -p no:pytest_logfire -p no:randomly" and no timeout. pytest-timeout is not in pyproject.toml or uv.lock; the loaded plugins are cov, anyio, hypothesis, mock, asyncio and xdist.
  • 173 of the 178 subprocess.run, check_output, check_call and Popen calls under tests/ pass no timeout, across 33 files (counted with an ast walk). The fixture's calls at tests/test_release.py:1916-1945 are among them.
  • The hang outlived its session: the stuck process group was still running more than an hour later, with 17 processes.

Remedy

  • Add pytest-timeout (a pytest-dev project) as a dev dependency and set a suite-wide timeout in [tool.pytest.ini_options], with timeout_method = "signal" so a stuck test fails in its own worker, subprocess.run kills its child, and the rest of the run carries on.
  • Size the timeout from the suite's own data: the slowest tests take 22-50 s under heavy load (ci cache durations). Keep it well above that, as a hang bound, not a speed target. Any test that needs longer says so with @pytest.mark.timeout(<seconds>).
  • devops ci test and the full gate report a timed-out test as a failure that names the test, like any other failure. No retry is added.
  • Do not add per-call timeout= to the 173 calls in this item: the suite-wide bound covers them, and it also covers hangs outside subprocess calls.

Acceptance criteria

  • Tests first, offline:
    • a pytester run of a test that sleeps past a small timeout fails with pytest-timeout's report, and a following test in the same run passes;
    • the same under -n 2, so a timed-out test does not take its xdist worker down or stall the session;
    • [tool.pytest.ini_options] declares timeout and timeout_method = "signal" (read with tomllib).
  • uv run devops ci passes with the timeout set; the summary line shows no test near the bound.
  • deptry and uv audit stay green with the new dependency.

Proposed design

Problem statement

A test that hangs causes the CI pipeline to stall indefinitely, as pytest has no built-in per-test timeout mechanism. This leads to long wait times and resource waste when a subprocess call gets stuck, blocking all xdist workers until manually killed.

Acceptance criteria

  • Criterion 1: A pytester run of a test that sleeps past a small timeout fails with pytest-timeout's report
    Verification: Run a test using pytester that includes a sleep longer than the configured timeout, and verify it fails with pytest-timeout's specific error message
  • Criterion 2: The same test under -n 2 does not take down its xdist worker or stall the session
    Verification: Execute the hanging test with pytest -n 2 and confirm that other tests in the session continue running and the hanging worker is isolated
  • Criterion 3: [tool.pytest.ini_options] declares timeout and timeout_method = "signal"
    Verification: Read the pyproject.toml file and verify that [tool.pytest.ini_options] contains timeout and timeout_method = "signal" settings

Key questions

  • What is the current timeout configuration in pytest?: pytest has no per-test timeout configured; it uses default behavior which allows tests to hang indefinitely. [sources: pyproject.toml:149-156]
  • How many subprocess calls in the tests directory pass no timeout parameter?: 173 subprocess.run, check_output, check_call and Popen calls in tests/ pass no timeout across 33 files. [sources: tests/test_release.py:1916-1945]
  • What are the requirements for adding pytest-timeout as a dev dependency?: pytest-timeout should be added to dev-dependencies and configured with timeout_method = "signal" in pyproject.toml. (proposed) [sources: pyproject.toml:149-156]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/infra
    on Oct 18, 2026
  2. dan-petty commented on Oct 18, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Type: type/infra. The change addresses CI infrastructure by adding a timeout mechanism to prevent hanging tests.
    • Priority: P1-High. CI gate speed is P0 priority, and this prevents indefinite hangs that stall the entire CI process.
    • Value: High. This fix prevents CI failures due to stuck processes, improving developer productivity and CI reliability.
    • Effort: Medium. Requires adding a new dependency and configuring pytest-timeout, but does not require rewriting existing tests or logic.
  3. dan-petty commented on Oct 18, 2026

    @dan-petty
    OwnerAuthor

    Status set to Ready at 25e2cda (see proposed design in issue body).

  4. dan-petty commented on Oct 19, 2026

    @dan-petty
    OwnerAuthor

    Amendment (2026-10-19): folded here by intake under the agent filing quota

    The cycle's agent filing allowance (#1153) was used, so devops roadmap intake filed nothing and named this item as the nearest. The candidate below is in the same gate (devops ci test) and can land with this item or on its own in a later cycle. The hook-speed investigation of 2026-10-18 recommended it as P0, under the owner's rule that CI gate speed is P0. Candidate title: "fix(ci): devops ci test starts 16 xdist workers for a few covering tests, so each commit's test hook spends about 180 CPU-seconds and 13 s it does not need".

    Friction

    • The pre-commit hook devops ci test <staged paths> inherits -n logical, so even a few covering tests start 16 xdist workers. Each worker imports conftest, runs the session fixtures and collects the whole selection.
    • It is the largest part of every Python commit: the hook took a median 23.1 s of a 25.2 s commit at load 13-15 (3 runs).
    • For a typical selection it uses about 10x the CPU of a serial run, and that CPU is load every other agent's hooks and gates pay for on the shared 16-CPU container.
    • Agents' direct targeted runs start the same 16 workers. AGENTS.md:108 and :134 give uv run pytest tests/test_<feature>.py as the targeted-test command, and about 25 agent worktrees ran such runs next to the gates on 2026-10-17.

    Evidence

    Checked at release/v0.2.37 30b8fdd.

    • test() builds the pytest command with no -n unless the user passes one (src/devops_cli/commands/ci.py:1388-1399, :1484), so addopts = "-n logical ..." (pyproject.toml:156) applies.
    • One staged file, src/devops_cli/security/sarif.py, selecting 264 tests, 3 interleaved runs at load 14-22: uv run devops ci test <file> took 49.8 / 22.5 / 28.0 s (median 28.0 s) and 221 / 201 / 196 CPU-s; with -n 0 it took 32.5 / 15.4 / 14.6 s (median 15.4 s) and 33 / 20 / 20 CPU-s.
    • Earlier runs of the same selection: equal wall at load 14-25 (22.7 against 24.1 s), and 209 against 26 CPU-s.
    • Larger selections need workers: 19 files and 441 tests took 94 s at -n 0, 50 s at -n 16 and 44 s at -n 4, and -n 4 used a third of the CPU of -n 16.
    • Selections from single-module commits have a median of 3 test files and a p90 of 12.
    • A serial narrowed run once hung after its summary. devops ci test runs pytest through the sync run_subprocess (_run at src/devops_cli/commands/ci.py:223), whose subprocess.run(timeout=1800) (src/devops_cli/core/process.py:393-402) kills only uv, which left pytest orphaned. xdist kills workers stuck at teardown, so -n logical hides such hangs.
    • The gate's checks already run through run_subprocess_async, which starts a new session and stops the whole process group on timeout with the guarded helper (src/devops_cli/core/process.py:506-527, :599; fix(security): the async process runner redacts telemetry and kills the process group on timeout #780).
    • Related: refactor(cli): one process runner owns execution, process groups and dry-run #775 later merges the sync and async runners and their process-group code. This item does not wait for it.

    Remedy

    • In test(), when paths are given and the user passed no -n, choose the worker count from the selection: -n 0 up to a file count set in src/devops_cli/config/defaults.py (for example DEFAULT_CI_TEST_SERIAL_MAX_FILES, calibrated between 5 and 19 files), and a small fixed cap above it (for example DEFAULT_CI_TEST_NARROWED_WORKERS = 4).
    • The full suite, the full-suite fallback and devops ci keep -n logical. The same tests run, and no threshold changes.
    • Change AGENTS.md's targeted-test command (:108, :134) to uv run devops ci test tests/test_<feature>.py, so direct runs get the same sizing.
    • Prerequisites in the same change: run the narrowed pytest through run_subprocess_async, as the gate's checks do, so a timeout stops its whole process group; write no new process-group code. And find the cause of the post-summary hang of a serial run before switching. One candidate is the atexit shutdown_tracer taking _client_lock in the pytest process (src/devops_cli/telemetry/tracer.py:839, :1411, :1634).

    Acceptance criteria

    • Tests first, offline: tests of the command builder show -n 0 for a selection at or under the constant, the capped count above it, the user's -n kept when given, and no -n for the full suite and the fallback.
    • A test shows a narrowed run that outlives its timeout is stopped with its whole process group, and leaves no process behind.
    • A test shows a serial narrowed run exits within a bound after its summary line.
    • AGENTS.md's targeted-test command runs through devops ci test.
    • Measured with load recorded, 3 runs: a one-file commit's hook uses at most a quarter of today's CPU and is no slower.
  5. dan-petty commented on Oct 19, 2026

    @dan-petty
    OwnerAuthor

    Amendment (2026-10-19): folded here by intake under the agent filing quota

    The cycle's agent filing allowance (#1153) was used, so devops roadmap intake filed nothing and named this item as the nearest. The candidate below is related CI-runner work: a test run that cannot start is reported as clean. It can land with this item or be filed on its own in a later cycle. Candidate title: "fix(ci): fail-to-pass reports a replay pytest could not start as could-not-run, not as zero failed and zero errored".

    Friction

    When pytest cannot start on the replayed base tree, devops ci fail-to-pass reports "0 failed and 0 passed …; 0 errored" and treats that as an outcome. For example, the overlaid tests/ import a name the base src/ lacks, and pytest stops with an INTERNALERROR in pytest_configure.

    • A pull request that is not a defect passes the check with nothing replayed.
    • A defect pull request fails with "requires at least one changed test to fail in the call phase on base". That names the wrong reason: no test ran at all.

    Evidence

    At release/v0.2.37 3b70671, with #1312 PR 3 (feat/1312-support-plugins), whose tests/support/bootstrap.py imports defines_fixture_or_hook, new in that PR's src/devops_cli/core/test_selection.py:

    • The replay by hand (base archive plus the head's tests/, python -m pytest -o addopts= -p no:cacheprovider -q tests/test_test_selection.py) exits 3 with INTERNALERROR> ImportError: cannot import name 'defines_fixture_or_hook'.
    • uv run devops ci fail-to-pass --base origin/release/v0.2.37 --title "feat(test): …" exits 0 and prints "0 failed on base (0 failed and 0 passed in tests/test_analyze.py, … tests/test_test_selection.py; 0 errored)".
    • Without a title (so the run counts as a defect pull request) it exits 1, saying a changed test must fail on base.
    • The cause is _run_pytest_on_base (src/devops_cli/ci/fail_to_pass.py). It discards pytest's exit status and returns no outcomes when the JUnit report is missing, so an internal error, a usage error or an interrupted run reads as an empty, clean replay.

    Remedy

    • _run_pytest_on_base keeps pytest's exit status.
    • An exit of 2 (interrupted), 3 (internal error) or 4 (usage error), or a missing JUnit report while changed test files were given, makes the report say the replay could not run on base. The report names the status and the last lines of pytest's output, bounded to 256 characters, and the check fails for any pull request, not only defects. A pull request whose tests cannot run against its base can still use the existing Fail-to-pass: not applicable: <reason> escape.
    • Exit 5 (no tests collected) stays an empty replay.

    Acceptance criteria

    • Tests first, offline: a replay whose pytest exits 3 with no JUnit report fails the check for a feat title and for a defect title. The message names the exit status and the replay, not "at least one changed test to fail".
    • A replay that collects and runs, failing or passing, reports as today.
    • uv run devops ci passes.
  6. added this to the v0.2.37 milestone on Oct 20, 2026
  7. added
    status/in-progress
    on Oct 20, 2026
  8. dan-petty commented on Oct 20, 2026

    @dan-petty
    OwnerAuthor

    What changed

    #1718, merged into release/v0.2.37 as f7aef65.

    The second and last of #1669's pull requests. A fail-to-pass replay whose pytest could not run on the base tree is now reported as "could not run", not as "0 failed, 0 errored". With #1717, this completes #1669.

    The defect. _run_pytest_on_base dropped pytest's exit status and returned no outcomes when the JUnit report was missing. So a replay that never started read as an empty, clean replay:

    • a feature pull request passed the check with nothing replayed;
    • a defect pull request failed it with the wrong reason, "requires at least one changed test to fail".

    #1312 PR 3 (#1703) hit this: its replayed tests imported a name the base's src lacked.

    The fix.

    • Exit status: _run_pytest_on_base keeps pytest's exit status.

    • What counts as "could not run":

      • an exit of 2 (interrupted), 3 (internal error) or 4 (usage error);
      • a run that wrote no complete JUnit report.

      It is recorded as ReplayCouldNotRun, with the exit status and the last whole lines of pytest's output that fit in 256 characters.

    • Exit 3 with a report: it still counts, since pytest writes a report holding pytest::internal when an internal error ends a session it had begun.

    • Broken reports: a report that is empty, or never closes its root testsuites element, counts as no report. The parser is still the stdlib HTMLParser and records whether the root closed.

    • The check: such a replay fails it for any pull request. The report names the exit status, the replayed files and…

    12 file(s) changed.

    How it was verified

    Check runs at 2e3ebd2:

    • Prune PR DevContainer Images: pass
    • Audit Defect PR Hardening: skipping
    • zizmor: pass
    • devops-cli: pass
    • bandit: pass
    • CodeQL: pass
    • Analyze (actions): pass
    • Rule Pack Tests: pass
    • Analyze (python): pass
    • Fail-to-Pass Check: pass
    • Static Analysis: pass
    • Changed-Test Independence: pass
    • Fail-to-Pass Self-Check: pass
    • Tests & Coverage: pass
    • DevContainer Image: skipping
    • Service Image: skipping
    • Dependency Freshness: skipping

    Acceptance Criteria of docs/agent/tasks/task-1669-ci-runner-never-stalls.md:

    • A test that sleeps past a small timeout fails with pytest-timeout's report and the test after it passes (tests/test_per_test_timeout.py::test_a_hanging_test_fails_at_the_timeout_and_the_next_test_passes, a pytester run in a subprocess under a 1 s signal timeout). It failed first because the pytest_timeout plugin could not be imported.
    • The same under -n 2 --dist loadfile: the worker that ran the hanging test reports pytest-timeout's failure and runs the next test itself, so no worker went down (test_a_hanging_test_under_xdist_fails_without_taking_its_worker_down).
    • [tool.pytest.ini_options] declares timeout, above THREAD_HANDOFF_BOUND_SECONDS and under half DEFAULT_SUBPROCESS_TIMEOUT_SECONDS, and timeout_method = "signal", read with tomllib, and the running session has the plugin loaded with those values (test_the_suite_bounds_every_test_with_a_signal_timeout_between_its_waits_and_its_run); with -p no:timeout it fails.
    • uv run devops ci passes with the timeout set, and its summary shows no test near the bound: the slowest took 19.3 s against 600 s.
    • A narrowed run with no -n gets -n 0; the full suite and the fallback pass no -n; a given -n 3 is kept (tests/test_test_selection.py::test_a_narrowed_run_chooses_its_workers_and_the_full_suite_keeps_addopts). A selection of DEFAULT_CI_TEST_SERIAL_MAX_FILES test modules runs serially and one more on DEFAULT_CI_TEST_NARROWED_WORKERS workers (test_a_selection_past_the_serial_limit_runs_on_the_capped_workers). Both read -n from the argv the stdlib double received.
    • devops ci test, narrowed or whole, starts pytest in a session of its own, never through subprocess.run, and leaves devops_cli.commands.ci with no runner name a later test's patch would miss (test_the_test_run_starts_pytest_in_a_session_of_its_own).
    • A narrowed run past its timeout fails naming its command and the 1800 s timeout (tests/test_ci.py::test_a_test_run_past_its_timeout_fails_naming_its_command_and_timeout), and a hung run is stopped at its bound with its whole group, SIGTERM first, as devops ci's checks were (tests/test_process.py::test_a_timed_out_check_is_stopped_with_sigterm_first_and_reports_as_before).
    • A serial narrowed run that hangs after its summary line is stopped: the only bound after the summary is the run's timeout, counted from the start, 1800 s for devops ci test, and a fake pytest that prints its summary and then hangs with a grandchild is stopped with its whole group at its bound (test_a_run_that_hangs_after_its_summary_line_is_stopped_at_its_bound_with_its_group). No hang after the summary reproduced in 21 serial runs, and the tracer's client lock was ruled out as a cause.
    • Ctrl-C, SIGTERM or SIGHUP to the real devops ci test forwards SIGINT to the run's group, whose fake uv then runs its teardown, stops the whole group and exits 130 (test_a_signal_to_devops_ci_test_lets_pytest_tear_down_then_stops_its_group); against the code before the stop sequence, the teardown never ran for any of the three.
    • A second Ctrl-C, SIGTERM or SIGHUP during the stop's grace kills a group that ignores SIGINT and SIGTERM, cutting the grace short: the child records no SIGTERM after the test marked its second signal (test_a_second_signal_to_devops_ci_test_cannot_skip_the_sigkill, and in-process test_a_further_cancellation_during_the_stop_cannot_skip_its_sigkill); before, the group was left running, and a stop that absorbs the second cancellation and still sends SIGTERM fails them.
    • A cancellation stops the whole group, sends SIGINT first so a teardown runs, still stops it when it lands while the child is starting, stays a CancelledError when the child has already exited, and still stops the members left after the leader exited (test_a_cancelled_run_stops_its_whole_process_group, test_a_cancelled_run_sends_its_group_sigint_first_so_its_teardown_runs, test_a_cancellation_while_the_child_starts_still_stops_its_group, test_a_cancellation_after_the_child_exited_stays_a_cancellation, test_a_cancellation_after_the_child_exited_still_stops_the_members_left); the middle three failed against the earlier stop, and a stop skipped once the leader has exited fails the last. A child that leads no session of its own fails every one of them.
    • With SIGINT and SIGHUP ignored by the process that starts the tests, the signal and handler tests still pass.
    • SIGTERM and SIGHUP are taken over during a main-thread run and handed back after it, whether it ends or raises; a run off the main thread sets none; an ignored SIGHUP stays ignored (test_a_run_takes_over_sigterm_and_sighup_and_hands_them_back, test_a_run_off_the_main_thread_sets_no_signal_handler, test_an_ignored_hangup_stays_ignored_through_a_run). Mutants that skip the hand-back or set handlers from any thread fail them.
    • The commit hook's command for a one-module change, uv run devops ci test src/devops_cli/security/sarif.py (5 test modules, 264 tests), three runs each, interleaved, at a load average of 0.9-5.8: 10.6 s and 12.1 CPU-s against 17.7 s and 182.5 CPU-s with -n logical, a fifteenth of the CPU and faster.
    • AGENTS.md's targeted-test command runs through devops ci test: its verification step and its commands table give uv run devops ci test tests/test_<feature>.py.
    • deptry and uv audit stay green with the new dependency.
    • Tests first, offline, in tests/test_fail_to_pass.py: test_a_replay_pytest_could_not_start_fails_the_check_for_any_pull_request replays with a pytest that exits 3 with an INTERNALERROR> traceback and no JUnit report, for a feat and a fix title. Before, the feature exited 0 with "0 failed on base (0 failed and 0 passed in tests/test_mod.py; 0 errored)." and the defect exited 1 with "Defect PR requires at least one changed test to fail"; both now exit 1 with the message naming status 3, tests/test_mod.py and the traceback's last lines, the whole output checked against its snapshot.
    • test_a_replay_that_stopped_or_wrote_no_report_could_not_run compares the whole model_dump() and the report's snapshot for exit 2 with a report holding a passing test, exit 3 with pytest's pytest::internal report, exit 4 with an empty report, a killed run with no report, and exit 1 with an empty and with a truncated report. The exit-2, exit-4 and killed cases failed before this PR (could_not_run absent, the check satisfied). The empty and truncated reports failed before the parser tracked its root: the check satisfied, could_not_run None, and the truncated report's passing test kept. With frozenset({2, 4}) as the could-not-run statuses, the exit-3 case fails (satisfied True), where every other test still passed.
    • test_a_replay_that_could_not_run_keeps_the_last_whole_lines_within_the_bound keeps the last 6 of 40 lines, the last 256 characters of one 315-character line, and all three of three lines that fill the 256 characters exactly; a slice one character short (printed[-256:]) keeps two of those and fails it.
    • test_a_replay_that_could_not_run_prints_pytest_s_output_as_it_is prints a usage error's [options] [file_or_dir] [file_or_dir] [...] and PATH=src:x:tests and a:b:c as pytest wrote them. Before this PR it exited 0; with print_error(..., safe=True) the second case failed, Rich printing an emoji for :x:, and without safe=True the first did.
    • A replay that collects and runs, failing, passing or erroring at collection, reports as before (test_a_replay_that_collects_and_runs_reports_its_outcomes), exit 5 stays an empty replay for a feature and a defect (test_a_replay_that_collects_no_tests_is_an_empty_replay), and the not-applicable line passes a replay whose pytest would exit 3 without starting it (test_the_not_applicable_line_wins_over_a_replay_that_could_not_run); each passed before and after.
    • uv run devops ci fail-to-pass --base origin/release/v0.2.37 --head origin/feat/1312-support-plugins --title "feat(test): …" exits 1: "The fail-to-pass replay could not run on base: pytest exited with status 3 replaying tests/test_analyze.py, … tests/test_test_selection.py." and "pytest's last output: INTERNALERROR> ImportError: cannot import name 'defines_fixture_or_hook' …".
    • uv run devops ci passes with PR 2's change on top of PR 1: every check green, coverage 92.6%, the run 2 m 47 s, its test step matching the 10 snapshots.
  9. removed
    status/in-progress
    on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    source/agenttype/infra

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions