Skip to content

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

Closed

Description

@jhrozek

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

  1. Run mecated (or mecak8s) locally with --session-lease-dir <dir>.
  2. Start a session and begin a run (or park it awaiting a permission approval).
  3. Put the laptop to sleep for more than the lease TTL (default 30s) and wake it.
  4. 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/mecak8s with the single-host flocklease backend
    (--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.Acquire itself 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 an Acquire/takeover in the interim — proven by the same CAS-style logic
    Acquire/Release already 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 of ErrLeaseHeld. Keep the token unchanged (matches Renew's existing
    "same token" contract and Release's tombstone invariant). A record whose
    owner/token did change during the gap must still return ErrLeaseHeld — 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 an Acquire
    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's SessionLease.Renew doc 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 — Renew succeeds, token unchanged, expiry extended; (b) a
    competitor Acquire'd during the gap — Renew still returns ErrLeaseHeld.

Activity

  1. self-assigned this
    on Sep 10, 2026
  2. added 2 commits that reference this issue on Sep 14, 2026
    4789778
    35e9e02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions