Repository navigation
chore: update pull request - #5
Merged
Merged
Conversation
…ess can hand it back A watcher TERMed inside the arm window left state/.wake-queue.lock and its singleton lock held by a pid that was already gone. Recovering an abandoned lock is not free: the dead-holder steal is roughly 20-30 forks, measured at 3.0s wall for one lock on an unloaded host, so the successor inherited seconds of work before its first beat, missed the same FM_ARM_CONFIRM_TIMEOUT, and abandoned the locks again. That is how one slow arm regenerated itself into a supervision outage that could not clear, observed twice: six consecutive confirmation-timeout cycle exits with beacon_age climbing 162, 175, 443, and again with both locks naming dead pids alongside an owner directory that had survived eleven days. The reported cause was that fm_lock_acquire_wait has no dead-holder steal. It does. The steal lives in fm_lock_try_acquire and is generic to every lock path rather than specific to the singleton watch lock, so fm_lock_acquire_wait inherits it. Both assertions the report asked for pass unchanged against the pre-change library, so they ship here as regression guards and not as proof. The actual defect is an ordering inversion in fm_lock_try_create. The lock became visible on disk at ln -s and carried this process's pid at fm_lock_claim, several forks before _fm_lock_track_held recorded it in memory. Every one of those forks is a signal-delivery point, so a signal landing in that window left a lock held by this process that its own exit sweep could not see. The same inversion around mktemp -d is what leaks the .owner.* directories. Locks and owner directories are now recorded before they are created and forgotten only after they are removed, so the in-memory record stays a superset of what is durably on disk. On the question the report left open, the TERM trap does need to release its locks, and the ordering fix is what makes that possible rather than a substitute for it. Without the trap nothing sweeps at all; without the ordering the sweep saw only about half of what was held, which is why releasing on the trap alone fixed the failure about half the time. Neither the arm confirmation timeout nor the pre-lock startup cost is touched. Both make the window reachable, and shrinking either would have hidden this rather than fixed it. Tests: a signalled watcher must hand back every lock it holds, which passed about half the time before and now passes 12 runs out of 12; and a direct assertion of the ordering invariant, which overrides the two steps that follow each durable write because a timing race cannot check the ordering reliably. Both fail against the pre-change library. Pre-existing and deliberately not addressed here, all three verified against the default branch on the same host. test_lock_single_winner_under_concurrency is a load-sensitive flake on both; interleaved runs put it at 1/10 failures on the default branch against 2/10 here, and it is what aborts that branch's own full-suite run. test_arm_self_eviction_is_loud_without_successor fails 4/4 on the default branch in isolation, and test_signal_catchup_without_running_watcher fails 3/3 on both, its 4s exit budget being shorter than the pre-lock startup this host needs.
…ck and ordering tests
The CI auto-fix round that closed the owner-directory race also committed tests/fm-watcher-lock-skip-flake.test.sh.tmp, a 1358-line scratch copy of tests/fm-watcher-lock.test.sh it had built to skip the known-flaky test_lock_single_winner_under_concurrency while verifying its own change. It is a working artifact, not a test the suite runs or should carry: it duplicates the real file, its name is not picked up as a test, and leaving it in the tree would strand a stale copy that silently drifts from the original. Removing it changes no behavior and no test coverage.
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 the wake-queue lock deadlock that killed firstmate supervision for about twenty minutes on 2026-08-12 with four tasks in flight: a watcher TERMed inside the arm window died still holding state/.wake-queue.lock, the next watcher blocked on that lock naming a dead pid, never beat, was TERMed at the confirmation timeout, and the cycle repeated (six consecutive state/.watch-cycle-exits.log records with reason=confirmation-timeout, beacon_age climbing 162, 175, 443). Manual recovery was to verify the recorded holder pid dead, remove the lock and its owner dir, and re-arm.
The task as briefed named fm_lock_acquire_wait in bin/fm-wake-lib.sh as the defect - an unbounded spin with no dead-holder steal - and asked for the same dead-holder steal the singleton watch lock already has, following that existing precedent in the same file rather than inventing a second mechanism.
ACCEPTED SUPERSEDING FINDING: that premise is wrong at HEAD. Investigation showed fm_lock_acquire_wait ALREADY steals dead holders, and both assertions the brief mandated pass against unmodified main. The real defect is one level down: fm_lock_try_create recorded a lock in its in-memory held-list AFTER publishing the lock on disk, and recorded an owner directory after creating it. Every fork between a durable write and its bookkeeping is a signal-delivery point, so a signal landing in that inverted window left this process holding a lock its own exit sweep could not see - which is exactly how a TERMed watcher stranded state/.wake-queue.lock, and separately how orphaned .owner.* directories leaked.
The accepted fix is therefore: (1) establish the ordering invariant RECORD BEFORE CREATING, FORGET AFTER REMOVING throughout the lock primitives, so the in-memory record is always a superset of what is durably on disk, with over-recording harmless because fm_lock_release no-ops unless the recorded pid is the caller's own; (2) replace the mktemp -d owner-directory creation with a fork-free pure-bash path computation so the path can be recorded as pending before anything reaches disk; (3) add fm_lock_release_all_held so a process that can still run code on its way out hands its locks back instead of leaving the next process a multi-second steal per lock.
The brief also asked, as an open question, whether bin/fm-watch.sh's TERM trap should release the locks it holds, requiring an explicit answer either way rather than a silent choice. ANSWER: it should, and it now does. The trap is changed from
trap 'exit 1' HUP INT TERMto one that marks a signal exit and sweeps held locks, and a watcher_bootstrap_cleanup EXIT trap now covers the arm window - the stretch between the singleton lock and the first beat that previously ran before any cleanup trap existed. The singleton watch lock is deliberately EXCLUDED from the blanket sweep and released only through fm_recovery_transition's recovery marker, so supervision keeps the durable record that the watcher went down; the recovery-marker exits that intentionally retain the lock as stale evidence must not be undone by the sweep.EXPLICIT EXCLUSIONS, still in force: do NOT change the arm confirmation timeout (FM_ARM_CONFIRM_TIMEOUT, default 10s in bin/fm-watch-arm.sh) and do NOT optimise the pre-lock startup cost (the reproducible 4.3s wall in fm-pr-check-migrate.sh --checks-safe before bin/fm-watch.sh reaches the lock). Those made the deadlock reachable but are not the deadlock; fixing either would make it rarer and leave the trap in place, which is worse than leaving it visible.
Tests, per the brief and following the repo's existing test convention in tests/fm-watcher-lock.test.sh with no new harness: the two assertions the brief mandated are shipped as regression guards (fm_lock_acquire_wait takes over a lock whose holder is verifiably dead; and, the property the steal must not break, a lock held by a LIVE process is never stolen and is waited out until the holder hands it back). Because those two pass on stock main, the tests that actually fail without this change are the two proving the real defect: a watcher signalled while parked holding BOTH the singleton and wake-queue locks hands back every lock it holds while still publishing downtime through the recovery marker, and the record-before-visible ordering invariant itself.
What Changed
Final changed paths and statuses:
Risk Assessment
✅ Low: Traced the full record-before-create/forget-after-remove invariant through fm_lock_try_create, fm_lock_remove_path, and fm_lock_release, and the arm-window/main-loop trap split in fm-watch.sh, against the concrete two-lock (singleton + wake-queue, via _fm_recovery_marker_arm_check) TERM scenario the intent describes; every path correctly tracks, sweeps, or intentionally excludes (WATCH_LOCK, released only through the recovery marker) matches production code exactly, and the new regression tests reproduce the real failure sequence via actual process signals and runtime state inspection rather than source-text assertions.
Testing
The full 33-case tests/fm-watcher-lock.test.sh suite passes on the target commit, and the specific regression test for the reported deadlock (a watcher TERMed while holding both the singleton and wake-queue locks) was confirmed to genuinely fail against the pre-fix base commit and pass after the fix, satisfying the reproduce-then-fix requirement; no issues found.
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
✅ **Review** - passed
✅ No issues found.
✅ **Test** - passed
✅ No issues found.
bin/fm-test-run.sh tests/fm-watcher-lock.test.sh (target commit 383c541) — 33/33 pass, exit=0bin/fm-test-run.sh tests/fm-watcher-lock.test.sh against base commit 7b88520 with the new test file copied in — test_watcher_signal_releases_held_locks fails as expected ('signalled watcher left its singleton lock held by a dead pid'), confirming it is a genuine pre-fix/post-fix regression proof rather than a vacuous passManually diffed bin/fm-wake-lib.sh and bin/fm-watch.sh against base to confirm the record-before-create/forget-after-remove ordering invariant, fork-free owner-path computation, fm_lock_release_all_held, and the watcher_bootstrap_cleanup/signal-exit trap changes match the accepted-fix description in user intent✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.