Skip to content

Expire held leases orphaned by a daemon restart #13

Closed

Description

@V3RON

Problem

A held lease's liveness is its daemon connection. When the daemon restarts,
every held-lease connection is gone by definition — but StartupConverger
restores the persisted TTL timers anyway:

// src/core/startup-converger.ts:47
await this.options.timers.restoreExpiryTimers();

So after any daemon restart (crash, pitlane daemon stop, machine sleep,
config change) a held lease survives with no holder at all, keeping its
device out of circulation for up to a full heldTtlBackstopMs.

The client side is equally stuck, but that half now lives in #35: the MCP
session caches a DaemonConnection that is dead after the restart and never
reconnects. This issue owns the daemon side only — see Notes.

This gets worse, not better, once #12 lands: a heartbeat-backed lease that
loses its daemon has no way to be re-established.

Proposed change

  • On startup, classify persisted leases. A held lease cannot have a live
    connection across a restart, so release it (reason killed) instead of
    re-arming its timer. detached leases keep today's timer restoration —
    their liveness is the TTL, not a connection.
  • Ordering matters: this must run before running-capacity convergence so the
    freed devices are visible to it.

Notes

Depends on #12 for the "connection liveness is the lease's liveness" model to
be fully coherent, but the startup half can land independently.

Acceptance criteria

  • Held leases persisted in state.json are released at daemon startup;
    detached leases still have their timers restored.
  • Devices freed this way are reclaimed and available to the next requester.
  • lease.released is emitted for each, with a reason distinguishing
    startup orphan cleanup.
  • docs/ARCHITECTURE.md startup-convergence description updated.
  • pnpm check green.

Activity

  1. added theissue type on Aug 19, 2026
  2. V3RON commented on Aug 20, 2026

    @V3RON
    ContributorAuthor

    Overlaps #35 (MCP server never recovers from a daemon restart), which I filed before spotting that this issue's problem statement already names McpSession.#connectionForUse().

    Worth settling here first, because the daemon-side decision determines the client-side one. The two disconnect paths are not symmetrical:

    • MCP process dies, daemon alive — DaemonServer releases that connection's heldLeaseIds (src/daemon/server.ts). The lease is gone.
    • Daemon restarts — StartupConverger.converge() calls restoreExpiryTimers() (src/core/startup-converger.ts), so the held lease returns with no holder, as described above. The lease still exists.

    So a reconnecting MCP session on the restart path may find its old lease alive and orphaned. Either this issue expires orphaned held leases at startup — and then #35 simply clears #ownedLease and fires onLeaseLost — or the session re-adopts the lease, which needs a daemon-side way to re-attach a lease to a new connection that does not exist today.

    Suggest deciding that here and treating #35 as the client half, so the two aren't built against contradictory assumptions.

  3. added 2 commits that reference this issue on Aug 20, 2026
    91c1d41
    39314e8
  4. V3RON commented on Aug 20, 2026

    @V3RON
    ContributorAuthor

    Correcting one thing in the problem statement above, now that this has
    been tested end to end against a real daemon.

    The issue lists the triggering scenarios as "crash, pitlane daemon stop,
    machine sleep, config change". pitlane daemon stop does not belong on
    that list. DaemonServer.#stop() calls #releaseHeld(connection) across
    every connection before the process exits, releasing each held lease with
    reason closed, so a graceful stop already frees the device and leaves
    nothing orphaned in state.json.

    This surfaced as a failed test rather than a code review. The first run
    followed the issue's own framing — take a held lease, daemon stop,
    restart, assert the orphan release — and found state.json had an empty
    leases array before the restart even happened. There was nothing to
    release because the graceful shutdown had already released it.

    Re-running with kill -9 against the daemon reproduced it immediately:
    lease.released with reason: "orphaned", the device back to ready,
    and the same simulator re-leased afterwards.

    So the merged behaviour is correct, but its real scope is narrower than
    this issue describes: it covers daemon death that skips the shutdown path
    — a crash, an OOM kill, kill -9, power loss — and not any orderly stop.
    Worth knowing for anyone reading this issue as background later, and worth
    keeping in mind for #35, whose reconnect story only has to account for the
    ungraceful case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions