You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: bound watcher cleanup wait on the downtime-marker lock - #5732
Fix the flaky tests/fm-watch-triage.test.sh failure in portable serial CI shard 1. Flaky tests are reliability bugs: fix their causes, never add retries, sleeps, or looser thresholds. Sightings: ~20min stalls failing different subtests ('acknowledged external-decision surface replayed 1 wakes' in run 34439141091; 'captain-held-churn pane churn re-alarmed a parked worker 1 time' in run 34441064828) and 'watcher pid did not exit within 10s of TERM' in run 36175955361 with ~1000s runtime.
Root cause found: a real product defect. watcher_cleanup (the EXIT trap) publishes the downtime marker via fm_recovery_transition release-lock -> _fm_recovery_marker_publish -> an unbounded fm_lock_acquire_wait on state/.watcher-down.lock. A live foreign holder of that lock (fm-watch-arm.sh clear-stale-lock, fm-wake-drain, afk-return, fm_wake_append writers) strands a single-TERM'd watcher inside its own trap indefinitely; bash only kills it when a second TERM hits termsig_sighandler's kill_shell short-circuit, matching the CI evidence of death ~0.1s after TERM#2. fm-watch-arm.sh stop sends exactly one TERM and waits ~5s, so production stops wedge identically.
Fix: new in-process bounded wait fm_lock_acquire_wait_max (SECONDS deadline) plus an optional bound parameter on fm_recovery_transition honoured by publish, release-lock, release-lock-existing, and clear-stale-lock; watcher_cleanup passes FM_WATCHER_CLEANUP_LOCK_BOUND (default 2s; parsed as decimal so 08 means 8, and empty/non-numeric/zero including 00 fall back to 2). On timeout the publish is skipped and the singleton is retained as dead-pid stale evidence via the existing 'recovery state could not be persisted; retaining stale lock evidence' path; the next arm's clear-stale-lock republishes and removes it. All existing fm_recovery_transition callers pass no bound and keep unbounded waits.
Regression coverage: test_term_stops_a_watcher_whose_cleanup_marker_lock_is_held drives a real watcher with .watcher-down.lock held by a live foreign subshell, sends exactly one TERM, asserts exit within the suite's standard 100-tick budget, asserts the retained stale singleton, then exercises clear-stale-lock recovery and ack_stopped_cycle after release. test_cleanup_marker_lock_bound_is_decimal_with_zero_default pins decimal parsing by keeping the lock contended until cleanup's first acquire attempt is observed (the holder serves its pid record through a FIFO), distinguishing an 8s bound from the 2s default and a zero spelling from an instant timeout. Verified red on unfixed code and green after the fix.
Also established for the report: ~1000s is the suite's normal runtime (907s and 1020s on recent green runs; the failing test was last in the file), so there is no post-failure hang to fix. The earlier replay/churn subtest failures are consistent with a watcher surviving ~11s past its TERM across a phase boundary but are not independently proven; the new regression pins the shared single-TERM guarantee. Deliberately out of scope: the unbounded waits on the poll path (native termsig handling makes them TERM-safe) and wake()'s trap '' TERM window (a separate defect class producing a different survived-TERM-then-KILL signature not seen in this failure).
What Changed
Bound the watcher’s EXIT cleanup wait for the downtime-marker lock, preventing a live lock holder from indefinitely blocking a single-TERM stop. A timed-out cleanup retains the stale singleton for recovery on the next arm.
Add an optional, positive decimal cleanup bound (FM_WATCHER_CLEANUP_LOCK_BOUND, default 2 seconds) while leaving other recovery-transition callers unbounded.
Add regression coverage for a held marker lock, stale-lock recovery, and decimal and zero bound values; document the behavior.
Risk Assessment
✅ Low: The bounded wait is confined to watcher EXIT cleanup, preserves unbounded behavior for existing callers, and the revised fixture observes a failed acquire before releasing contention.
Testing
All three focused tests passed. Isolated live watcher runs confirmed single-TERM exit, stale-lock recovery, decimal cleanup bounds, and normal poll cleanup; CLI transcripts were captured. An initial private-tmux attempt could not create its socket because the worktree path exceeded the socket-name limit, so the live checks were rerun with the standalone watcher and no tmux claim.
Live validation: ✅ go - 3 of 3 scenarios driven live against the product
Scenario
Result
Live
Evidence
Stop a watcher with one TERM while a foreign process holds its downtime-marker lock; retain and later clear stale singleton evidence
watcher: recovery state could not be persisted; retaining stale lock evidence
ok - TERM stops a watcher whose downtime-marker lock is held, retaining stale evidence
⚠️tests/fm-watch-triage.test.sh:4626 - The round-2 contention fixture sends TERM before arming the FIFO at line 4578. On a loaded CI worker, cleanup can try the ordinary pid record and time out before the holder installs the FIFO; the 00 case then fails without observing the intended contention. Install and confirm the FIFO before sending TERM, while keeping the holder alive until cleanup reads it.
⚠️tests/fm-watch-triage.test.sh:6470 - The new FM_TEST_ONLY_LATE mode is not needed for the stated regression: the existing FM_TEST_ONLY selector immediately above can already invoke these late-defined tests. No intent requirement calls for a second selector. Remove this mode.
🔧 Fix applied.
✅ Re-checked - no issues remain.
⚠️tests/fm-watch-triage.test.sh:4741 - The round-3 fixture uses 00:0. After the FIFO read marks contention, the holder can release the lock before the watcher's first fm_lock_try_acquire finishes. That attempt can then reclaim the lock, so the test passes even if 00 incorrectly means an immediate timeout. Keep the holder contested through a completed failed acquire before allowing release; the immediate-release path is at line 4678.
🔧 Fix applied.
✅ Re-checked - no issues remain.
🔧 **Test** - 1 issue found ✅
⚠️ live validation verdict: inconclusive (1 of 3 scenarios were driven live against the product); untested: Use 00 as the default cleanup bound and parse 08 as eight decimal seconds while cleanup contends on the lock, Stop a watcher blocked inside a poll with one TERM and run its cleanup
Live validation: ⚠️ inconclusive - 1 of 3 scenarios driven live against the product
Scenario
Result
Live
Evidence
Stop a watcher with one TERM while another process holds the downtime-marker lock; retain stale evidence and recover after release
✅ pass
live
watch-held-marker-live.txt records exit within 10 seconds, the retained singleton PID, and subsequent marker publication and singleton removal.
Use 00 as the default cleanup bound and parse 08 as eight decimal seconds while cleanup contends on the lock
⏸️ untested
no
The prior payload records only watcher subprocess tests with a fixture pane, not a live product run of this scenario. Drive the cleanup-bound behavior against a real running watcher and capture eviden…
Stop a watcher blocked inside a poll with one TERM and run its cleanup
⏸️ untested
no
The prior payload records only a watcher subprocess test with fixtures, not a live product run of this scenario. Drive a real running watcher blocked inside a poll and capture evidence to establish a…
Manually ran a watcher in a disposable lab home, held its downtime-marker lock, sent one TERM, and cleared the retained stale singleton after releasing the lock.
✅ No issues found.
Live validation: ✅ go - 2 of 2 scenarios driven live against the product
Scenario
Result
Live
Evidence
Send one TERM while a foreign process holds the downtime-marker lock; the watcher exits and leaves recoverable stale singleton evidence
✅ pass
live
Live held-lock stop and recovery
Set cleanup bounds to 00 and 08 while the marker lock is contested; cleanup completes after the lock is released
✅ pass
live
Live decimal-bound checks; targeted decimal-bound regression also passed
Ran a real watcher against a private tmux primary in a disposable lab home; held its downtime-marker lock, sent one TERM, then cleared the retained stale singleton.
Re-ran the disposable-lab watcher with cleanup bounds 00 and 08, confirming contention before releasing the lock. The first manual fixture had a Bash 3.2 setup error; it was corrected and both cases were re-driven.
✅ No issues found.
Live validation: ✅ go - 3 of 3 scenarios driven live against the product
Scenario
Result
Live
Evidence
Stop a watcher with one TERM while a foreign process holds its downtime-marker lock; retain and later clear stale singleton evidence
Intent
Fix the flaky tests/fm-watch-triage.test.sh failure in portable serial CI shard 1. Flaky tests are reliability bugs: fix their causes, never add retries, sleeps, or looser thresholds. Sightings: ~20min stalls failing different subtests ('acknowledged external-decision surface replayed 1 wakes' in run 34439141091; 'captain-held-churn pane churn re-alarmed a parked worker 1 time' in run 34441064828) and 'watcher pid did not exit within 10s of TERM' in run 36175955361 with ~1000s runtime.
Root cause found: a real product defect. watcher_cleanup (the EXIT trap) publishes the downtime marker via fm_recovery_transition release-lock -> _fm_recovery_marker_publish -> an unbounded fm_lock_acquire_wait on state/.watcher-down.lock. A live foreign holder of that lock (fm-watch-arm.sh clear-stale-lock, fm-wake-drain, afk-return, fm_wake_append writers) strands a single-TERM'd watcher inside its own trap indefinitely; bash only kills it when a second TERM hits termsig_sighandler's kill_shell short-circuit, matching the CI evidence of death ~0.1s after TERM#2. fm-watch-arm.sh stop sends exactly one TERM and waits ~5s, so production stops wedge identically.
Fix: new in-process bounded wait fm_lock_acquire_wait_max (SECONDS deadline) plus an optional bound parameter on fm_recovery_transition honoured by publish, release-lock, release-lock-existing, and clear-stale-lock; watcher_cleanup passes FM_WATCHER_CLEANUP_LOCK_BOUND (default 2s; parsed as decimal so 08 means 8, and empty/non-numeric/zero including 00 fall back to 2). On timeout the publish is skipped and the singleton is retained as dead-pid stale evidence via the existing 'recovery state could not be persisted; retaining stale lock evidence' path; the next arm's clear-stale-lock republishes and removes it. All existing fm_recovery_transition callers pass no bound and keep unbounded waits.
Regression coverage: test_term_stops_a_watcher_whose_cleanup_marker_lock_is_held drives a real watcher with .watcher-down.lock held by a live foreign subshell, sends exactly one TERM, asserts exit within the suite's standard 100-tick budget, asserts the retained stale singleton, then exercises clear-stale-lock recovery and ack_stopped_cycle after release. test_cleanup_marker_lock_bound_is_decimal_with_zero_default pins decimal parsing by keeping the lock contended until cleanup's first acquire attempt is observed (the holder serves its pid record through a FIFO), distinguishing an 8s bound from the 2s default and a zero spelling from an instant timeout. Verified red on unfixed code and green after the fix.
Also established for the report: ~1000s is the suite's normal runtime (907s and 1020s on recent green runs; the failing test was last in the file), so there is no post-failure hang to fix. The earlier replay/churn subtest failures are consistent with a watcher surviving ~11s past its TERM across a phase boundary but are not independently proven; the new regression pins the shared single-TERM guarantee. Deliberately out of scope: the unbounded waits on the poll path (native termsig handling makes them TERM-safe) and wake()'s trap '' TERM window (a separate defect class producing a different survived-TERM-then-KILL signature not seen in this failure).
What Changed
FM_WATCHER_CLEANUP_LOCK_BOUND, default 2 seconds) while leaving other recovery-transition callers unbounded.Risk Assessment
✅ Low: The bounded wait is confined to watcher EXIT cleanup, preserves unbounded behavior for existing callers, and the revised fixture observes a failed acquire before releasing contention.
Testing
All three focused tests passed. Isolated live watcher runs confirmed single-TERM exit, stale-lock recovery, decimal cleanup bounds, and normal poll cleanup; CLI transcripts were captured. An initial private-tmux attempt could not create its socket because the worktree path exceeded the socket-name limit, so the live checks were rerun with the standalone watcher and no tmux claim.
Evidence: Live held-lock stop and recovery
Source: Live held-lock stop and recovery
Evidence: Live decimal-bound behavior
Source: Live decimal-bound behavior
Evidence: Live poll interruption
Source: Live poll interruption
Evidence: Focused held-lock regression
Source: Focused held-lock regression
Evidence: Focused decimal-bound regression
Source: Focused decimal-bound regression
Evidence: Focused poll-interruption regression
Source: Focused poll-interruption regression
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
✅ No issues found.
tests/fm-watch-triage.test.sh:4626- The round-2 contention fixture sends TERM before arming the FIFO at line 4578. On a loaded CI worker, cleanup can try the ordinary pid record and time out before the holder installs the FIFO; the00case then fails without observing the intended contention. Install and confirm the FIFO before sending TERM, while keeping the holder alive until cleanup reads it.tests/fm-watch-triage.test.sh:6470- The newFM_TEST_ONLY_LATEmode is not needed for the stated regression: the existingFM_TEST_ONLYselector immediately above can already invoke these late-defined tests. No intent requirement calls for a second selector. Remove this mode.🔧 Fix applied.
✅ Re-checked - no issues remain.
tests/fm-watch-triage.test.sh:4741- The round-3 fixture uses00:0. After the FIFO read marks contention, the holder can release the lock before the watcher's firstfm_lock_try_acquirefinishes. That attempt can then reclaim the lock, so the test passes even if00incorrectly means an immediate timeout. Keep the holder contested through a completed failed acquire before allowing release; the immediate-release path is at line 4678.🔧 Fix applied.
✅ Re-checked - no issues remain.
🔧 **Test** - 1 issue found ✅
FM_TEST_ONLY_LATE=test_term_stops_a_watcher_whose_cleanup_marker_lock_is_held bash tests/fm-watch-triage.test.shFM_TEST_ONLY_LATE=test_cleanup_marker_lock_bound_is_decimal_with_zero_default bash tests/fm-watch-triage.test.shFM_TEST_ONLY_LATE=test_term_stops_a_watcher_blocked_inside_a_poll bash tests/fm-watch-triage.test.shManually ran a watcher in a disposable lab home, held its downtime-marker lock, sent one TERM, and cleared the retained stale singleton after releasing the lock.✅ No issues found.
FM_TEST_ONLY=test_term_stops_a_watcher_whose_cleanup_marker_lock_is_held bash tests/fm-watch-triage.test.shFM_TEST_ONLY=test_cleanup_marker_lock_bound_is_decimal_with_zero_default bash tests/fm-watch-triage.test.shFM_TEST_ONLY=test_term_stops_a_watcher_blocked_inside_a_poll bash tests/fm-watch-triage.test.shRan a real watcher against a private tmux primary in a disposable lab home; held its downtime-marker lock, sent one TERM, then cleared the retained stale singleton.Re-ran the disposable-lab watcher with cleanup bounds00and08, confirming contention before releasing the lock. The first manual fixture had a Bash 3.2 setup error; it was corrected and both cases were re-driven.✅ No issues found.
FM_TEST_ONLY=test_term_stops_a_watcher_whose_cleanup_marker_lock_is_held bash tests/fm-watch-triage.test.shFM_TEST_ONLY=test_cleanup_marker_lock_bound_is_decimal_with_zero_default bash tests/fm-watch-triage.test.shFM_TEST_ONLY=test_term_stops_a_watcher_blocked_inside_a_poll bash tests/fm-watch-triage.test.shRan a real watcher with a foreign marker-lock holder, sent one TERM, then cleared the retained singleton.Ran real watchers with cleanup bounds00and08while holding the marker lock for one and four seconds respectively.Sent one TERM to a real watcher during polling and inspected its cleanup state.⏭️ **Document** - skipped
Step was skipped.
✅ **Lint** - passed
✅ No issues found.
✅ No issues found.
✅ No issues found.
✅ **Push** - passed
✅ No issues found.
✅ No issues found.
✅ No issues found.