Repository navigation
fix(server): let the stale-session sweep re-acquire a lost lease - #1302
Merged
Merged
Conversation
jhrozek
force-pushed
the
fix-sweep-lease-tombstone
branch
from
September 9, 2026 18:35
9d6f50d to
743e86b
Compare
SettleIfStale (the crash-orphaned-running-session reconcile sweep, issue #475) could never recover a session whose lease was lost via a merely-late renew rather than a real competing owner: flocklease's Renew returns ErrLeaseHeld identically for both cases, onLeaseLost sets a permanent lostOwnership tombstone, and reaffirmLease fails fast on that tombstone without ever attempting a real backend Acquire. The only caller that ever clears the tombstone is CloseSession's teardown (77e2b32), which SettleIfStale never reaches — so a session recovered only by the periodic sweep (never closed) was stuck in StateRunning forever, failing "session is leased by another process" on every sweep tick. Add acquireMutationLeaseForStaleSettle, used only by SettleIfStale: its caller is already authorized as the system stale-reconciler and has independently verified (age-horizon + IsLive) that the session is a genuine crash orphan, so it attempts a real Acquire past the tombstone and clears it on success. Every other lease caller (acquireLease/reaffirmLease, and therefore normal run-entry and approval-resume) is untouched — an earlier, broader version of this fix regressed the awaiting-lease-loss and session-affinity/handoff tests, which deliberately require a stale owner to stay refused even when no successor ever took over. Co-Authored-By: Claude Sonnet 5 <[email protected]>
Extract the Acquire->classify->install-and-renew body shared by reaffirmLease and acquireMutationLeaseForStaleSettle into one acquireLeaseCore(ctx, id, bypassTombstone bool), fixing two real issues the duplication was hiding: - The post-Acquire dup-collapse only ever checked for ANY existing heldLeases[id] entry. onLeaseLost's preserveAwaiting branch deliberately leaves a stale, invalidated entry behind (it isn't deleted, only marked invalid) — so a stale-settle re-Acquire that landed on such an id would see that stale entry, wrongly treat it as a live winner, cancel its own just-acquired lease's renewer, and drop the fresh port.Lease without ever releasing it. The dup-check now only collapses against a genuinely VALID existing entry. - Four independent reviewers (Standards, software-architect, library-reuse-reviewer, code-duplication-reviewer) flagged the ~50-line duplication itself as a drift risk; the shared core removes it while keeping the one safety-relevant axis (bypassTombstone) as an explicit, documented parameter rather than folding it into shared control flow. Also, per review: - Document (don't gate) that the stale-settle path skips acquireLease's drain check — repairing an already-crash-orphaned session is cleanup, not a new admission, so this is deliberate. - Correct two comments that still claimed CloseSession is the ONLY place lostOwnership is cleared (the lostOwnership field comment, reverted to stale wording by an earlier `git checkout`, and ADR 0027 List 1 row 27). - Cross-reference TestCallerIdentityEdgeRejectsMalformedPrincipal (caller_identity_test.go) from the new method's doc comment: it already pins that staleReconcileAuthorized's system-principal check can't be forged from any external gRPC/HTTP surface, closing the "no co-located test" gap a reviewer raised. - Add TestStaleSessionSweepRefusesWhenGenuinelyHeldElsewhere: the negative case adjacent to the recovery test — a real competitor holding the lease when the sweep runs must still be refused, with the durable snapshot left untouched. One reviewer's "blocker" (claiming acquireMutationLeaseForStaleSettle was unreferenced dead code) was independently verified false — a stale patch snapshot on that reviewer's side, not a real gap. Co-Authored-By: Claude Sonnet 5 <[email protected]>
jhrozek
force-pushed
the
fix-sweep-lease-tombstone
branch
from
September 9, 2026 20:25
743e86b to
aa1a53d
Compare
jbeda
added a commit
that referenced
this pull request
Sep 11, 2026
* commit 'cc6b16afbf7feec3e16451fdc590a3265fbf0c0b': docs: amend Shell diagnostic ownership (#1330) fix(mecatui): refresh status context on session continuation (#1328) fix(jsonlstore): preserve session saves with legacy lineage (#1327) test(sessiondebug): eliminate handle tampering flake (#1323) fix(agent): authorize structured subagent results (#1317) feat(mcp): support attachment-backed broker queries for CallMcpWithQuery (#1309) docs(agents): document full-gate timeout guidance (#1316) fix(server): let the stale-session sweep re-acquire a lost lease (#1302) feat(clientauth): headless Linux credential storage (#1291) chore(mcpbroker): trace protected call latency (#1314) # Conflicts: # docs/acceptance/canonical-shell-command-tool.md
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
SettleIfStale, issue Sessions left StateRunning by a crash report as in-progress forever #475) could never recover a session whose lease was lost via a merely-late renew rather than a real competing owner —flocklease.RenewreturnsErrLeaseHeldidentically for both cases, and the resultinglostOwnershiptombstone is otherwise only ever cleared byCloseSession's teardown (fix(server): let CloseSession clear a stale lost-lease tombstone #1266), which the sweep never reaches. It failed identically on every 5-minute sweep tick, forever, until a process restart.acquireMutationLeaseForStaleSettle, used only bySettleIfStale: its caller is authorized as the system stale-reconciler and has already independently verified (age-horizon +IsLive) that the session is a genuine crash orphan, so it's safe to attempt a real re-Acquire past the tombstone and clear it on success. Every other lease caller (normal run-entry,ApproveRun's awaiting-resume,RenameSession/DeleteSession/other management mutations) is untouched — a stale owner must still be permanently refused there even when no successor ever took over (pinned byTestADR_0294_AwaitingLeaseLossRetractsLocalAskPreservesSnapshotand two session-affinity-and-handoff scenario tests; an earlier, broader version of this fix regressed all three and was reverted in favor of this narrow scoping).acquireLeaseCore(ctx, id, bypassTombstone bool), used by bothreaffirmLease(the unchanged general path) and the new function — this also fixes a real lease-leak: the old duplicated dup-collapse check treated any existingheldLeases[id]entry as a live winner, including a stale invalidated oneonLeaseLost'spreserveAwaitingbranch deliberately leaves behind, silently dropping a freshly-Acquired lease without ever releasing it.acquireLease's drain check — repairing an already-crash-orphaned session is cleanup, not a new admission.CloseSessionis the only place the tombstone is cleared (lostOwnership's field doc, and ADR 0027 List 1 row 27).This PR went through a 9-agent MoE panel review (Spec/Standards/Test-adequacy axes + a 6-agent Domain panel: secure-code-reviewer, go-security-reviewer, software-architect, go-architect, library-reuse-reviewer, code-duplication-reviewer). All important-and-above findings are addressed in the second commit; one reviewer's "blocker" (claiming the new function was unreferenced dead code) was independently verified false.
Test plan
TestStaleSessionSweepRecoversAfterSelfInflictedLeaseLoss(new): reproduces the deadlock via a genuine renewer failure (not a shortcut), fails without the fix, passes with it.TestStaleSessionSweepRefusesWhenGenuinelyHeldElsewhere(new): the sweep's bypass still correctly refuses when a real competitor holds the lease, snapshot left untouched.go test ./internal/adapter/server/...— full suite green, includingTestADR_0294_AwaitingLeaseLossRetractsLocalAskPreservesSnapshotand the Scenario5/Scenario7 session-affinity-and-handoff tests (unregressed).go build ./...,go vet ./...,gofmt -l,golangci-lint run ./internal/adapter/server/...(0 issues).docs/lintTestRealDesignDocsCitations(ADR citation check) green.main(TestReconcileStaleFireAfterSessionSettles,TestSessionDebuggerCrossBoundaryAcceptance) confirmed present without this change too — not introduced by this PR.🤖 Generated with Claude Code