Repository navigation
fix(bridge): the host runs a native or remote client's onDisconnect when its socket closes (#973) - #996
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 ownclientSessionId), from the new fixturepackages/cli/test/serve/fixtures/hosted-sqlite-relay-ondisconnect.ts: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-disconnectcloses 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 sendsworker-client-interrupted. The hosted runtime already handles that frame for any port: it runsdrainPortRtdbDisconnectsand keeps the port, so the signed-in user survives for re-attach.worker-portis 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 intobridge/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
.info/connectedto re-register presence (Native clients have no .info/connected, so presence cannot re-arm after a drop #990), so an app's presence staysofflineafter a reconnect until it writes it again.worker-client-interruptedinside atryand only to aworker-portpeer, which is the only peer that can hold a worker-port session.Integration evidence
Current evidence is the real-
wsfixturehosted-sqlite-relay-ondisconnect.ts, registered inhosted-sqlite.test.ts: a realwsserver 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. Againstmainit 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: anode --import @pyric/cli/registerprocess paused withSIGSTOPagainst 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:interruptConsumerextracted fromworkerSessions.detach, gated onWORKER_PORT_CAPABILITY, and exposed onBridge.packages/cli/src/bridge/server/peer.ts:ConsumerSession.detach()callsinterruptConsumerwhen the socket still owns the session, anddetachConsumerotherwise.packages/cli/src/bridge/server/peer-liveness.ts: header comment says who heartbeats now and what a close runs.ondisconnect-set-removerows ofrtdb-kotlin.ts,rtdb-swift.tsandrtdb-flutter.tsnames the host fixture. No rows added or removed, so noassurance-node-countschange. The lane E scenario will be added to that evidence when it lands.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 againstmain'speer.ts.worker-client-interruptedalready 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.ts29 pass;tsc --noEmitclean.packages/cli:tsc --noEmitclean.