Repository navigation
Description
Bug description
flocklease.Renew (internal/adapter/flocklease/flocklease.go) treats any expired
lease record as definitive loss (port.ErrLeaseHeld) — even when the record still
names the calling process as owner at its own fencing token. On a single-host
deployment (mecated/mecak8s with --session-lease-dir), if the host process is
suspended (laptop sleep) for longer than the lease TTL (default 30s — trivially
exceeded by any real sleep), the very next renew after resume finds the record
expired and returns ErrLeaseHeld, even though nothing else could possibly have
taken over: it's a single host, and no competing process ever ran while this one was
suspended.
renewLoop (internal/adapter/server/service.go) treats ErrLeaseHeld as immediate,
ungraced loss (no retry/backoff, unlike other transient renew errors) and calls
onLeaseLost, which tombstones the session and cancels the run. See also
#1334 for why that tombstone then tends to be permanent.
Steps to reproduce
- Run
mecated(ormecak8s) locally with--session-lease-dir <dir>. - Start a session and begin a run (or park it awaiting a permission approval).
- Put the laptop to sleep for more than the lease TTL (default 30s) and wake it.
- Try to continue the session (send another prompt, or approve the pending ask).
Expected behavior
The renewer recognizes that no other process could have raced it on a single host —
the lease record still shows this process as owner at its own token — and reclaims
(re-extends) the lease instead of declaring loss.
Actual behavior
✗ stream error: rpc error: code = FailedPrecondition desc = server: session is leased by another process: "<session id>"
The run is cancelled and the session gets stuck: further attempts to resume/add to
it keep failing with the same error (see #1334 for the reason it doesn't
self-heal). This reproduces every time the laptop sleeps.
Environment (if relevant)
- Deployment shape:
mecated/mecak8swith the single-hostflockleasebackend
(--session-lease-dir).
Additional context
Root cause and proposed fix (reviewed by a go-architect + kubernetes-operator-expert
panel):
- The safe invariant is not "the flock generation file handle is still open" (that
can be invalidated from under a live process —flocklease.Acquireitself takes
over and unlinks the old generation file once a record is expired, even if the old
holder's flock is still technically held). The safe invariant is: the durable
record, read under the existing stable transition lock, still shows the same owner
at the same fencing token. On a single host, that can only be true if nobody else
ever raced anAcquire/takeover in the interim — proven by the same CAS-style logic
Acquire/Releasealready use to bump/clear it. - Fix: in
flocklease.Renew, when the record is expired but owner+token still match
the caller (and the caller's local generation handle is still tracked — not
previously relinquished), rewrite the record with a fresh expiry and return success,
instead ofErrLeaseHeld. Keep the token unchanged (matchesRenew's existing
"same token" contract andRelease's tombstone invariant). A record whose
owner/token did change during the gap must still returnErrLeaseHeld— that is
a genuine takeover. - This does not generalize to the k8s (
coordination.k8s.io/v1) or Redis/gRPC
backends: there, a real second replica can legitimately race and win anAcquire
during a comparable gap (pod eviction, API-server unavailability during a
control-plane upgrade, CFS throttling, GC pauses), and there is no equivalent local
proof of "nobody else could have taken it." A genuine TTL blowout on those backends
losing the lease is correct behavior, not something to suppress. engine/port/lease.go'sSessionLease.Renewdoc comment should be clarified: bare
expiry with the record still naming the caller at the caller's token is not, by
itself, in the enumerated definitive-loss conditions — loss is specifically a
holder or token change. This narrows (never widens) when implementations may return
ErrLeaseHeld, so it's a safe clarification for existing callers.- Add a table test in
flocklease: advance the fake clock past TTL, then (a) no
competitor raced —Renewsucceeds, token unchanged, expiry extended; (b) a
competitorAcquire'd during the gap —Renewstill returnsErrLeaseHeld.
Activity
fix(flocklease): reclaim expired lease when record still names caller
test(flocklease): cover held==nil hard-fail regardless of record state