Skip to content

desk-hub: a machine's reconnect within a second, without a flap (#57) - #58

Merged
SSFSKIM merged 5 commits into
masterfrom
desk-reconnect-hub
Oct 18, 2026
Merged

SSFSKIM merged 5 commits into
masterfrom
desk-reconnect-hub

Conversation

@SSFSKIM

@SSFSKIM SSFSKIM commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

The hub's half of #57. With it, a machine's reconnect no longer flaps the machine's presence, and it starts within about a second of the hub losing the stream instead of at the next heartbeat's ack (up to 15 s). The web's half is in #(web PR). That PR keeps the transcript drawn through any disruption and stays quiet for a gap under 15 s.

Why the hub loses the stream

The evidence comes from both Macs' daemon.log, 10-17 05:46Z to 10-18 19:13Z: 53 hub-side drops in all.

  • 27 drops came from deploys, 2 from wrangler tail. Each landed 298–313 s after a deploy, on both machines within 9 s of each other. At the 19:07:27 deploy, mini dropped at 19:12:28 and macbook at 19:12:36.
    • Cloudflare keeps an active object on its old code for about five minutes. It then routes new requests to a successor.
    • Meanwhile the old instance's in-flight request runs on and keeps beating. That request is the machine's SSE stream. The docs say in-flight requests "are allowed to finish if they do not access a Durable Object's storage".
    • The machine's next POST reached a successor that held no stream, and was refused 409 stale-generation. Usually that POST was the hb-ack, sent up to 15 s later.
    • On 10-18, 11 deploys accounted for 22 of the 33 drops.
  • 24 drops hit one machine at a time, with nothing the client could see as a trigger. They were 409s, or clean ends of stream; workerd delivers an errored body as a clean end (workerd #6818).
    • They are either the platform replacing one object, or one of Machine.end's own paths.
    • The hub now logs every stream's end with its cause, which will tell these apart.
  • One 500 error code: 1101 came a minute after a deploy. It was a stub.fetch exception the Worker did not catch.
  • What was ruled out: skipped generations, local sleep, network faults.

What changes

  • Machine finds a replacement within about a second.
    • The instance that holds a stream asks once a second whether the object's own id still reaches it. The check is an RPC instance(), preceded by a storage read that throws on a replaced instance. If the id reaches another instance, it ends the stream, and the machine reconnects at once.
    • Why the probe and not the storage read alone: Cloudflare's docs say uniqueness "is enforced … upon accessing storage". Their only statement that a stale instance's access throws is a 2021 staff answer about the KV-backed storage. The message it quotes is in Cloudflare's closed runtime, not in workerd.
    • Whether a synchronous SQLite read on a replaced instance throws is not documented, and Miniflare cannot reproduce a replacement. So the read stays as a free first check, and the probe is what this relies on.
    • The probe's cost is one RPC a second per connected machine, on an object the stream already keeps active. That is about 5 M requests a month for two machines, around $0.60.
    • The suite proves the probe against a live object, in reconnect.test.ts: a live object's probe reaches itself.
  • A closed stream's lease holds the machine online for one heartbeat (15 s). Its next stream's hello takes the lease over, so a reconnect never shows the machine offline. What the stream carried still ends at once: its subscriptions end machine-offline, and its RPCs become uncertain.
  • A subscribe or a new RPC in that grace waits for the next stream and is carried to it at its hello, so an answer, a decision or an interrupt sent in the gap is not lost or refused. If the grace runs out first, the subscription ends machine-offline at n 0, and the RPC, which never reached the machine, fails machine-offline. A retry of an uncertain RPC is answered from its record in the grace, as while offline.
  • 409 bodies say "replaced": true when the POST names the generation the object last opened and the object holds no stream. The uplink already logs the body, so daemon.log shows the cause.
  • Each stream's open and end is logged with its cause ({"desk":"machine-stream",…}).
    • The causes are newer-stream, client-closed, write-failed, undeliverable, directory, revoked and replaced. A stale POST is logged too.
    • The lines appear in wrangler tail (which itself forces a replacement), or in Workers Logs if observability is turned on. That is not turned on here; it is the owner's call.
  • The Worker answers an object call's exception as JSON: 503 hub-unavailable when Cloudflare marks the error retryable or overloaded, 500 hub-error otherwise. Before this, the runtime answered with a bare 1101.
  • DX4 chunk bodies may carry t, the bridge's stamp, for the web's latency readout. The doperpowers side is sminos plane: a DX4 chunk carries the bridge's stamp t (MAWS #57) (7.183.2) doperpowers#246. The fake machine now stamps its chunks the same way.
  • The spec is amended:
    • DX2's lease paragraph and the POST answer;
    • DX4's subscriptions the hub ends itself;
    • DX6's machine-offline;
    • a Decision Log entry with the causes and what was rejected.

Storage

No new table, column, or key. A subscription or an RPC that waits for the next stream is kept in its existing table (subs, rpcs) with generation 0. That is a new value in an existing column; no stream is ever generation 0.

Tests

  • New: test/reconnect.test.ts. It covers:
    • a live probe reaches itself;
    • a replaced object ends its stream within the probe, the machine never reads offline, and a subscribe made in between reaches the next stream;
    • 409 replaced;
    • the grace holds, then lapses, ending a waiting subscription at n 0 and failing a waiting RPC;
    • a reconnect within the grace is no flap, and an RPC sent between streams reaches the next one;
    • an object's exception reaches the machine as JSON.
  • Changed: stream-close.test.ts. The RPC still turns uncertain at once. The machine now goes offline one grace later.
  • The full desk-hub suite passes locally: 492 tests before the new file's cases.
  • reviewer-high: no material findings.

Gates: static,unit,e2e:desk-web --class1a linux green at the head fc692dd: https://github.com/SSFSKIM/MAWS/actions/runs/39160629366.

  • test/latency.test.ts prints the hub's hops on the local runtime. Over three local runs, p95 was 3–72 ms per hop. A replaced object's stream ended 415–467 ms after the replacement. The next stream took a waiting subscription 20–31 ms after the reconnect. The machine never read offline.
  • The second review (reviewer-high, the RPC delta) also had no material findings.

Cloudflare replaces a Durable Object (a deploy's rollover about five
minutes after it, `wrangler tail`, a runtime update, a move) by routing new
requests to a successor while the old instance's in-flight request, the
machine's SSE stream, runs on and beats. The machine learned of it only when
its next POST, at worst the hb-ack 15 s later, reached the successor and was
refused 409 stale-generation: 27 of the 53 hub-side drops in both Macs'
logs (10-17/18) came 298-313 s after a deploy, both machines within 9 s.

- Machine: the instance holding a stream asks every second (PROBE_MS)
  which instance its own id reaches (and reads storage, which throws on a
  replaced instance) and ends the stream once it is another, so the machine
  reconnects within about a second. A 409 for the generation the object
  last opened says `replaced: true`. Every stream's open and end is logged
  with its cause (`{"desk":"machine-stream",...}`).
- Project: a closed stream's lease holds the machine online for a grace of
  one heartbeat (MachineContext.graceMs), so a reconnect never flaps its
  presence; what the stream carried still ends at once. A subscribe in the
  grace waits (route generation 0, a new value in the existing subs column)
  and is carried to the next stream at its hello; an RPC in the grace fails
  at once ("reconnecting: try again in a moment").
- Worker: an object call's exception is answered as JSON (503
  hub-unavailable when retryable or overloaded, 500 hub-error otherwise)
  instead of the runtime's bare 1101.
- DX4 chunk bodies may carry `t`, the bridge's stamp, for the web's latency
  readout.
- Spec: DX2's lease paragraph, the POST answer, DX4's hub-ended
  subscriptions, DX6's error code, and a Decision Log entry with the causes.
… call that never settles cannot stop the probing
…tream, as a subscription does; one that waits out the grace fails machine-offline (MAWS #57)
… printed each run (and to DESK_LATENCY_OUT), held to a loose bound
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