Repository navigation
fix(ws): probe a silent socket a few seconds after a send - #2838
Conversation
A submit, insert or pending action that hears nothing from the socket within PROBE_TIMEOUT_MS starts the liveness probe at once, instead of waiting for the idle check (up to IDLE_PROBE_MS away). After a network change that leaves the old socket half-open, a message sent once the network is back no longer sits unanswered for about half a minute.
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 38 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
Review: the send timer could start or resume a probe while the page was hidden, past the rule that a hidden page is probed when it is shown again.
|
Review round (omo: CLEAR at 0941b8b; Codex gpt-6.1-terra xhigh: two MEDIUM, no HIGH).
Validation for 904b373: |
|
Merge note: part of the desktop release; owner decisions delegated to the lead. omo CLEAR at this head, Codex rounds done (no reproduced HIGH left), required checks green. CodeRabbit skipped or rate-limited; findings and resolutions are in the PR comments. |
## What After a network loss that leaves the old connections half-open (a phone changing networks, a laptop waking elsewhere), the agent's reply showed in Chat 30-60 s after the agent had written it, although its `conversation-changed` push reached the page at once (#2839). Traced in a real browser against an isolated server behind a proxy that silences every open connection, as such a loss does: 1. While the network was gone, the page kept sending reads: the chat's newest page, worktree lists, omo tasks, health and machine checks. Each went out on a connection that never answers again. 2. The browser keeps at most six connections to a host. Those stuck reads held all of them, so every later request queued behind them, the chat's reads included, even after the WebSocket was replaced and the push for the reply had come. 3. The queue only moved when the browser finally gave the dead connections up (here about 30 s after the network came back), and then every queued request completed at once. A smaller part: the chat's own newest read, if sent before the reconnect, kept the chat's serial read lane busy until its 15 s deadline, so the push behind it waited too (the usual ~3 s gap). ## Change - `ws.ts` / `transport.ts`: when a connection was dropped because a probe went unanswered (the silent-loss signal from #2838), the next connection that opens gives up the native GET/HEAD reads still in flight (`abandonNativeReads`). This frees the browser's connections, so later requests open fresh ones. This server's own socket going silent gives up every read (it is the browser's path to the server); another PC's socket gives up only that PC's `/api/machines/<id>/` reads. Only this server's `/api/` reads are tracked; writes are never given up (the server may have done them), a caller's own abort still reaches the request and its body, and the relay transport is untouched (its requests ride the relay session). - `ConversationRefresh.restart()`: the chat's read on reconnect gives up the newest read in flight instead of queueing behind it. `ChatView` treats a read given up this way as no failure (no error flash, no failure count). ## Measurements perf-everyday's harness (isolated server: test herdr session, temp state and HOME, port 0; headless Chromium on the built app behind a TCP proxy; a stand-in agent writes a reply to the transcript for each message). The proxy silences all open connections for 3 s and keeps the old ones dead. Numbers are from the moment the agent's reply is in the transcript until it is on screen, ABBA, 4 rounds x desktop and phone per arm (A = origin/main, B = this branch): | arm | desktop gap (ms) | phone gap (ms) | |---|---|---| | A | 2734, 2941, 2393, 2820 | 2877, **28942**, 3207, 3247 | | B | 32, 140, 7, 21 | 36, 0, 50, 65 | | B | 63, 63, 1, 12 | -10, 12, 26, 0 | | A | 3086, 3102, 2812, 3173 | **52354**, 3175, 3109, 3134 | A: usually ~3 s, and 30-60 s in 2 of 16 rounds (4 of 16 and 3 of 16 in two earlier runs). B: at most 0.14 s in all 16 rounds, no tail. The time until the message reaches the agent is unchanged (9.5-10 s, set by #2838's probe). An intermediate version that only re-sent the chat's read every 3 s after a reconnect did not help on the phone: the new reads queued behind the other stuck requests, which is how the pool cause was found. ## Tests - `src/lib/transport.test.ts`: in-flight native reads are given up; a write, a read sent later and a caller's own abort behave as before. - `src/lib/ws.test.ts`: a connection dropped for an unanswered probe gives up the reads in flight when its replacement opens; a reconnect after a plain close gives up nothing. - `src/lib/conversationFetch.test.ts`: `restart()` gives up a newest read sent before a reconnect and reads again at once. Each failed before the change (missing export / no abort / `restart is not a function`). ## Validation - `bun run check fast` at 1731371: all ok (unit 11364 pass, 0 fail; typecheck, build, generated files, workflow syntax). The head merges origin/main, which touched only app/electron. - `bun test src/lib/api-request-id.test.ts src/lib/conversationFetch.test.ts src/lib/transport.test.ts src/lib/ws.test.ts src/components/ChatView*`: 194 pass. - ABBA above (loopback, desktop and phone). Not measured over the hosted relay: this change does not touch the relay transport's requests. Fixes #2839.
What
After a network drop that leaves the old WebSocket half-open (a phone moving from Wi-Fi to
cellular, a laptop waking on another network), a message sent once the network is back went into
the dead socket and nothing happened until the idle check probed the connection: up to
IDLE_PROBE_MS(20 s) after the last frame, checked every 5 s, then two 3 s pings before thedrop. The user saw a sent message with no answer for about half a minute.
Now a submit (also an insert or a pending action) that hears nothing at all from the socket within
PROBE_TIMEOUT_MS(3 s) starts the existing liveness probe at once. A socket that answers anythingin that window is left alone, so a healthy connection sends no extra ping.
Measured
ABBA on an isolated server (test herdr session, temp state and HOME, port 0), headless Chromium,
the built app behind a TCP proxy that makes every open connection go silent for 3 s and keeps the
old ones dead afterwards. The case: the network comes back, the user sends a message, and sends
again whenever one is not answered within 8 s. Time from the network's return to the agent's
answer on screen:
Arms in order A, B, B, A on one quiet machine (32 cores, load under 8). The fix moves the time a
message reaches the agent from 28.1-28.4 s to 9.4-9.8 s, with no overlap.
The "reply on screen" column is not this change's measure: after a reconnect the chat sometimes
shows a reply the agent already wrote only tens of seconds later, on main as well (A: delivered at
28 s, shown at 54-57 s). That is a separate issue and stays out of this PR.
Tests
probes a socket that has said nothing since a submit left, without waiting for the idle check(fails on main: no ping until the idle check)
does not probe after a submit when the socket has answered meanwhileValidation
bun run check fast: passed (old-desktop, 242 s).bun test src/lib/ws.test.ts: 108 pass.check full(integration and browser lanes run in CI).