Skip to content

fix(flocklease): reclaim expired lease when record still names caller - #1343

Merged
JAORMX merged 4 commits into
mainfrom
worktree-agent-a8b342280a6346555
Sep 14, 2026
Merged

JAORMX merged 4 commits into
mainfrom
worktree-agent-a8b342280a6346555

Conversation

@jhrozek

@jhrozek jhrozek commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Summary

flocklease.Renew treated any expired lease record as definitive loss
(port.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. a laptop going to sleep) lost its lease on the
next Renew after resume, even though nothing else could possibly have raced
it — a single host, and no competing process ever ran while this one was
suspended. renewLoop then treated that as immediate, ungraced loss and
cancelled the run.

The safe invariant isn't "the flock generation handle is still open" (Acquire
can 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/Release already 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.

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 == nil hard-fail branch) is addressed in the second commit.

Development stage

  • Spike / Routine — acceptance-plan spine exempt; Spike evidence does not ship as-is

Contract linkage

  • Work classification: Routine — mechanical, reversible bug fix restoring the already-documented Renew/ErrLeaseHeld contract intent; introduces no new durable architecture decision, no public API/persistence/trust-boundary change.
  • Classification rationale: Single-file behavioral fix scoped to internal/adapter/flocklease + a narrowing doc-comment clarification on engine/port/lease.go (no exported signature changed, task api:check unaffected). No gRPC/protobuf, CLI/config, or persistence-format change.
  • Decision record: None — bug fix, not a new architectural decision.
  • Human waiver of spine: No
  • Acceptance plan: N/A (Routine)
  • Human decisions resolved and recorded: N/A
  • Plan / Interface PR: N/A
  • Approved commit baseline: N/A
  • Combined/exemption rationale: N/A (Routine, not Combined)

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 return ErrLeaseHeld; every other backend (k8slease, grpcdriver, memlease) is untouched and still treats bare expiry as loss.

Issue relationship

Fixes #1333

Type of change

  • Bug fix

Test plan

Baseline checks

  • Linting (scoped: golangci-lint run --config .golangci.yml ./internal/adapter/flocklease/... and ./engine/port/...)
  • Offline test suite (scoped: go test ./internal/adapter/flocklease/... -race -count=1 and ./engine/port/... -race -count=1)
  • Markdown changed: engine/CHANGELOG.md + docs/adr/0027-cloud-native.md touched; matlatl check . --strict run clean (372 documents, 3553 references, 0 broken)
  • Guarded engine API affected: not applicable — doc-comment-only change to engine/port/lease.go, no exported signature moved
  • Final implementation review: /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 commit

Full-repo task lint/task test intentionally not run in this environment (disk space constrained during development); scoped checks above cover every touched package plus a full go vet ./.../go build ./... sanity pass on both modules. CI will run the full gates.

Changes

File Change
engine/port/lease.go Clarify Renew's doc comment: bare expiry with the record still naming the caller is not itself loss
internal/adapter/flocklease/flocklease.go Renew reclaims an expired-but-uncontested lease instead of returning ErrLeaseHeld
internal/adapter/flocklease/flocklease_test.go New/updated tests: reclaim-with-no-competitor, still-fails-after-genuine-takeover, held == nil hard-fail
engine/CHANGELOG.md Note the narrowed Renew/ErrLeaseHeld contract
docs/adr/0027-cloud-native.md Extend the flocklease resource-inventory bullet

User-facing change

A mecated/mecak8s deployment using the single-host --session-lease-dir backend 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: k8slease and grpcdriver/sessionlease.go are 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-StateRunning sessions).

jhrozek and others added 2 commits September 14, 2026 11:29
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]>
@jhrozek
jhrozek force-pushed the worktree-agent-a8b342280a6346555 branch from ae16acd to 35e9e02 Compare September 14, 2026 09:39

@JAORMX JAORMX left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 JAORMX left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 JAORMX left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@JAORMX
JAORMX merged commit 8c58b27 into main Sep 14, 2026
33 checks passed
@JAORMX
JAORMX deleted the worktree-agent-a8b342280a6346555 branch September 14, 2026 10:59
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Sep 14, 2026
2 tasks

This branch was successfully deployed

1 active deployment
Preview — f97590dc Deployed Sep 14, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

flocklease.Renew declares definitive loss on process suspend (laptop sleep), even with no competitor

2 participants