Repository navigation
fix(bin): stop the watch arm self-killing on a repeat teardown signal - #13
Merged
Merged
Conversation
added 3 commits
September 5, 2026 19:22
handle_arm_signal and handle_attached_signal reset HUP/TERM/INT to their default disposition before finishing cleanup. A second copy of the trapped signal arriving while that cleanup is still running (blocked in the handler's own wait, or in cycle_log_append's lock-retry loop) finds the disposition already at default, and bash's pending-trap redelivery resends the signal to the process at SIG_DFL, killing the arm before cleanup_child and exit ever run - the exact failure captured in the run_pending_traps warning that took fleet supervision down. Ignoring the signal instead of resetting it removes the vulnerable state: the arm is already tearing down and exits with the correct status a few lines later regardless of a repeat delivery. Verified against the real arm and watcher scripts (no test doubles) using a lock-contention race harness: 8/8 red against the prior code, 8/8 green with the fix, with no orphaned watcher and no stale lock file in either outcome. Added a colocated regression test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
Fix bin/fm-watch-arm.sh killing itself, and its watcher child, on signal teardown, which had left fleet supervision off. Evidence: state/.claude-autoarm-output.RKMLGH showed the arm start a healthy watcher, then die on its own re-raised SIGTERM with bash's run_pending_traps warning ('bad value in trap_list[15]: 0x0' / 'signal handler is SIG_DFL, resending 15 (SIGTERM) to myself'). Root cause: handle_arm_signal (and the sibling handle_attached_signal) reset HUP/TERM/INT to their default disposition (trap - HUP TERM INT) before finishing cleanup. If a second copy of the trapped signal lands while cleanup is still running - blocked in the handler's own wait "$child", or in cycle_log_append's lock-retry loop - bash finds the disposition already at default and its pending-trap redelivery resends the signal to the process at SIG_DFL, killing it before cleanup_child/exit ever run. Fix: trap '' (ignore) instead of trap - (reset to default) in both handlers, since the process is already exiting with the correct status a few lines later regardless of a repeat delivery. The one line at the very end of the script (post-confirmation-loop cleanup, not inside a signal handler) intentionally stays trap - since it isn't in the vulnerable state. Constraints given by the task: do not redesign supervision, add a new watcher owner, or introduce a supervisor-of-the-supervisor - this had to stay a signal-handling bug fix in the existing functions. Do not change the Stop hook contract, bin/fm-claude-stop-autoarm.sh, the beacon format, or the lock protocol (not touched). Do not touch anything under projects/ (not touched). bin/fm-watch.sh itself was confirmed NOT to be the defect (real watcher exits via 'trap exit 1' HUP INT TERM near-instantly; the race is purely in the arm's own trap-reset timing) and was deliberately left unmodified. Verification performed: reproduced the self-kill 10/10 in a minimal bash harness (isolated repro scripts, no repo code) to confirm the run_pending_traps mechanism, then proved the fix 8/8 red-to-green against the REAL, unmodified bin/fm-watch-arm.sh and bin/fm-watch.sh using a lock-contention race harness (holding the arm's own state/.watch-cycle-exits.lock briefly, which is real production contention cycle_log_append already retries against, to open the timing window a fast-dying real watcher would otherwise close in milliseconds) - no test doubles or stub scripts were used for the shipped regression test. Confirmed in that harness: the signalled arm tears down its child watcher, exits with the correct status for the signal, leaves no live watcher process and no stale state/.watch.lock, and does not die of its own re-raised signal. Added a colocated regression test, tests/fm-watcher-lock.test.sh:test_arm_survives_a_second_signal_during_its_own_cleanup, placed next to the existing sibling test_arm_hup_cleans_child_and_temp_output; confirmed it fails against the pre-fix code and passes with the fix. Ran both tests/fm-watcher-lock.test.sh (34 cases) and tests/fm-watch-arm.test.sh (14 cases) fully green, and bin/fm-lint.sh (pinned shellcheck) clean. All work is committed as a single commit (cfc4984) on branch fm/fix-watch-arm-signal-selfkill with no agent added as commit co-author.
What Changed
handle_arm_signalandhandle_attached_signalinbin/fm-watch-arm.shnow ignore HUP/TERM/INT (trap '') instead of resetting them to their default disposition (trap -), so a second copy of the trapped signal arriving mid-cleanup — while the handler is blocked inwait "$child"or incycle_log_append's lock retry — is no longer resent at SIG_DFL and no longer kills the arm beforecleanup_childandexitrun. The trailingtrap -outside the signal handlers is unchanged.tests/fm-watcher-lock.test.sh:test_arm_survives_a_second_signal_during_its_own_cleanup, which runs the real arm and watcher, holds the arm's ownstate/.watch-cycle-exits.lockto widen the teardown window, sends two TERMs, and asserts exit status 143, anarm-interruptedcycle-log row, no surviving child watcher, and no stalestate/.watch.lock.tests/fm-watcher-lock.test.shcoverage sentence indocs/watcher-continuity.mdto name the repeat-signal teardown case.Risk Assessment
✅ Low: The change is a two-token signal-disposition fix in two colocated handlers whose mechanism I verified against bash's run_pending_traps semantics, backed by a genuine behavioral regression test, with a comment-only fix round and full conformance to the stated intent; the only findings are hygiene notes in the new test.
Testing
I ran the two suites that cover the changed code —
tests/fm-watcher-lock.test.sh(34 cases, home of the new regression) andtests/fm-watch-arm.test.sh(14 cases) — both fully green, and confirmed the new case is a genuine regression by checking the base commit'sbin/fm-watch-arm.shback in (it fails) and the target version back in (it passes). Because a passing unit test isn't itself proof of the user-visible behaviour, I also drove the real arm and real watcher through the actual failure scenario and captured the operator-facing surfaces: the arm's stdout (what lands instate/.claude-autoarm-output.*), its exit status, thestate/.watch-cycle-exits.logledger, the child watcher's liveness, and the singleton lock. Pre-fix, six of six runs ended with the handler abandoned mid-cleanup and no ledger row — the silent death described in the intent; post-fix, six of six ran the teardown to completion with the correct TERM status, a fullarm-interruptedledger row, the child watcher reaped and the lock released. Worth noting for whoever reads the transcript: on this host (bash 5.2.37, arm64) the pre-fix runs did not print bash'srun_pending_trapswarning that appeared in the originalstate/.claude-autoarm-output.RKMLGH— that diagnostic line is emitted inconsistently — but the observable damage it accompanied reproduced deterministically, so the evidence rests on the missing ledger row rather than the warning text. This is a CLI/daemon change with no rendered surface, so the evidence is CLI transcripts rather than screenshots. The transient filtered test runner I used for the red/green pass was removed;git statusis clean at cffe5a0 and no watcher processes were orphaned.Evidence: Before/after transcript: real arm under a repeat SIGTERM during its own cleanup
Source: Before/after transcript: real arm under a repeat SIGTERM during its own cleanup
Evidence: Post-fix run detail (arm stdout, exit status, cycle ledger, watcher + lock state)
Source: Post-fix run detail (arm stdout, exit status, cycle ledger, watcher + lock state)
Evidence: Pre-fix run detail — handler abandoned, no cycle-ledger row
Source: Pre-fix run detail — handler abandoned, no cycle-ledger row
Evidence: Regression test red-to-green transcript
Source: Regression test red-to-green transcript
Evidence: Repro harness used for the manual end-to-end check (re-runnable)
Source: Repro harness used for the manual end-to-end check (re-runnable)
Evidence: Cycle-ledger row the fix restores (post-fix, was absent pre-fix)
Follow-ups
Code review during validation surfaced the same repeat-signal self-kill shape in two other supervisors:
bin/fm-supervise-daemon.sh'scleanup()andbin/fm-remote-job-worker.sh'sworker_supervisor_shutdown(), both reachable from a real repeat-TERM source in this repo's multi-pass process reaper. Deliberately left out of this PR: it's scoped to a signal-handling fix confined to the watch arm, validation had already started against that scope, and these are independent, already-live defects rather than corrections needed to make this fix correct. Filed as a separate follow-up task (fix-daemon-trap-selfkill) rather than dropped.Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
bin/fm-supervise-daemon.sh:1456- The same self-kill shape the arm was just fixed for is still live in two sibling supervisors, and it is reachable from a real repeat-TERM source in this repo.cleanup()(registeredtrap cleanup TERM INTat bin/fm-supervise-daemon.sh:1470) doestrap - TERM INTas its first statement, then blocks acrosswedge_alarm_stop_active_notifier(sleep 0.2+wait),escalate_flush, andwait "$WATCHER_PID"before it ever reachesfm_lock_release "$LOCK"andrm -f "$PIDFILE". bin/fm-remote-job-worker.sh:737 (worker_supervisor_shutdown) is identical:trap - HUP INT TERMthenkill -TERM "$pid"; wait "$pid". Concrete path: bin/fm-teardown.sh:1428reap_task_worktree_processesloops up tomax_passes=3, sendingkill -TERM "$pid"(bin/fm-teardown.sh:1470) to still-present pids on each pass — the second TERM lands whilecleanupis blocked inwait "$WATCHER_PID", bash'srun_pending_trapsfindstrap_list[15] == DEFAULT_SIGand re-raises to a SIG_DFL process. Result: the daemon dies without killing WATCHER_PID (orphaned supervision watcher), without flushing buffered escalations, and without releasing the singleton lock or PIDFILE. Remedy is the same one-token change (trap ''), but it EXTENDS this change into two files the stated intent scoped out ("this had to stay a signal-handling bug fix in the existing functions"), so it needs your authorization rather than an in-review fix — the remedy, not the defect, is what needs the decision.bin/fm-watch-arm.sh:598- The intent states this line "isn't in the vulnerable state", which is accurate for the pending-trap redelivery mechanism (it is not inside a handler, sorun_pending_trapsnever re-raises here) but leaves a longer plain window than the wording suggests. Aftertrap - HUP TERM INTthe confirmation-timeout path still runscleanup_child,wait "$child", andcycle_log_append ... confirmation-timeout.cleanup_childTERMs only the watcher bash, not its foregroundsleep "$POLL"(bin/fm-watch.sh:796/812/829, FM_POLL default 15), and bash defers a trap across a foreground child — sowait "$child"can block ~15s with HUP/TERM/INT at SIG_DFL. A single TERM in that window kills the arm with noconfirmation-timeoutledger row and nowatcher: FAILEDline: the same silent-death symptom class the fix targets, from a different trigger. This is pre-existing and explicitly scoped out, so flagging only —trap ''here would be equally safe since the next statement isexit 1, but that is your call.bin/fm-watch-arm.sh:473- Tradeoff note on the chosen fix, not a defect. SIG_IGN makes the arm uninterruptible by HUP/TERM/INT for the entire teardown, and that teardown is bounded by the child's exit, which is not always fast:kill -TERM "$child"targets the watcher bash only, so if the watcher is inside a foregroundsleep "$POLL"(default 15s) orsleep "$SIGNAL_GRACE"(bin/fm-watch.sh:1077, default 30s), bash defers itstrap 'exit 1'until that sleep returns and the arm sits inwait "$child"for that long, ignoring every further signal. Pre-fix a repeat signal ended the process (badly — that was the bug); post-fix only SIGKILL does. Callers escalate to KILL (bin/fm-teardown.sh:1502 after a 1s grace), so nothing hangs forever, and the trade is clearly the right one — recording it because arm teardown latency under an individually-targeted TERM is now visibly longer than before.bin/fm-watch-arm.sh:473- Diagnostic-string note for whoever next reads astate/.claude-autoarm-output.*file. bash'srun_pending_trapsemitsrun_pending_traps: bad value in trap_list[%d]: %pfor IGNORE_SIG as well as DEFAULT_SIG, and only gates theresending N to myselfline plus thekill(getpid(), sig)behind DEFAULT_SIG. So when a second signal lands in the window between the first delivery andtrap '', the captured output will still showbad value in trap_list[15]: 0x1— same warning family as the evidence that diagnosed this bug, now benign and without the fatal second line. It does not break the consumer: bin/fm-claude-stop-autoarm.sh:215 matches only anchored^(signal:|stale:|check:|heartbeat...), so a stray warning cannot be mistaken for an actionable wake.🔧 Fix: narrow trap-fix comments to the proven SIG_DFL resend
2 infos still open:
tests/fm-watcher-lock.test.sh:743- The new test is the only one of the 15$WATCH_ARMinvocations in this file that omitsPATH="$fakebin:$PATH"(compare the sibling at line 702, and every other launch at 437/482/539/581/620/663/800/824/873/904/913/938/967).make_casestill installs the hermetic faketmuxandfm-crew-state.shinto$dir/fakebin, but with them off PATH the spawned real watcher would resolve the developer's actual binaries. It does not manifest today: with an empty$statethere are no*.metafiles, sorecorded_windows(bin/fm-watch.sh:283) yields nothing,event_wait_or_sleepshort-circuits tosleep "$POLL", and FM_CHECK_INTERVAL/FM_HEARTBEAT=999999 plus an empty signal set keepsignal_crew_provably_workingunreached — so no PATH lookup happens. The fix is mechanical (add the same prefix the sibling uses) and restores the isolation guard the rest of the file relies on before a future watcher change on the empty-state path can reach a real tmux server.tests/fm-watcher-lock.test.sh:768- Note on which assertion actually carries this regression, not a defect.[ "$status" -eq 143 ]does not discriminate the bug:wait_for_exitends inwait "$pid"(tests/wake-helpers.sh:286), and a pre-fix arm that self-kills on the re-raised SIGTERM is reported by bash as 128+15 = 143, identical to the post-fixexit 143from the handler. The discriminating assertions are the.watch-cycle-exits.loggrep at line 769 (pre-fix the arm dies insidewait "$child"/cycle_log_appendbefore the row is written) and the orphan/stale-lock checks at 775/776. Worth knowing before anyone loosens the ledger grep — dropping it would leave a test that passes against the original bug.✅ **Test** - passed
✅ No issues found.
bash tests/fm-watcher-lock.test.sh— full file containing the new regression (34 cases, all ok, exit 0)bash tests/fm-watch-arm.test.sh— arm behaviour suite (14 cases, exit 0, no failures)Red proof:git checkout 409d8f2 -- bin/fm-watch-arm.shthen re-ran the arm signal cases —not ok - a repeat signal during cleanup skipped the handler instead of completing itGreen proof:git checkout cffe5a0 -- bin/fm-watch-arm.shthen re-ran the same cases —ok - arm completes its cleanup instead of self-killing on a repeat signal during teardownTargeted subset actually executed for the red/green pass:test_arm_hup_cleans_child_and_temp_output,test_attached_arm_signal_is_recorded_in_cycle_ledger,test_arm_survives_a_second_signal_during_its_own_cleanupManual end-to-end repro (repro-arm-repeat-signal.sh): started the real arm, held its ownstate/.watch-cycle-exits.lock, sent two SIGTERMs 150ms apart, then captured arm stdout, exit status,state/.watch-cycle-exits.log, child-watcher liveness andstate/.watch.lock— 6 runs pre-fix, 6 runs post-fix✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.