TL;DR

  • Demand is waiting work, in-flight work, open admissions, and a short connection reservation.
  • After 15 minutes with no demand, stop only when a single-host stop lease and the host agent’s activity fence both agree the box is idle.
  • An authorized client that hits an auto-stopped host gets 202 with ec2_waking, waits up to three minutes, and never retries an engine write or shell.

A shared remote host sitting at AWS status checks OK still burns money. Leaving it running forever is the easy path. Stopping it the moment the last request ends is the cheap path. Both break in different ways. The shape that survived starts from the constraints that do not move.

Four constraints that decide the power path

  1. Jobs live in the database across a stop. A stop must not drop pending or claimed work. Physical stop is confirmed before a restart is allowed.
  2. Auto-stop and manual stop are different. An auto-stopped host must still accept work and wake, even with a stale heartbeat. A manual stop, a delete, or a disabled workspace must stay stopped until an operator starts it.
  3. The host seals admission before AWS is asked to stop. The control plane alone cannot know whether a child process is still finishing. The supervisor on the VM owns that gate.
  4. A waking host is not a retryable write target. Clients may retry grant issuance. They must not replay engine writes, exec, or shell.

Those four rules almost write the rest of the design.

Demand is four things, not a heartbeat

A one-minute reconcile looks at runnable waiting work, in-flight work, open admissions, and a connection reservation. Heartbeat freshness is not demand. A host that auto-stopped with an old heartbeat still wakes when one of those four appears.

When an authorized Web, desktop, or mobile client asks for a grant against an auto-stopped host, the control plane first stores a three-minute access reservation and raises desired state to running (or marks start-after-stop if a stop is already in flight). Then it returns without minting the grant:

# After commit, enqueue reconcile. Do not issue the grant yet.
raise Ec2Waking(instance_id)
# API maps that to: 202 {"code": "ec2_waking"}

The client retries only the unissued grant:

/** Only unissued grants can be retried. Never replay an engine operation. */
export async function waitForEngineGrant(send: () => Promise<Response>) {
  const deadline = Date.now() + 180_000;
  for (;;) {
    const response = await send();
    if (response.status !== 202) return response;
    const pending = await response.clone().json().catch(() => null);
    if (pending?.code !== "ec2_waking") return response;
    if (Date.now() >= deadline) throw new Error("Engine startup timed out");
    await new Promise((r) => setTimeout(r, 1000));
  }
}

On a real host this returned to a usable grant in tens of seconds once AWS and the host agent were ready.

Idle stop needs a lease and a fence, not a timer alone

Fifteen minutes without demand only starts the stop attempt. The control plane takes a single-host stop lease so two workers cannot race StopInstances. It then asks the host agent to prepare an automatic idle stop.

The host agent’s idle gate seals new admission only when owners report idle and the last non-observation activity is older than fifteen minutes. A busy refusal does not start a drain and does not close admission. Once sealed, retries never reopen it until the process restarts:

// PrepareAutomaticStop: busy refusal never starts a drain.
func (g *IdleGate) PrepareAutomaticStop(w http.ResponseWriter, r *http.Request) {
    g.mu.Lock()
    defer g.mu.Unlock()
    _, idle := g.activityLocked()
    if g.deployment || g.draining || !idle ||
        (!g.sealed && g.now().Sub(g.lastActivity) < 15*time.Minute) {
        w.WriteHeader(http.StatusConflict)
        return
    }
    g.sealed = true
    w.WriteHeader(http.StatusNoContent)
}

Observation traffic (read-only event streams) does not keep the host alive. Explicit stop drains first, then seals when owners finish. After a wake, the prior boot’s stop lease is cleared before readiness is trusted again.

Stopped is not the same as AWS OK zero

Host monitoring uses -1 for a deliberately stopped instance so it is not confused with AWS status checks reporting 0 (OK) on a running host. Faults that stay up for three minutes — power, host agent, control, observation gaps, create stalls, a five-minute resync gap — notify. A normal auto-stop does not.

Retired execution targets still holding pending rows are dropped from demand so they cannot keep a VM warm forever. Claimed and running work on a retired target still blocks stop.

The constraints above are why the path is longer than “cron + StopInstances”. The timer is only the first gate. The lease and the activity fence are what make stop safe enough that demand can wake the same host again without replaying work.