Repository navigation
fix(bin): stop a watcher promptly when a take-over lands in its event wait - #7740
Conversation
On bash 3.2 a TERM is held until a running command substitution's child exits. The watcher's push-backend event wait ran as rec=$(...) and blocks for up to POLL, so a supervision-host take-over that landed there could not confirm the stop within its 5s bound. The unconfirmed take-over skipped restoring main's acknowledgement, the stopped watcher's close minted a fresh downtime generation, and the next watcher announced it as check: rearm-resurface with nothing queued, repeating every ~30s. Run the event wait through the same waited background process group the pane reads already use (watcher_capture, generalized to watcher_waited). The helper's own setup failures now return 2, which the event wait reads as an unusable event path rather than a full-budget wait. Because a stop now kills the wait before its own cleanup, the wait gets a watcher-owned scratch TMPDIR that the watcher removes after each wait and on exit, and the cleanup's group stop is silenced so bash's job-termination notice does not reach the arm's output. Add a fm-watch-arm regression case that takes over a watcher inside its herdr event wait and asserts a confirmed take-over, an unchanged acknowledged marker, a quiet fresh cycle, and no leftover wait scratch. Make the event-wait unit case assert the capability flag outside the wait, since the wait now runs in its own process.
|
Speaking as Kun's firstmate: thank you @vatayush, and welcome. This is a clear write-up, and the base-vs-fix live comparison on I read the diff on head My read:
Waiting on CI only. If CI comes back green and the PR stays mergeable, I expect to merge it next pass. Nothing is needed from you right now. |
|
CI is done, can we merge this now? |
|
Speaking as Kun's firstmate: thanks for checking in, @vatayush, and sorry for the wait. Your change is not the holdup. All of CI is green on Nothing is needed from you. My read hasn't changed: contract class restore, VISION aligns on every rule, the no-mistakes attestation binds this head, and it merges cleanly. I also ran your two test files on current main |
|
Speaking as Kun's firstmate: this is merged. Thank you @vatayush — really appreciate you taking the time on this. A watcher stop that lands inside the event wait now exits promptly on bash 3.2. |
… wait (kunchenguid#7740) * fix(bin): stop a watcher promptly when a stop lands in its event wait On bash 3.2 a TERM is held until a running command substitution's child exits. The watcher's push-backend event wait ran as rec=$(...) and blocks for up to POLL, so a supervision-host take-over that landed there could not confirm the stop within its 5s bound. The unconfirmed take-over skipped restoring main's acknowledgement, the stopped watcher's close minted a fresh downtime generation, and the next watcher announced it as check: rearm-resurface with nothing queued, repeating every ~30s. Run the event wait through the same waited background process group the pane reads already use (watcher_capture, generalized to watcher_waited). The helper's own setup failures now return 2, which the event wait reads as an unusable event path rather than a full-budget wait. Because a stop now kills the wait before its own cleanup, the wait gets a watcher-owned scratch TMPDIR that the watcher removes after each wait and on exit, and the cleanup's group stop is silenced so bash's job-termination notice does not reach the arm's output. Add a fm-watch-arm regression case that takes over a watcher inside its herdr event wait and asserts a confirmed take-over, an unchanged acknowledged marker, a quiet fresh cycle, and no leftover wait scratch. Make the event-wait unit case assert the capability flag outside the wait, since the wait now runs in its own process. * no-mistakes(test): Raise event-wait take-over regression POLL so it fails on base * no-mistakes(document): Document event-wait stop handling in watcher continuity --------- Co-authored-by: vatsa <[email protected]> (cherry picked from commit ffb2aca)
… wait (kunchenguid#7740) * fix(bin): stop a watcher promptly when a stop lands in its event wait On bash 3.2 a TERM is held until a running command substitution's child exits. The watcher's push-backend event wait ran as rec=$(...) and blocks for up to POLL, so a supervision-host take-over that landed there could not confirm the stop within its 5s bound. The unconfirmed take-over skipped restoring main's acknowledgement, the stopped watcher's close minted a fresh downtime generation, and the next watcher announced it as check: rearm-resurface with nothing queued, repeating every ~30s. Run the event wait through the same waited background process group the pane reads already use (watcher_capture, generalized to watcher_waited). The helper's own setup failures now return 2, which the event wait reads as an unusable event path rather than a full-budget wait. Because a stop now kills the wait before its own cleanup, the wait gets a watcher-owned scratch TMPDIR that the watcher removes after each wait and on exit, and the cleanup's group stop is silenced so bash's job-termination notice does not reach the arm's output. Add a fm-watch-arm regression case that takes over a watcher inside its herdr event wait and asserts a confirmed take-over, an unchanged acknowledged marker, a quiet fresh cycle, and no leftover wait scratch. Make the event-wait unit case assert the capability flag outside the wait, since the wait now runs in its own process. * no-mistakes(test): Raise event-wait take-over regression POLL so it fails on base * no-mistakes(document): Document event-wait stop handling in watcher continuity --------- Co-authored-by: vatsa <[email protected]> (cherry picked from commit ffb2aca)
Intent
Contribute the fix to upstream as a PR to the firstmate project.
The bug: on macOS with the system /bin/bash 3.2, a supervision-host take-over (bin/fm-watch-arm.sh --take-over) of a watcher that is inside its herdr event wait cannot confirm the stop within its 5-second bound, because event_wait_or_sleep runs the up-to-POLL event wait as a $(...) command substitution and bash 3.2 holds the TERM until that child exits. The unconfirmed take-over skips restoring main's acknowledgement, the stopped watcher's close mints a fresh downtime generation over an already-acknowledged recovery episode, and the next watcher announces it as "check: rearm-resurface" with an empty queue; the cycle repeats roughly every 30 seconds. The fix runs the event wait through the same waited background process group the watcher already uses for pane reads (watcher_capture), generalized to a waited helper, maps the helper's own setup failures to rc 2 for the event wait, and silences bash's job-termination notice in watcher_cleanup's stop; a regression case in tests/fm-watch-arm.test.sh takes over a watcher inside its herdr event wait and asserts a confirmed take-over, an unchanged acknowledged marker, and a quiet fresh cycle.
What Changed
bin/fm-watch.sh: the herdr event wait inevent_wait_or_sleepno longer runs inside$(...). It now goes throughwatcher_waited, which is the oldwatcher_capturepane-read helper turned into a general waited background process group. On bash 3.2, the old command substitution held a HUP/TERM until the wait finished, which could take up to POLL. Now a take-over's stop is handled right away. The helper's own setup failures (mktemp, setpgid mismatch, stop failure) return rc 2, which the event wait treats as an unusable event path.watcher_captureis now a thin wrapper aroundwatcher_waited.TMPDIR, owned by the watcher. It is removed after each wait and inwatcher_cleanup, so a stop that kills the wait mid-flight doesn't leave its reader FIFO behind.watcher_cleanupalso hides bash's "Terminated" job notice when it stops a waited group still in flight.tests/fm-watch-arm.test.shtakes over a watcher while it is inside its herdr event wait. It checks that the take-over is confirmed, the acknowledged downtime marker is unchanged, and the fresh cycle stays quiet.tests/fm-supervision-events.test.shnow records the capability-confirmed flag to a file, because the wait now runs in a child process.docs/watcher-continuity.mdnotes that stops are now handled during the event wait too.🤖 Generated with Claude Code
Risk Assessment
✅ Low: The change sends the event wait through the same waited process-group mechanism the watcher already uses for pane reads. The rc mapping still matches every caller: watcher_capture callers only test for success or failure, and event_wait_or_sleep reads 2 as an unusable event path. The watcher-owned TMPDIR scratch only redirects the backends' own mktemp calls, and recovery-marker writes in cleanup use absolute paths. The regression test checks behavior without grepping implementation source.
Testing
I ran the new regression case on its own: it passes on HEAD and fails against the base bin/fm-watch.sh. The modified supervision-events test also passes. These are test-suite results, not live runs. Then I provisioned a real Herdr lab session (fm-lab-*) and ran the real arm and watcher on /bin/bash 3.2 with the real event reader against a marked disposable lab home. With the fix, a --take-over issued while the watcher sat inside its 40s event wait confirmed within 1-2s (ledger reason=taken-over). The acknowledged marker was unchanged, the fresh cycle stayed running with no rearm-resurface line, the stopped watcher printed no "Terminated" notice, and no wait scratch was left behind. With the base watcher, the take-over only attached after about 50s, the cycle failed, and the marker flipped from acked to pending:downtime. The lab session and homes were torn down, and the worktree is clean. This is CLI-only behavior, so the evidence is transcripts, not screenshots.
Evidence: Live Herdr take-over with fix (run 1)
Source: Live Herdr take-over with fix (run 1)
Evidence: Live Herdr take-over with fix (run 2, includes stopped watcher output: no Terminated notice)
Source: Live Herdr take-over with fix (run 2, includes stopped watcher output: no Terminated notice)
Evidence: Live Herdr take-over with base 842eab1 watcher (bug reproduced: 50s, marker reopened)
Source: Live Herdr take-over with base 842eab1 watcher (bug reproduced: 50s, marker reopened)
Evidence: Live driver script
Source: Live driver script
Evidence: Regression case passes on fix
Source: Regression case passes on fix
Evidence: Regression case fails on base watcher
Source: Regression case fails on base watcher
Evidence: Base vs fix live comparison
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
✅ **Review** - passed
✅ No issues found.
🔧 **Test** - 1 issue found → auto-fixed ✅
tests/fm-watch-arm.test.sh:1330- The new regression case test_take_over_confirms_a_watcher_stopped_inside_its_event_wait (FM_POLL=12) passes against the unfixed bin/fm-watch.sh from 842eab1 on this macOS bash 3.2 host. The arm's 50×0.1s stop loop runs a ps fork on each iteration, so it stretches to about 11-12s and outlasts the 12s POLL. On base the held TERM was released just inside that stretched bound: the base ledger shows the take-over starting at 924 and confirming at 936. With FM_POLL=40 (and a longer wait for the start line) the same case fails on base and passes on the fix, so raising the POLL (and the matching start-line wait ceiling) would make this a real fail-before / pass-after regression.bash tests/fm-supervision-events.test.sh, all assertions passed). That is not a live product run, so it did not establish a li…bash tests/fm-watch-arm.test.shcut down to onlytest_take_over_confirms_a_watcher_stopped_inside_its_event_wait, fixed watcher, 3 runs (all ok)Same single case with bin/fm-watch.sh restored from 842eab1 (base): still passes at FM_POLL=12, but its ledger shows the stop took about 12sCopy of the case with FM_POLL=40 and a longer wait for the start line, against base bin/fm-watch.sh: not ok (take-over unconfirmed, attached-cycle-ended, acknowledged marker replaced by pending:downtime)Same FM_POLL=40 copy against the fixed watcher: ok (taken-over in the same second, quiet fresh cycle)bash tests/fm-supervision-events.test.sh(all assertions passed, including the updated per-wait capability check)Full stdout and stderr capture of the fixed take-over case, checked for a 'Terminated' job notice (0 matches)Hand-run live check: real arm and watcher with FM_POLL=40 and a fake reader that streams a blocked edge after 3s; the watcher woke with the stale/blocked reason after 5s and left no scratch dirs behind🔧 Fix applied.
✅ Re-checked - no issues remain.
/bin/bash tests/zz-evt-only.test.sh(a temporary copy of tests/fm-watch-arm.test.sh that runs only test_take_over_confirms_a_watcher_stopped_inside_its_event_wait), run on HEAD: passSame filtered case with bin/fm-watch.sh swapped to 842eab1 (base), then restored withgit checkout -- bin/fm-watch.sh: fails with 'the take-over did not own a fresh cycle within 20s'/bin/bash tests/fm-supervision-events.test.sh(the modified test): all assertions passLive Herdr lab:bin/fm-herdr-lab.sh provision fm-lab-evtwait-29293-16627, plusworkspace createfor pane w1:p1 throughbin/fm-herdr-lab.sh runLive driver live-herdr-takeover-driver.sh (fix, twice). Uses a marked lab home frombin/fm-lab-home.sh create, starts the realbin/fm-watch-arm.shon /bin/bash 3.2 with FM_POLL=40 and the real capability probe and herdr-eventwait.py reader, acknowledges a wake through real fm-wake-drain.sh, waits for the reader subscription, then runsfm-watch-arm.sh --take-over <owner>Same live driver with base 842eab1 bin/fm-watch.sh (restored afterward)bin/fm-herdr-lab.sh teardown fm-lab-evtwait-29293-16627; lab homes removed with rm -rf✅ **Document** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.
Local suite results (macOS, /bin/bash 3.2.57)
Run with
bin/fm-test-run.sh --changed(26 suites) on a host that was also running other work (load average 5-11 on 8 cores), then each failing suite rerun alone on this branch and on unpatched main (d96bff0).fm-supervision-events,fm-watch-recovery-loop,fm-watch-inbox-meta,fm-watch-wedge-cap,fm-backend-t3codeandfm-turnend-guard.bin/fm-lint.shpasses with the pinned ShellCheck 0.11.0 and actionlint 1.7.12 (the pipeline's lint step could not find ShellCheck on its PATH, so it was checked locally on the pipeline head).Failures that are pre-existing or environmental, not caused by this change:
fm-watch-arm:test_append_wakes_handling_successor ... identicalfails with "re-append was not byte-identical to the inherited row", identically on unpatched main. The suite stops at its first failure, so every other case was run on its own: all 39 pass on this branch, and the 38 that exist on main pass there.fm-guard-stale-banner: "an extension-owned hand-off with a live tagged session must stay silent" fails identically on unpatched main.fm-watch-wedge-cap-unconfigured-parity: fails on both trees withcap_args[@]: unbound variable(an empty array underset -uon bash 3.2).fm-wake-queue: "the oversized unread status line was truncated or omitted" followsgrep: out of memory; the host's/usr/bin/grep(BSD grep 2.6.0) fails standalone on that 20000-character-Fxpattern.fm-watch-checkpoint: failed only under load; passes alone on both trees.fm-watch-triage: exceeded the runner's 1500s bound under load. Alone on this branch it failed one case near the bound at load ~10 (test_reheld_captain_call_starts_its_own_resurface_window); that case passes when run by itself on this branch and on main. Its fixtures use tmux panes, so the event wait is not on their path.fm-watcher-lock: exceeded the 1500s bound, and a solo rerun on this branch hung inlock-stale-concurrency(a test child blocked reading a FIFO, "Interrupted system call") until it was stopped. The same hang was seen on this host before this change was written. That case drives onlybin/fm-wake-lib.sh's lock, which this change does not touch. It was not rerun on main.CI note
The "Behavior portable serial 1" job (tests/fm-supervision-host.test.sh) was cancelled by the CI provider at the job's 30-minute timeout rather than failing: every case it reached passed, and it was still progressing when cancelled. The same shard has been cancelled at that timeout on several recent main runs. It needs a maintainer rerun; every other required check passed.