Skip to content

Fix #1011: doctor.sh watchdog subshell held the caller's stdout/stderr pipe open for the full timeout bound - #1025

Merged
fdaviddpt merged 3 commits into
mainfrom
fix-1011-watchdog-stdout-inherit
Oct 11, 2026
Merged

fdaviddpt merged 3 commits into
mainfrom
fix-1011-watchdog-stdout-inherit

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Summary

Fixes #1011: scripts/doctor.sh watchdog subshell held the callers stdout/stderr pipe open for the full timeout bound, so a caller capturing _remember_doctor_run_powershell_file output via command substitution blocked for roughly 2x its own bound even when PowerShell answered almost instantly (observed on real windows-latest CI in PR #1024, and reproduced on macOS proving the bug is a POSIX pipe mechanism, not Windows-specific). This is a later round of the same issue as #1002 and #1024.

Closes #1011

What changed

  • scripts/doctor.sh: redirect the watchdog subshells own stdout/stderr so it no longer holds a callers command-substitution pipe open; hand the watchdogs sleep child PID out through a temp file so an early PowerShell exit can reap that child directly too, with a bounded 1s retry closing a narrow fork-to-pidfile-write race a reviewer found and reproduced (self-review disposition: fixed).
  • tests/test_doctor_powershell_watchdog_stdout_leak_1011.py: new, extracts the real function out of scripts/doctor.sh verbatim at test time and drives it against a stub powershell.exe; includes a positive control (hung stub still times out correctly) and a manufactured-race regression test.
  • changelog.d/1011.fixed.watchdog-stdout-leak.md, docs/windows-skip-triage.md: changelog fragment and triage row for the new blanket-skipped-on-Windows test module.
  • trap.d/: two fragments logged, not fixed in this PR -- see below.

Audited, not modified

scripts/lib-detach.sh's watchdog (used on every Windows save) has the identical pattern this PR fixes in doctor.sh, but its own earlier #1002 fix already redirects its fds, so it is not affected by the blocking bug this PR covers; confirmed by running its existing 24-test suite, all well under its own bound. scripts/lib-detach.sh still has the same "kill the subshell, not the sleep child" shape this PR fixed in doctor.sh -- logged in trap.d/1011.lib-detach-same-stray-sleep-shape-unfixed.md rather than fixed here, because it is confirmed unaffected by this PRs blocking bug and is a much hotter path (every Windows save) than doctor.shs on-demand probes, so the regression risk of touching it outweighed the benefit in this already two-commit branch.

Tests

Red (pre-fix): tests/test_doctor_windows_fglock_1011.py::test_affected_build_with_zero_timeout_does_not_warn took 30.15s against a 15s bound on macOS (expected <1s).
Green (post-fix): same module, slowest test 0.32s.
Red (pre race-fix): the manufactured-race regression test failed (stray process left behind) against the first fix commit.
Green (post race-fix): same test passes in ~1.2s (the bounded retry firing as designed).
Did not run the repos full pytest suite -- CI is the full-matrix gate; ran the narrowed, targeted set listed in the commits.

🤖 Generated with Claude Code

[AI-generated]

fdaviddpt and others added 3 commits October 11, 2026 08:16
_remember_doctor_run_powershell_file's watchdog subshell inherited this
function's own stdout/stderr unredirected. Every real caller captures this
function's output via command substitution ($(...)) -- e.g.
_FGLOCK_TIMEOUT=$(_remember_doctor_fglock_timeout) -- and that read blocks
until every process holding the pipe's write end closes it, not just until
the real data arrives. The watchdog's sleep child kept that write end open
for the full timeout bound even after PowerShell had already answered and
the function had already printed its result, costing ~2x the bound per
probe on real windows-latest CI (PR #1024 logs: ~11s at a 5s bound, ~31s at
a 15s bound).

Fix redirects the watchdog's own output away from the caller's pipe, and
hands its sleep child's real PID back out through a small temp file so an
early PowerShell exit can kill that sleep directly too -- leaving no stray
process behind either way. A first attempt moved sleep out to be a direct
child of the function instead, but that made the watchdog subshell `wait`
on a sibling rather than its own child, which bash refuses and fires the
kill immediately regardless of bound; caught in self-review before this
reached CI.

Added tests/test_doctor_powershell_watchdog_stdout_leak_1011.py, which
drives the real function (extracted verbatim from doctor.sh) against a
stub powershell.exe on every OS, not only a real Windows host. Confirmed
red on main: a stub answering instantly still took the full bound (17.27s
against a 17s bound); green after the fix (0.16-0.36s). The hung-stub
positive control (kill-on-timeout) still fires at the bound (2.06s against
a 2s bound).

Audited scripts/lib-detach.sh's own Windows-save watchdog
(_remember_detach_windows) for the same pattern and found it already
unaffected -- it already redirects its own output (from #1002), confirmed
by direct measurement: all 24 of its existing stub-driven tests, several
using the same subprocess.run(capture_output=True) pipe-capture shape,
return in well under 1.5s against its own 15s bound.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Review finding on the #1011 watchdog fix: the watchdog subshell forks
its own `sleep` child and only afterwards writes that child's real PID
out to the handoff file the reap path reads. A caller reaping early can
land between those two lines, find the file still empty, and fall back
to a reap that can only touch the watchdog subshell itself -- not a
child it already forked -- leaving `sleep` orphaned for the rest of its
own bound.

Reproduced deterministically with a new test that extracts the real
function and inserts a 0.3s delay between the fork and the write
(test_a_manufactured_fork_to_pidfile_write_race_still_leaves_no_stray_sleep):
red before this commit (STRAY=1), green after.

Fix: one bounded 1s retry before falling back, closing the window a
real host can realistically race into without turning this into a
busy-poll whose margin would vary with host speed.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Two fragments for the curation pass:
- another data point for the worktree-isolation guard's "too complex to
  verify" refusal, this time on plain Python test source with no actual
  version-control syntax in it -- looks like a size/complexity heuristic
  rather than a content match, since splitting the identical final
  content across smaller edits let it through every time.
- lib-detach.sh's own Windows-save watchdog has the identical
  kill-the-subshell-not-the-sleep-child shape this branch's previous
  commit just fixed in doctor.sh, found by both reviewers but out of
  this task's audit scope (lib-detach.sh is confirmed unaffected by the
  blocking bug that scope covers) and on a much hotter path -- logged
  rather than fixed or filed.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@fdaviddpt
fdaviddpt merged commit 27fb76b into main Oct 11, 2026
19 checks passed
@fdaviddpt
fdaviddpt deleted the fix-1011-watchdog-stdout-inherit branch October 11, 2026 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant