Repository navigation
fix(watcher): publish watch lock identity atomically to close turnend-guard race - #19
Merged
Merged
Conversation
…end-guard.sh fm-watch.sh published the watch lock's pid file (making the process look alive) before writing the fm-home/watcher-path/pid-identity files fm_watcher_lock_matches_pid needs to confirm it. A guard read landing in that gap saw a live pid with no matching identity yet and misreported "no live watcher holds this home lock", even though the watcher was mid-startup and about to finish. The gap widened under load because identity computation shells out to ps. fm_lock_try_create/fm_lock_try_acquire now take an optional prep_fn hook that writes into the lock's owner dir before it is published via ln -s, so pid and identity become visible to any reader atomically. fm-watch.sh uses it to write its identity, closing the race at the source.
This was referenced Jul 15, 2026
Merged
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 fm-turnend-guard.sh false-positive race: the guard was blocking turn ends immediately after bin/fm-watch-arm.sh, reporting no live watcher holds the home lock, even though the watcher was genuinely alive (live pid in state/.watch.lock/pid, beacon touched seconds ago). Root cause verified by reading fm-watch.sh's startup sequence: it published the watch lock's pid file (making fm_pid_alive succeed) before writing the fm-home/watcher-path/pid-identity files that fm_watcher_lock_matches_pid needs to confirm a match, and the gap widened under load because identity computation shells out to ps. Chosen fix (deliberately, over the guard-side bounded-retry alternative mentioned in the task): make the watcher's lock+identity write atomic by threading an optional prep_fn hook through fm_lock_try_create/fm_lock_try_acquire in bin/fm-wake-lib.sh that runs inside the not-yet-published lock owner directory before the ln -s that makes the lock discoverable, so pid and identity become visible to any reader in one atomic step. bin/fm-watch.sh now writes its identity through that hook instead of after the lock is already published. This closes the race at the source rather than papering over it with a guard-side timing retry, and the guard's real purpose (catching a genuinely dead/missing watcher, including the stale-beacon case) is unchanged - only the internal write ordering of fm-watch.sh's own lock claim changed. Added tests/fm-watcher-lock.test.sh::test_lock_prep_fn_publishes_atomically_before_ln, which reproduces the race window deterministically using a slow prep_fn (a controlled 1.5s sleep before the identity write) and asserts the lock stays entirely undiscoverable (no lockdir symlink at all) until the write completes, then asserts pid and identity content both exist once published. Documented the symptom, root cause, and fix in docs/turnend-guard.md (the doc that owns the guard contract, per the repo's one-owner rule for contracts) as a new dated section, following the existing dated-update pattern already used in that file. Verified manually before committing: bin/fm-lint.sh clean on the full repo, and the full existing test suite in tests/fm-watcher-lock.test.sh plus the related suites tests/fm-turnend-guard.test.sh, fm-wake-queue.test.sh, fm-watch-triage.test.sh, fm-watch-checkpoint.test.sh, fm-clickstack.test.sh, fm-tangle-guard.test.sh, fm-daemon.test.sh, fm-supervision-events.test.sh, fm-session-start.test.sh, and fm-pi-watch-extension.test.sh all pass.
What Changed
prep_fnhook tofm_lock_try_create/fm_lock_try_acquireinbin/fm-wake-lib.shthat runs inside the lock's owner directory before theln -spublish step, so hook-written content becomes visible atomically with the lock itself.bin/fm-watch.shto write its identity files (fm-home,watcher-path,pid-identity) through that hook instead of after the lock was already published, eliminating the window where a live pid was visible without a matching identity.docs/turnend-guard.md, and addedtests/fm-watcher-lock.test.shcoverage asserting the lock stays entirely undiscoverable until the prep_fn write completes.Risk Assessment
✅ Low: Small, well-contained change that threads an optional prep_fn hook through existing lock primitives to make pid+identity publication atomic; the fix is verified against the exact race described (pid visible via ln -s before identity files existed), covered by a deterministic regression test, and both call sites of fm_lock_try_create (normal and steal paths) correctly propagate prep_fn.</risk_rationale>
[]
Testing
The configured baseline test command (all 74 tests/*.test.sh) already passed on the target commit before this session. Beyond that, I built and ran a manual end-to-end repro that starts the real watcher process the way an end user's fm-watch-arm.sh would, and directly confirmed the described race: on the base commit the guard's exact failure precondition (live pid, no matching identity) reproduced in every one of 3 runs with a ~0.4s window; on the target commit the same repro showed pid and identity appearing atomically in all 3 runs, with zero race samples. The new regression test in tests/fm-watcher-lock.test.sh stayed green across 5 repeated runs (no flakiness), and all suites the author listed as manually verified passed. No issues found.
Evidence: E2E race repro script (used against both base and target commits)
Evidence: Base commit (c8c9089): real fm-watch.sh startup, slow-ps shim, 3 runs - race window observed every time
Evidence: Target commit (b5c6dd1): identical repro, 3 runs - pid and identity always appear atomically, zero race samples
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.
command -v tmux >/dev/null || { echo "tmux is required for e2e tests" >&2; exit 1; }; tmux -V; rc=0; for t in tests/*.test.sh; do echo "== $t =="; bash "$t" || rc=1; done; exit "$rc"bash tests/fm-watcher-lock.test.sh (5x, checking for flakiness in the new prep_fn atomicity test)bash tests/fm-turnend-guard.test.shbash tests/fm-wake-queue.test.shbash tests/fm-watch-triage.test.shbash tests/fm-watch-checkpoint.test.shbash tests/fm-clickstack.test.shbash tests/fm-tangle-guard.test.shbash tests/fm-daemon.test.shbash tests/fm-supervision-events.test.shbash tests/fm-session-start.test.shbash tests/fm-pi-watch-extension.test.shManual E2E repro: real bin/fm-watch.sh launched from a temporary worktree of the base commit (c8c9089) and from the target commit, with a slow ps shim on PATH, sampling state/.watch.lock/{pid,pid-identity,fm-home,watcher-path} every 10ms during startup to detect a live-pid-without-identity window (3 runs each commit)✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.