Repository navigation
fix(flocklease): reclaim expired lease when record still names caller - #1343
Conversation
flocklease.Renew treated any expired lease record as definitive loss (ErrLeaseHeld), even when the record still named the calling process as owner at its own fencing token. On a single-host deployment, a process suspended past the TTL (e.g. laptop sleep) would lose its lease on the next Renew after resume even though nothing else could have raced it. The safe invariant is not "the flock generation handle is still open" (Acquire can take over an expired record regardless), but that the durable record, read under the same stable transition lock Acquire/Release use, still shows the same owner at the same fencing token. On a single host that can only be true if nobody else raced an Acquire/takeover in the interim, so Renew now reclaims with a fresh expiry (token unchanged) instead of declaring loss. A record whose owner or token changed during the gap (a genuine takeover) still returns ErrLeaseHeld unconditionally. Clarifies port.SessionLease.Renew's doc comment (narrowing only, no contract change: implementations may still always treat expiry as loss if they can't prove otherwise) and flocklease.Renew's own comment. Rewrites the flocklease test that encoded the old behavior into two tests covering the reclaim-with-no-competitor and still-fails-after-genuine-takeover cases. Scope is flocklease only; k8slease and grpcdriver/sessionlease.go are untouched per the panel review in the issue. fixes #1333 Co-Authored-By: Claude Sonnet 5 <[email protected]>
Panel review on 56b9bce flagged that the held == nil hard-fail branch in Renew -- the one that actually carries the cross-process safety argument -- wasn't isolated by either new test from that commit. Add TestRenewFailsWithoutLocallyTrackedGeneration: two independent flocklease.New instances over the same dir; the first Acquires, then Renew is called on the SECOND (unheld) instance with a copy of the first's returned port.Lease. The durable record still names that exact owner and token and has not expired, yet the second instance never populated its own `held` map for that generation, so Renew must still fail closed with ErrLeaseHeld. Also records the narrowed Renew/ErrLeaseHeld port contract in engine/CHANGELOG.md (doc-comment-only clarification, no exported signature changed, no task api:update needed) and notes the new uncontested-expiry-reclaim behavior in the flocklease adapter bullet of docs/adr/0027-cloud-native.md's resource inventory. Follow-up to #1333. Co-Authored-By: Claude Sonnet 5 <[email protected]>
ae16acd to
35e9e02
Compare
JAORMX
left a comment
There was a problem hiding this comment.
Second-pass review of 35e9e02: the flock renewal implementation is sound; I found no runtime or security blocker. One required documentation correction before approval: update user-docs/building/extension-points/session-lease.md (currently line 126 and the sentinel description) in this PR, as required for user-facing behavior changes. It still describes Renew ErrLeaseHeld as another process taking over when the TTL lapses. Explain definitive backend-reported loss, the local flock exception for an expired but still locally tracked matching owner/token, and the unchanged stricter distributed-backend behavior. The ADR inventory and changelog do not replace the canonical user guide. Separately, the takeover regression test needs stronger coverage, noted inline; that is non-blocking. Existing CI is still running after the rebase; I did not run local tests or linters.
| t.Fatalf("Acquire first: %v", err) | ||
| } | ||
| clk.advance(ttl) | ||
| competitor, err := adapter.Acquire(ctx, id, "owner-b") |
There was a problem hiding this comment.
Non-blocking regression coverage: this takeover uses the same adapter, whose Acquire relinquishes the original locally tracked generation. Renew(first) therefore fails through held == nil even if the durable owner/token mismatch checks are removed. Please use a second adapter over the same directory for the takeover, then renew through the original adapter, which still retains its generation. That pins the real stale-holder mismatch path. The nearby claim that this race cannot happen on a single host is also misleading: a paused holder and another local process can produce exactly this ordering.
session-lease.md still described Renew's ErrLeaseHeld as always meaning "another process took over on TTL lapse", which no longer holds for flocklease's single-host reclaim exception. Document the exception explicitly and note other backends are unchanged. TestRenewStillFailsAfterGenuineTakeover reused one adapter instance for both Acquire calls, so the competitor's Acquire silently relinquished the original holder's held-generation entry and the test passed via the unrelated held == nil branch instead of the durable owner/token mismatch check. Use two instances over the same directory so the test exercises the intended path, and correct the comment claiming this race can't happen on a single host. Co-Authored-By: Claude Sonnet 5 <[email protected]>
JAORMX
left a comment
There was a problem hiding this comment.
Re-reviewed bfc985c. The independent-holder takeover test now exercises the durable owner/token mismatch path correctly, and the misleading single-host-race comment is fixed. No implementation or test-adequacy concerns remain. The documentation correction is still incomplete: the new paragraph says Renew ErrLeaseHeld proves a competitor changed the durable owner/token, but memlease and k8slease also return it on bare expiry without a takeover. Please describe it as the backend definitive-loss signal, not proof of a competitor, and state explicitly that stricter backends may declare loss on expiry alone. The flock exception requires both the locally retained generation and the matching durable owner/token. This is the outstanding semantics correction from the previous review, not a request to change other backends. Current head CI is green; no local tests or linters were run.
| the lease release. | ||
| - A `Renew` returning `ErrLeaseHeld` means another process took over (the TTL | ||
| lapsed). The renewer goroutine cancels the run so the rogue-run scenario | ||
| - A `Renew` returning `ErrLeaseHeld` means the backend has definitively lost the |
There was a problem hiding this comment.
This still asserts that ErrLeaseHeld proves a competitor changed the record. memlease.Renew and k8slease.Renew return it for expiry alone, even with matching owner/token. Suggested wording: A Renew returning ErrLeaseHeld is the backend definitive-loss signal and causes the renewer to cancel the run; it does not necessarily mean a competitor took over. Backends may treat bare expiry as loss. flocklease can instead reclaim an expired lease when it still retains the local generation and the durable owner/token match under the transition lock. A changed owner/token or missing local generation remains loss. This resolves the original documentation request without changing any backend behavior.
The prior wording claimed Renew's ErrLeaseHeld always means a competitor changed the durable owner/token. memlease and k8slease return it on bare TTL expiry alone, with owner/token still matching. Restate it as the backend's definitive-loss signal, call out that memlease/k8slease treat expiry itself as loss, and keep flocklease's reclaim exception scoped to local-generation-retained + owner/token-matched. Co-Authored-By: Claude Sonnet 5 <[email protected]>
JAORMX
left a comment
There was a problem hiding this comment.
Re-reviewed f97590d, including the latest documentation-only correction. The requested canonical guide now distinguishes definitive backend loss from competitor takeover, documents bare-expiry loss in memlease/k8slease, and requires both retained local generation and matching durable owner/token for flock reclamation. The independent-holder takeover regression also resolves the earlier test gap. No blockers remain; approving. The final local-generation/on-every-backend sentence could be scoped more precisely to flock, but that is optional wording polish. CI is still completing on this latest documentation commit, with no failures observed; no local tests or linters were run.
Summary
flocklease.Renewtreated any expired lease record as definitive loss(
port.ErrLeaseHeld), even when the record still named the calling process asowner at its own fencing token. On a single-host deployment, a process
suspended past the TTL (e.g. a laptop going to sleep) lost its lease on the
next
Renewafter resume, even though nothing else could possibly have racedit — a single host, and no competing process ever ran while this one was
suspended.
renewLoopthen treated that as immediate, ungraced loss andcancelled the run.
The safe invariant isn't "the flock generation handle is still open" (
Acquirecan take over an expired record regardless of whether the old holder's flock
is still technically held) — it's that the durable record, read under the same
stable transition lock
Acquire/Releasealready use, still shows the sameowner at the same fencing token. On a single host that can only be true if
nobody else raced an
Acquire/takeover in the interim, soRenewnow reclaimswith a fresh expiry (token unchanged) instead of declaring loss. A record whose
owner or token changed during the gap (a genuine takeover) still returns
ErrLeaseHeldunconditionally.Reviewed by a go-architect + kubernetes-operator-expert panel before
implementation (confirmed this does not generalize to
k8slease/grpcdriver— those backends have no equivalent local proof of exclusivity,and a genuine TTL blowout losing the lease there is correct behavior), and by
a full four-axis panel review (Spec/Standards/Test-adequacy/Domain) after
implementation — no blockers found; the one important finding (an untested
held == nilhard-fail branch) is addressed in the second commit.Development stage
Contract linkage
Renew/ErrLeaseHeldcontract intent; introduces no new durable architecture decision, no public API/persistence/trust-boundary change.internal/adapter/flocklease+ a narrowing doc-comment clarification onengine/port/lease.go(no exported signature changed,task api:checkunaffected). No gRPC/protobuf, CLI/config, or persistence-format change.Interface conformance
port.SessionLease.Renew's doc comment is clarified, not widened: bare expiry with the record still naming the caller's owner+token is no longer, by itself, defined as loss — only a holder/token change is. This narrows (never widens) when an implementation may returnErrLeaseHeld; every other backend (k8slease,grpcdriver,memlease) is untouched and still treats bare expiry as loss.Issue relationship
Fixes #1333
Type of change
Test plan
Baseline checks
golangci-lint run --config .golangci.yml ./internal/adapter/flocklease/...and./engine/port/...)go test ./internal/adapter/flocklease/... -race -count=1and./engine/port/... -race -count=1)engine/CHANGELOG.md+docs/adr/0027-cloud-native.mdtouched;matlatl check . --strictrun clean (372 documents, 3553 references, 0 broken)engine/port/lease.go, no exported signature moved/panel-review-equivalent four-axis panel run manually (see PR discussion) —PANEL: ship_blockers=0 important=1 advisory=6 reviewer_failures=0; the one important finding is fixed in the second commitFull-repo
task lint/task testintentionally not run in this environment (disk space constrained during development); scoped checks above cover every touched package plus a fullgo vet ./.../go build ./...sanity pass on both modules. CI will run the full gates.Changes
engine/port/lease.goRenew's doc comment: bare expiry with the record still naming the caller is not itself lossinternal/adapter/flocklease/flocklease.goRenewreclaims an expired-but-uncontested lease instead of returningErrLeaseHeldinternal/adapter/flocklease/flocklease_test.goheld == nilhard-failengine/CHANGELOG.mdRenew/ErrLeaseHeldcontractdocs/adr/0027-cloud-native.mdUser-facing change
A
mecated/mecak8sdeployment using the single-host--session-lease-dirbackend no longer loses a session's write access every time the host process is suspended (e.g. a laptop going to sleep) past the lease TTL, as long as nothing else actually took over the session in the meantime.Special notes for reviewers
Scope is deliberately narrow:
k8sleaseandgrpcdriver/sessionlease.goare untouched. A genuine TTL blowout on those backends should still lose the lease — there is no local proof of exclusivity there the way flock's stable transition lock provides. See #1333 and #1334 for the companion fix (a separate, orthogonal issue: once a loss is declared, for any reason, the resulting tombstone was permanent for non-StateRunningsessions).