Skip to content

fix(bridge): the host runs a native or remote client's onDisconnect when its socket closes (#973) - #996

Merged
davideast merged 1 commit into
mainfrom
fix/host-relay-ondisconnect-on-close
Oct 15, 2026
Merged

davideast merged 1 commit into
mainfrom
fix/host-relay-ondisconnect-on-close

Conversation

@davideast

Copy link
Copy Markdown
Owner

The Node host now runs a native or Node remote client's RTDB onDisconnect operations when that client's socket closes, as ADR-0019's heartbeat amendment requires. Part of #973.

These are the frames a Kotlin, Swift or Flutter client sends on the relay leg (no transport, its own clientSessionId), from the new fixture packages/cli/test/serve/fixtures/hosted-sqlite-relay-ondisconnect.ts:

const socket = new WebSocket(`ws://127.0.0.1:${port}`);
socket.send(JSON.stringify({ type: 'attach', protocol: 1, clientSessionId: 'phone', clientInfo: { platform: 'kotlin' } }));
socket.send(JSON.stringify({ type: 'worker-op', id: 'op-1', op: { method: 'auth.signInAnonymously' } }));
socket.send(JSON.stringify({ type: 'worker-op', id: 'op-2', op: { method: 'rtdb.set', path: 'status/phone', value: 'online' } }));
socket.send(JSON.stringify({ type: 'worker-op', id: 'op-3', op: { method: 'rtdb.onDisconnectSet', path: 'status/phone', value: 'offline' } }));

socket.close(1000);
// status/phone now reads 'offline'. On main it stayed 'online' until the host exited.

// Re-attaching with clientSessionId 'phone' finds the same anonymous user:
// running the onDisconnect operations kept the session.

A relay consumer's close now tells the host the session was interrupted

Before this change, a relay consumer's close released its listeners, failed its operations in flight and dropped it from presence, but sent the host nothing. Only worker-client-disconnect closes a relay port, and no native client sends it, so their onDisconnect operations never ran on the Node host: not on a clean close, not when the app was killed, and not when the host closed a silent socket.

Both closes now go through one Bridge.interruptConsumer(clientSessionId, socket?), which the web client's worker session detach also uses. It fails that socket's operations, drops the presence entry, and sends worker-client-interrupted. The hosted runtime already handles that frame for any port: it runs drainPortRtdbDisconnects and keeps the port, so the signed-in user survives for re-attach.

  • A replaced socket runs nothing. When the client has re-attached the session on a newer socket before the old socket's close is processed, the old close settles only its own operations, as before.
  • Only a peer advertising worker-port is sent the frame. That is the Node host. The in-page sandbox peer (bridge mode) does not accept it, so a consumer attached through a bridge-mode tab still runs no onDisconnect operations on close, as the ADR states.
  • HOSTED_CLIENT_LIVENESS_TIMEOUT_MS (45 s, three heartbeats) moves into bridge/protocol.ts; the web hosted client uses it in place of its local constant, with no behavior change. The Node remote client's heartbeat (lane R) uses it next.

Risk

  • onDisconnect runs on every relay close, including one the client recovers from a second later, such as a host restarted in place. This matches production and is decision 2 of the plan. Native clients have no .info/connected to re-register presence (Native clients have no .info/connected, so presence cannot re-arm after a drop #990), so an app's presence stays offline after a reconnect until it writes it again.
  • Clients built before the heartbeat are still not watched, but their close now runs their onDisconnect operations, since the rule is in the host.
  • The web client's detach path changed shape only: it now sends worker-client-interrupted inside a try and only to a worker-port peer, which is the only peer that can hold a worker-port session.

Integration evidence

Current evidence is the real-ws fixture hosted-sqlite-relay-ondisconnect.ts, registered in hosted-sqlite.test.ts: a real ws server and client on the real hosted runtime, with the liveness window shortened to 400 ms. It covers a clean close, a client that pings once and goes silent (terminated, close code 1006), and a re-attach that keeps the Auth user. Against main it fails at its first step: onDisconnect did not run after a clean close.

The integration test through real entry points is lane E of the #973 plan, test/apps/host-closes-silent-consumer: a node --import @pyric/cli/register process paused with SIGSTOP against the dev server. It needs both this change and the Node remote client's heartbeat (lane R), so it follows in its own PR once both merge.

Implementation notes
  • packages/cli/src/bridge/server/bridge.ts: interruptConsumer extracted from workerSessions.detach, gated on WORKER_PORT_CAPABILITY, and exposed on Bridge.
  • packages/cli/src/bridge/server/peer.ts: ConsumerSession.detach() calls interruptConsumer when the socket still owns the session, and detachConsumer otherwise.
  • packages/cli/src/bridge/server/peer-liveness.ts: header comment says who heartbeats now and what a close runs.
  • Conformance: evidence text on the existing ondisconnect-set-remove rows of rtdb-kotlin.ts, rtdb-swift.ts and rtdb-flutter.ts names the host fixture. No rows added or removed, so no assurance-node-counts change. The lane E scenario will be added to that evidence when it lands.
  • New unit cases in packages/cli/test/bridge/peer-liveness.test.ts: a relay close sends one interruption; a relay consumer that pings and goes silent is terminated and interrupted; a replaced relay socket's close sends none; an in-page peer (relay capability only) is sent none. The quiet-consumer case is renamed to say a client that never pings, such as an older build, is not closed. The three behavior cases fail against main's peer.ts.
  • Dual-plane parity (AGENTS rule 3): no SDK surface or worker protocol shape changed; worker-client-interrupted already existed.

Verification:

  • bun test test/bridge/peer-liveness.test.ts: 12 pass.
  • bun test test/serve/hosted-sqlite.test.ts -t "liveness|#973": 3 pass, five runs in a row.
  • bun test test/bridge test/remote: 1595 pass, 0 fail.
  • bun test test/serve: 2029 pass, 8 skip, 0 fail.
  • bun test test/unit test/rtdb test/register test/conformance: 196 pass.
  • packages/conformance: conformance-model.test.ts 29 pass; tsc --noEmit clean.
  • packages/cli: tsc --noEmit clean.

…hen its socket closes (#973)

When a relay consumer's socket closed (Kotlin, Swift, Flutter, or the Node
remote client), the bridge released its listeners, failed its operations
in flight and dropped it from presence, but never told the host the session
was interrupted. Its RTDB onDisconnect operations ran only when the host
exited.

The close now goes through one Bridge.interruptConsumer, shared with the web
client's worker session detach: it fails the socket's operations, drops the
presence entry, and sends worker-client-interrupted to a peer that
advertises the worker-port capability (the Node host). The host runs the
session's onDisconnect operations and keeps the session and its signed-in
user for re-attach. A socket whose session was already re-attached on a
newer socket runs nothing. The in-page sandbox peer does not accept the
frame and is not sent it.

HOSTED_CLIENT_LIVENESS_TIMEOUT_MS (45 s) moves into bridge/protocol.ts, and
the web hosted client uses it in place of its local constant.

Against main, the new real-ws fixture hosted-sqlite-relay-ondisconnect
fails at its first step: "onDisconnect did not run after a clean close".
The app scenario through real entry points follows as a separate change
once the Node remote client heartbeats.
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