Repository navigation
fix: retire stale worker busy state and reclaim orphan locks - #338
Merged
Merged
Conversation
…an busy locks Adapt upstream PR 6733 and PR 8177. retire_busy_incarnation (bin/fm-control.sh) retired the busy sidecar but left busy_gen= in the task record, so the record and the sidecar disagreed about the generation a later steer resolves. It now retires the exact armed generation and drops the matching record line under the task-record lock, leaving the record untouched when it already names another generation. lock_acquire (bin/fm-busy-event.sh) gave up after 40 tries (about 2s) while a lock only becomes reclaimable at FM_BUSY_LOCK_STALE_SECS (default 5s), so a fresh orphan left by a killed writer failed the first writer behind it. It now polls until a lock that existed at wait start can cross that bound, then reclaims it, and still refuses within stale + 2s when a lock never ages. A digit-only stale setting keeps its decimal value.
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.
Summary
Retire a stopped worker's busy generation with its incarnation, and let a fresh orphan busy lock reach the reclaim threshold before the writer refuses.
Adapted upstream 6733 (
e7c3bdc1) and 8177 (4869cf58).bin/fm-busy-lib.shfm_busy_retire_incarnation: one retirement owner beside the generation contract it readsbin/fm-control.shexitretires after every proven stop, so a retry repairs a half-finished retirement;relaunchretires before launchingbin/fm-spawn.shbin/fm-busy-event.shdocs/architecture.mdowns the retirement contract;agent-control.mdandconfiguration.mdcarry the verb and toolchain consequencesWhere the fork diverges from upstream
bin/fm-control.shthroughfm_backlog_atomic_transition publishafter inline-sourcing the backlog and tasks-axi libraries. This fork puts retirement inbin/fm-busy-lib.shbeside the generation contract, takes the record lock, and republishes withmktemp+mv- the path this fork's ownms_meta_apply(fork PR 327) already uses for a control-plane record rewrite.bin/fm-spawn.sh <id> --relaunch --harness <name>, which may switch to a harness that arms no busy generation, so retirement also runs there before the record is published.--relaunchnow acquiresstate/.control-<id>.lock, where before only OMP model-switch coordination took it, so the early retirement cannot interleave with a control verb.fm-control relaunchstill passes its own lock through the existingFM_CONTROL_RELAUNCH_TX/FM_CONTROL_RELAUNCH_LOCK_OWNERhandshake, so it does not deadlock against itself.exitretry clears a generation whose sidecar is already gone (upgrade leftovers, or a failed record rewrite). A failed rewrite fails the verb naming the retry and the relaunch recovery instead of passing silently.mkdir/date/statfor the whole wait. This port keeps the interval and measures elapsed wait on a monotonic clock, so a backward wall-clock correction cannot extend it; lock age still reads filesystem mtimes.Time::HiResandCLOCK_MONOTONICa requirement for a contended busy write, whichdocs/configuration.mdnow states.tests/fm-control.test.shdoes not exist in this fork, so the AC1 regressions drive the realbin/fm-control.shandbin/fm-spawn.shthrough this fork's fake-backend harness, and the lock tests sit beside the existing lock tests intests/fm-busy-state.test.sh.Evidence
078d185d,bash tests/fm-busy-state.test.shfails withnot ok - the first retire over a fresh orphan lock was refused: error: busy-state lock timeout for t1, andbash tests/fm-spawn-relaunch-dead-endpoint.test.shfails withnot ok - the retired incarnation's busy_gen survived in the task record.exitretry that finishes a retirement whose record rename was forced to fail, a real--relaunch --harness devinthat leaves no generation and keepsidle herdr-nativethrough a late retired-generation event, a fresh orphan reclaimed while a live holder is never broken, a lock that stays young refused inside the bound, and decimal08reclaimed at eight seconds.tests/fm-busy-adapter-wiring.test.sh,tests/fm-control-switch-model.test.sh,tests/fm-omp-relaunch-guard.test.sh, andbin/fm-lint.shon the changed files are green; the pipeline's live scenarios and CI run are below.Merge Danger
Door: two-way for behavior - a revert restores the previous waiter and stops the new retirement - but the retirement deletes a worker's busy sidecar, record, and metadata line, which no revert can restore. Those are reconstructible from the next launch, so the loss is bounded.
Blast Radius: worker lifecycle and supervision.
exit,relaunch, spawn-time arming, and every harness hook that writes a busy event share this code. A defect here can misreport a worker idle or busy (which gates steers, idle alarms, and teardown), or block a relaunch outright. Safety that stays in place: retirement refuses when the recordedspawn_genno longer matches, releases the record lock on every path, leaves the record untouched when it names another generation, and a failed retirement fails the verb loudly instead of claiming success.Deferred review findings
The final review round raised these after the port's stop rule; each is recorded rather than fixed, and none changes an acceptance criterion.
busy_gen, so that path is dead weightOMP_SECONDMATE_RELAUNCHcondition and its prior-generation read, so the final code retires only on an explicit--relaunchAccepted tradeoff: a writer that holds its lock past
FM_BUSY_LOCK_STALE_SECSis treated as dead and its lock is reclaimed, now on the first attempt instead of a later retry. That is upstream 8177's contract and this fork's existing stale bound. Owner-aware locks and the rare concurrent-reclaimer race are follow-ups, not changes here.Testing
The targeted busy-state, spawn, and control regressions passed, and all seven live local scenarios passed. The two transcripts capture the worker recovery and lock timing results, including both regressions reproduced on the base commit.
Evidence: Live tmux worker retirement and recovery transcript
Evidence: Live busy-lock timing and persisted state transcript
Pipeline
Updates from git push no-mistakes
✅ **Intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
bin/fm-control.sh:660- AC1 requires that “When a worker's incarnation is replaced, its recorded busy generation is retired with it.” The new retirement logic is reachable only through control. The documented standalonefm-spawn.sh --relaunch --harness devinpath can replace a stopped Claude worker on Herdr without arming a new generation. Spawn publishes fresh metadata but leaves the old sidecar and record in place (bin/fm-spawn.sh:4815,:5327,:5374). If the old record saysbusy fm-spawn, the classifier accepts it before checking Devin’s native idle state (bin/fm-busy-lib.sh:190,:319,:361). This affects direct switches to an unarmed harness whenever the old busy record survives. Retire the old generation at the shared, verified replacement boundary before publishing the new incarnation. The existing control paths atbin/fm-control.sh:696,:724, and:1565must use compatible retirement semantics.bin/fm-busy-event.sh:134- AC2 requires coverage of “a live holder (still refused).” The changedif [ "$age" -ge "$stale" ]branch deletes the lock based on age alone. If a live hook stalls in its write and a retire starts while the lock is fresh, the old waiter would time out at about two seconds. The new waiter reaches five seconds and deletes the live hook’s lock. The resumed hook can overwrite state or release the waiter’s lock. This is uncommon at the default threshold and can happen immediately under contention withFM_BUSY_LOCK_STALE_SECS=0. Enforce owner-aware acquisition, reclaim, and release at the shared writer boundary used by arm and apply/retire (bin/fm-busy-event.sh:151,:166,:189). The remedy needs approval if it adds an ownership record. The tests release the simulated holder after one second or refresh an ownerless directory (tests/fm-busy-state.test.sh:203,:249), so both pass despite this bug. Keep a real writer alive past the reclaim threshold and assert refusal and unchanged state.tests/fm-spawn-relaunch-dead-endpoint.test.sh:1471- AC1 requires “A focused test shows a replaced incarnation's stale busy_gen no longer marks the new incarnation busy.” This test deliberately refuses the replacement attests/fm-spawn-relaunch-dead-endpoint.test.sh:1460, then manually appends the generation it checks at:1472–1475. A replacement publisher that leaves stale metadata can pass this test on every CI run. Keep the failed-launch cleanup assertions at:1463–1467. Remove the simulated replacement and use a successful relaunch through the public CLI, then assert that the replacement’s busy verdict is unchanged when the old worker emits a late event.bin/fm-control.sh:669- Retirement cannot repair a task whose sidecar is already gone. Every successful stop under the base implementation can leave that shape, and it also recurs if control is interrupted after removing the sidecar or if the new metadata rewrite fails. After an upgrade, a replacement refused before publication leaves the oldbusy_genbehind becausebin/fm-control.sh:662skips cleanup. Retryingexitalso skips cleanup at:681–683. Use the existing idempotent exact-generation retirement path to reconcile the stopped incarnation’s recorded generation when the sidecar is absent, and surface a failed metadata rewrite so the operator can retry.tests/fm-busy-state.test.sh:223- The08regression case releases its lock after one second, so a writer that silently falls back to five seconds still passes on every CI run. The intended oracle is an eight-second reclaim threshold. Give the lock an age between five and eight seconds and verify that it remains intact until eight. This catches a fallback that prematurely reclaims a lock for operators using the existing setting.🔧 Fix applied.
7 issues (2 errors, 5 warnings) still open:
bin/fm-control.sh:660- AC1 requires that “When a worker's incarnation is replaced, its recorded busy generation is retired with it.” The new retirement logic is reachable only through control. The documented standalonefm-spawn.sh --relaunch --harness devinpath can replace a stopped Claude worker on Herdr without arming a new generation. Spawn publishes fresh metadata but leaves the old sidecar and record in place (bin/fm-spawn.sh:4815,:5327,:5374). If the old record saysbusy fm-spawn, the classifier accepts it before checking Devin’s native idle state (bin/fm-busy-lib.sh:190,:319,:361). This affects direct switches to an unarmed harness whenever the old busy record survives. Retire the old generation at the shared, verified replacement boundary before publishing the new incarnation. The existing control paths atbin/fm-control.sh:696,:724, and:1565must use compatible retirement semantics.bin/fm-busy-event.sh:134- AC2 requires coverage of “a live holder (still refused).” The changedif [ "$age" -ge "$stale" ]branch deletes the lock based on age alone. If a live hook stalls in its write and a retire starts while the lock is fresh, the old waiter would time out at about two seconds. The new waiter reaches five seconds and deletes the live hook’s lock. The resumed hook can overwrite state or release the waiter’s lock. This is uncommon at the default threshold and can happen immediately under contention withFM_BUSY_LOCK_STALE_SECS=0. Enforce owner-aware acquisition, reclaim, and release at the shared writer boundary used by arm and apply/retire (bin/fm-busy-event.sh:151,:166,:189). The remedy needs approval if it adds an ownership record. The tests release the simulated holder after one second or refresh an ownerless directory (tests/fm-busy-state.test.sh:203,:249), so both pass despite this bug. Keep a real writer alive past the reclaim threshold and assert refusal and unchanged state.bin/fm-control.sh:669- Retirement cannot repair a task whose sidecar is already gone. Every successful stop under the base implementation can leave that shape, and it also recurs if control is interrupted after removing the sidecar or if the new metadata rewrite fails. After an upgrade, a replacement refused before publication leaves the oldbusy_genbehind becausebin/fm-control.sh:662skips cleanup. Retryingexitalso skips cleanup at:681–683. Use the existing idempotent exact-generation retirement path to reconcile the stopped incarnation’s recorded generation when the sidecar is absent, and surface a failed metadata rewrite so the operator can retry.bin/fm-busy-event.sh:143- The wait deadline uses wall-clock time. If an operator corrects a host clock backward by a minute while a waiter is polling a fresh orphan, bothnow - startand the lock age become negative, so the writer can wait about a minute despite the advertised seven-second default bound. This is uncommon and affects launch, hook, and retirement calls through bin/fm-busy-event.sh:166 and :189. Bound elapsed waiting independently of the wall clock used to interpret filesystem timestamps. The refusal test at tests/fm-busy-state.test.sh:254 exercises a lock that stays young under a stable clock.bin/fm-busy-event.sh:135- Follow-up for a rare concurrent reclaim. If a late hook and a retire command are already waiting on the same fresh orphan, both can observe it crossing the age threshold in the same poll. One waiter can remove the orphan and acquire the lock before the other executes its pending removal. The second waiter then deletes a newly acquired lock whose holder is still inside the writer. This can break serialization for arm at bin/fm-busy-event.sh:166 and apply or retire at :189, with release at :151 also operating on the reused path. The race is distinct from reclaiming a single holder that has itself exceeded the age threshold. Address reclaim ownership at the shared writer boundary in a follow-up.bin/fm-control.sh:1614- Round 1's retirement-retry fix left out exits that close the pane. The original invocation accepts amissingendpoint as proof of a delivered exit at bin/fm-control.sh:681. If retirement then fails, retryingexitrejects that same missing endpoint at :649 before reaching retirement at :1614. This is uncommon and requires one retirement failure after a verified stop. The regression test at tests/fm-spawn-relaunch-dead-endpoint.test.sh:1480 and :1497 keeps an idle shell in the pane, so it misses this case. Keep the missing-endpoint refusal and identify the supported recovery path in the retirement diagnostic. Relaunch already reconciles the missing-endpoint case at bin/fm-control.sh:1517 and :1527.bin/fm-spawn.sh:4793- Round 1's standalone relaunch fix also added retirement to automatic OMP secondmate recovery. AC1 requires retirement of a worker's recorded busy generation, and bin/fm-spawn.sh:4816 excludes secondmates from busy-generation publication. Every stopped or missing OMP secondmate recovered throughfm-spawn --secondmatenow reaches this extra metadata-lock and retirement path. Remove the OMP secondmate condition at bin/fm-spawn.sh:4793 and its separate prior-generation read and fallback at :2585 and :4794. The explicit relaunch branch covers the standalone worker replacement that prompted the fix.🔧 Fix applied.
10 issues (2 errors, 8 warnings) still open:
bin/fm-control.sh:660- AC1 requires that “When a worker's incarnation is replaced, its recorded busy generation is retired with it.” The new retirement logic is reachable only through control. The documented standalonefm-spawn.sh --relaunch --harness devinpath can replace a stopped Claude worker on Herdr without arming a new generation. Spawn publishes fresh metadata but leaves the old sidecar and record in place (bin/fm-spawn.sh:4815,:5327,:5374). If the old record saysbusy fm-spawn, the classifier accepts it before checking Devin’s native idle state (bin/fm-busy-lib.sh:190,:319,:361). This affects direct switches to an unarmed harness whenever the old busy record survives. Retire the old generation at the shared, verified replacement boundary before publishing the new incarnation. The existing control paths atbin/fm-control.sh:696,:724, and:1565must use compatible retirement semantics.bin/fm-busy-event.sh:134- AC2 requires coverage of “a live holder (still refused).” The changedif [ "$age" -ge "$stale" ]branch deletes the lock based on age alone. If a live hook stalls in its write and a retire starts while the lock is fresh, the old waiter would time out at about two seconds. The new waiter reaches five seconds and deletes the live hook’s lock. The resumed hook can overwrite state or release the waiter’s lock. This is uncommon at the default threshold and can happen immediately under contention withFM_BUSY_LOCK_STALE_SECS=0. Enforce owner-aware acquisition, reclaim, and release at the shared writer boundary used by arm and apply/retire (bin/fm-busy-event.sh:151,:166,:189). The remedy needs approval if it adds an ownership record. The tests release the simulated holder after one second or refresh an ownerless directory (tests/fm-busy-state.test.sh:203,:249), so both pass despite this bug. Keep a real writer alive past the reclaim threshold and assert refusal and unchanged state.bin/fm-control.sh:669- Retirement cannot repair a task whose sidecar is already gone. Every successful stop under the base implementation can leave that shape, and it also recurs if control is interrupted after removing the sidecar or if the new metadata rewrite fails. After an upgrade, a replacement refused before publication leaves the oldbusy_genbehind becausebin/fm-control.sh:662skips cleanup. Retryingexitalso skips cleanup at:681–683. Use the existing idempotent exact-generation retirement path to reconcile the stopped incarnation’s recorded generation when the sidecar is absent, and surface a failed metadata rewrite so the operator can retry.bin/fm-busy-event.sh:143- The wait deadline uses wall-clock time. If an operator corrects a host clock backward by a minute while a waiter is polling a fresh orphan, bothnow - startand the lock age become negative, so the writer can wait about a minute despite the advertised seven-second default bound. This is uncommon and affects launch, hook, and retirement calls through bin/fm-busy-event.sh:166 and :189. Bound elapsed waiting independently of the wall clock used to interpret filesystem timestamps. The refusal test at tests/fm-busy-state.test.sh:254 exercises a lock that stays young under a stable clock.bin/fm-busy-event.sh:135- Follow-up for a rare concurrent reclaim. If a late hook and a retire command are already waiting on the same fresh orphan, both can observe it crossing the age threshold in the same poll. One waiter can remove the orphan and acquire the lock before the other executes its pending removal. The second waiter then deletes a newly acquired lock whose holder is still inside the writer. This can break serialization for arm at bin/fm-busy-event.sh:166 and apply or retire at :189, with release at :151 also operating on the reused path. The race is distinct from reclaiming a single holder that has itself exceeded the age threshold. Address reclaim ownership at the shared writer boundary in a follow-up.bin/fm-control.sh:1614- Round 1's retirement-retry fix left out exits that close the pane. The original invocation accepts amissingendpoint as proof of a delivered exit at bin/fm-control.sh:681. If retirement then fails, retryingexitrejects that same missing endpoint at :649 before reaching retirement at :1614. This is uncommon and requires one retirement failure after a verified stop. The regression test at tests/fm-spawn-relaunch-dead-endpoint.test.sh:1480 and :1497 keeps an idle shell in the pane, so it misses this case. Keep the missing-endpoint refusal and identify the supported recovery path in the retirement diagnostic. Relaunch already reconciles the missing-endpoint case at bin/fm-control.sh:1517 and :1527.bin/fm-spawn.sh:4793- Round 1's standalone relaunch fix also added retirement to automatic OMP secondmate recovery. AC1 requires retirement of a worker's recorded busy generation, and bin/fm-spawn.sh:4816 excludes secondmates from busy-generation publication. Every stopped or missing OMP secondmate recovered throughfm-spawn --secondmatenow reaches this extra metadata-lock and retirement path. Remove the OMP secondmate condition at bin/fm-spawn.sh:4793 and its separate prior-generation read and fallback at :2585 and :4794. The explicit relaunch branch covers the standalone worker replacement that prompted the fix.bin/fm-spawn.sh:4793- Round 1 placed retirement after endpoint creation in standalone relaunch. For example, if an operator replaces a stopped Claude worker with Devin after its Herdr pane has closed, spawn creates a new pane at bin/fm-spawn.sh:4142 before retiring the old busy generation. If the metadata rewrite fails, the command exits with the new pane open and the task record still pointing at the old pane. A retry can create another pane. This is uncommon and requires one retirement failure. The same ordering applies to tmux at bin/fm-spawn.sh:3917, Zellij at bin/fm-spawn.sh:4179, and cmux at bin/fm-spawn.sh:4210. Retire the generation after the shared missing-endpoint and lease checks at bin/fm-spawn.sh:3896, before any backend creates a replacement. Keep retirement after the idle-shell proof when reusing an existing endpoint.bin/fm-busy-event.sh:126- Round 2 made Perl Time::HiRes mandatory whenever the busy lock is contested. The documented toolchain permits a tmux host without Perl. If teardown kills a worker while its hook owns the lock on such a host, the next writer refuses before inspecting the lock's age, even if the orphan is already reclaimable. This occurs on every contention or orphan recovery on affected hosts, while uncontended writes conceal the dependency. Arm at bin/fm-busy-event.sh:166 and apply or retire at bin/fm-busy-event.sh:189 share this path. Provide a bounded, wall-clock-independent fallback at lock acquisition for hosts without the Perl clock.bin/fm-control.sh:652- Round 2 also changed the generic missing-endpoint exit message to recommend relaunch. Every operator who repeats an exit after a pane closes now gets advice to start a replacement, even when no busy retirement failed and the intended outcome was to leave the worker stopped. The accepted recovery requirement only needs that advice in the retirement-failure diagnostic at bin/fm-control.sh:635, which the test covers at tests/fm-spawn-relaunch-dead-endpoint.test.sh:1512. Restore the generic missing-endpoint message.✅ **Test** - passed
✅ No issues found.
bash tests/fm-busy-state.test.shbash tests/fm-spawn-relaunch-dead-endpoint.test.shbash tests/fm-control-switch-model.test.shuv run --no-project --offline --no-python-downloads python -u .phase-test/locks.pydrove the real busy-event CLI with a killed writer, active contention, a decimal threshold, and wall-clock rollback.unshare -Urmn --propagation private bash .phase-test/workers.shdrove standalone relaunch, exit retry, and closed-pane recovery in isolated tmux with real Codex.unshare -Urmn --propagation private bash .phase-test/workers-base.shreproduced acceptance of the late event on the base commit.Read the final persisted worker records, saved both CLI transcripts, and verified the disposable tmux servers had stopped.✅ **Document** - passed
✅ No issues found.
🔧 **Lint** - 1 issue found → auto-fixed ✅
🔧 Fix applied.
✅ Re-checked - no issues remain.
✅ **Push** - passed
✅ No issues found.