Skip to content

fix(server): let CloseSession clear a stale lost-lease tombstone - #1266

Merged
jhrozek merged 2 commits into
mainfrom
fix/session-lease-tombstone
Sep 8, 2026
Merged

jhrozek merged 2 commits into
mainfrom
fix/session-lease-tombstone

Conversation

@jhrozek

@jhrozek jhrozek commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Summary

  • A process that ever definitively lost a session's cross-process lease (ADR 0027 Phase 4 — Service.onLeaseLost sets lostOwnership[id]) could never use that session again, even after the lease was actually free. CloseSession is the documented recovery path, but closeSessionAuthorized always tried to reaffirm the lease first — which fails the exact same way every time once lostOwnership[id] is set — so closeSessionLocal (the only place that clears the tombstone) was never reached.
  • Fix: when lostOwnership[id] is already set, closeSessionAuthorized skips the impossible reaffirm/settle step and goes straight to closeSessionLocal, which is already safe to call in that state. A subsequent run then goes through the normal reaffirmLease path and acquires cleanly if the lease is genuinely free (or is correctly refused again if someone else really holds it).

Development stage

  • Trivial / mechanical — acceptance-plan spine exempt

Contract linkage

  • Acceptance plan: N/A
  • Human decisions resolved and recorded: N/A — single-function bug fix restoring already-documented recovery behavior (docs/adr/0027-cloud-native.md List 1 row 27); no new interface, no behavior change beyond making the documented CloseSession recovery path actually work
  • Plan / Interface PR: N/A
  • Approved commit baseline: N/A
  • Combined/exemption rationale: N/A

Interface conformance

  • No public/wire interface change. port.SessionLease, all backends (flocklease/k8slease/grpcdriver/memlease), and every other lease code path (reaffirmLease, onLeaseLost, renewLoop) are untouched.

Issue relationship

Relates to a live bug found while investigating a "session is leased by another process" report on a local mecatui instance whose renewer missed ticks (host sleep) past the lease TTL.

Type of change

  • Bug fix

Test plan

Baseline checks

  • Linting (task lint scoped to internal/adapter/server via golangci-lint run ./internal/adapter/server/... --config .golangci.yml — 0 issues)
  • Offline test suite — go test ./internal/adapter/server/... -race passes except one pre-existing, unrelated failure (TestSDKTypescriptRelease_Scenario2_HTTPCodecParity), confirmed present on main before this change too (stashed the diff and re-ran to verify)
  • Offline demo (go run ./cmd/mecademo) — not run; change is scoped to internal/adapter/server, no engine/demo path touched
  • Markdown changed: N/A
  • User docs/user-facing behavior changed: N/A (internal recovery-path fix, no new user-facing surface)
  • Guarded engine API affected: N/A (no engine/ change)
  • Final implementation review: not yet run

Changes

File Change
internal/adapter/server/service.go closeSessionAuthorized skips reaffirm/settle when lostOwnership[id] is already set, going straight to closeSessionLocal so the tombstone actually clears
internal/adapter/server/lease_test.go Add TestCloseSessionClearsLostOwnershipAndReacquires (proves the fix) and TestCloseSessionStillRefusedWhileLeaseHeldElsewhere (regression guard: a lease genuinely held elsewhere must still be refused)
internal/adapter/server/session_mutation_inventory.go Update closeSessionLocal's rationale comment to note the new direct call path from closeSessionAuthorized

User-facing change

None directly — but a mecatui/mecated process that ever loses a session's local flock-backed lease (e.g. due to the host sleeping past the lease TTL) can now recover it via CloseSession instead of requiring a full process restart.

Special notes for reviewers

The tombstone's permanence-until-explicit-close is intentional (an existing pinned test, TestADR_0294_AwaitingLeaseLossRetractsLocalAskPreservesSnapshot, requires a lost lease to stay refused absent an explicit close) — this PR does not weaken that. It only fixes the fact that the explicit-close path itself was unreachable once the tombstone was set.

The engine/provider engine-version and x/sync bumps merged via dependabot
(#1246, #1247, #1248, #1249, #1250) left root's go.mod/go.sum out of sync
with what GOWORK=off resolution needs, breaking `task e2e:k8s` the same way
9991e54 fixed previously: `go: updates to go.mod needed, disabled by
-mod=readonly`. Re-run `task tidy` to bring go.mod/go.sum back in sync.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
closeSessionAuthorized always reaffirmed the session lease before tearing
down local state, but a process that already lost its lease (lostOwnership
set by onLeaseLost) fails that reaffirm the same way every time, so
closeSessionLocal — the only place that clears the tombstone — was never
reached. CloseSession is the documented recovery path for a lost lease
(ADR 0027 Phase 4), so once a process ever lost a session's lease it could
never touch that session again without a restart, even after the lease
became free.

Skip the impossible reaffirm/settle step when lostOwnership[id] is already
set and go straight to closeSessionLocal, which is already safe to call in
that state. A subsequent run then goes through the normal reaffirmLease path
and re-acquires cleanly if the lease is actually free.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@jhrozek
jhrozek force-pushed the fix/session-lease-tombstone branch from 16c4c0c to 8b749ec Compare September 8, 2026 20:09
@jhrozek
jhrozek merged commit 77e2b32 into main Sep 8, 2026
33 checks passed
@jhrozek
jhrozek deleted the fix/session-lease-tombstone branch September 8, 2026 20:32
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.

1 participant