Skip to Content
Motoko

Closing idle instances

An open instance holds its Harness, its storage and its scope's lease. With closeIdleAfterMs, the runtime closes an instance once it has been idle that long: it closes the instance's streams and Harness, releases its lease (so another process can take the scope) and lets it go. Off by default.

Idle means all of these, continuously for closeIdleAfterMs since the last activity (a call's end, a reader's end, any change to pi-durable's task graph):

  • no call of the runtime's under way on the instance (open, createSession, listSessions, submit, continue, withdraw, approve, abort, compact, resume);
  • no event reader attached to any of its sessions;
  • harness.wake() is not active: no task can run now or is running, so no run is going (a run's generation or tool calls, queued inputs it will take, a compaction);
  • no sleep due within closeIdleAfterMs (harness.wake().at).

A call parked for an approval does not count: it is committed state, needs no process until its decision arrives, and approve() on a closed instance reopens it and delivers the decision. A nested call parked for one counts, through the call that awaits it, until it is decided or nestedApprovalTimeoutMs passes (see "Durable approval"). Inputs a failed run left queued wait for the next input either way, which reopens the instance.

Sleeping work. pi-durable's own timers wake sleeping tasks only while their Harness is open. An instance whose earliest sleep is due within closeIdleAfterMs stays open, and pi-durable wakes it. One whose sleep is due later closes, and the runtime reopens it at that time: with a process host it keeps a timer for it (the instance shows in runtime.inspect() as dormant with wakeAt, and the timer keeps a Node.js process alive, as pi's would); opening runs harness.resume(), which runs what is due. A reopen that fails is reported as reopen_failed and tried again, unless another writer holds the scope, which then runs it. With an alarm host the runtime arms the host with harness.wake() (active: false, and at when a sleep is due) just before it closes and keeps no timer: the host's alarm fires at at, the host calls instance.resume(), and that reopens the instance. If that arm fails, the instance stays open and the runtime tries again later.

Handles keep working. An AgentInstance or AgentSession the app holds stays valid: its next call reopens the instance transparently, and runtime.get returns that same handle while the app holds it. open() does not pin an instance open. Explicit close(), runtime.close(), a lost lease and a failed storage keep their meaning: the instance ends and never reopens (its calls reject with InstanceClosedError; runtime.get makes a fresh one).

Readers. An attached reader keeps its instance open however long it waits, so a server ends a reader (its signal) when its client goes away. A reader of an idle session sees nothing until the session's next commit. After an idle close the session's live log is gone; a client that reconnects with a cursor from before it reopens the instance and is caught up from committed state (no stream:reset, no repeats), exactly as after a restart.

Reopening races. A call that arrives while an idle close is still closing the Harness or releasing the lease waits for the release, then opens anew: it never fails with LeaseHeldError against its own instance, never opens a second writer, and is not lost. Every caller is counted before it touches the open Harness, so a close never starts under a call.

Observing. runtime.inspect() returns { open, instances }: how many instances hold (or are acquiring or releasing) a lease, and for each instance the runtime holds its scope, state, the calls under way, the readers attached and any wakeAt. instance.state says where one instance is.

Not done here: a cap on open instances. Work an instance left unfinished, or a sleep it closed with, runs when something next opens that instance in a new process; runtime.recover() (below) is that something at startup.