Skip to content

fix(ws): probe a silent socket a few seconds after a send - #2838

Merged
devswha merged 2 commits into
mainfrom
fix/send-probes-silent-socket
Nov 5, 2026
Merged

devswha merged 2 commits into
mainfrom
fix/send-probes-silent-socket

Conversation

@devswha

@devswha devswha commented Nov 5, 2026

Copy link
Copy Markdown
Owner

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 the
drop. 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 anything
in 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:

arm viewport reached the agent (s), 4 rounds reply on screen (s)
A desktop 28.3, 28.2, 28.2, 28.2 54.0, 28.2, 28.2, 53.8
A phone 28.2, 28.2, 28.2, 28.3 28.3, 28.2, 55.4, 56.8
B desktop 9.5, 9.5, 9.4, 9.4 12.8, 12.8, 12.9, 12.8
B phone 9.4, 9.8, 9.4, 9.4 12.9, 61.6, 61.3, 61.3
B desktop 9.7, 9.4, 9.5, 9.5 12.8, 12.7, 21.8, 61.6
B phone 9.4, 9.8, 9.7, 9.4 61.6, 61.6, 12.9, 61.8
A desktop 28.4, 28.2, 28.2, 28.2 28.3, 28.2, 28.2, 28.2
A phone 28.4, 28.2, 28.1, 28.2 57.0, 56.8, 28.1, 28.3

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 meanwhile

Validation

  • bun run check fast: passed (old-desktop, 242 s).
  • bun test src/lib/ws.test.ts: 108 pass.
  • Skipped: check full (integration and browser lanes run in CI).

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.
@coderabbitai

coderabbitai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between fda9c00 and 904b373.

Files selected for processing: 3

  • changelog.d/send-probes-silent-connection.md
  • src/lib/ws.test.ts
  • src/lib/ws.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

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.
@devswha

devswha commented Nov 5, 2026

Copy link
Copy Markdown
Owner Author

Review round (omo: CLEAR at 0941b8b; Codex gpt-6.1-terra xhigh: two MEDIUM, no HIGH).

  • MEDIUM, fixed in 904b373: 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. The timer now leaves
    a hidden page alone (onWake probes it when shown). Test: leaves a silent submit to the page being shown again when it was hidden meanwhile (failed before the fix).
  • MEDIUM, not changed: on a link where nothing at all arrives for 3 s after a send, yet the socket
    is alive, the probe this starts can drop it when its pong takes longer than the probe deadlines
    (3 s, then 3 s). The probe only starts when the socket said nothing since the send (any frame
    counts), so a busy or congested link that keeps delivering frames is never probed; it was not
    seen on the measured relay path (133 ms RTT, ~0.1 MB/s), where frames keep arriving. The existing
    idle probe already has the same exposure; the deadline doubling after a drop still applies.

Validation for 904b373: bun run check fast passed (old-desktop, 96 s); bun test src/lib/ws.test.ts 109 pass.

@devswha

devswha commented Nov 5, 2026

Copy link
Copy Markdown
Owner Author

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.

@devswha
devswha merged commit ba3a851 into main Nov 5, 2026
15 checks passed
@devswha
devswha deleted the fix/send-probes-silent-socket branch November 5, 2026 02:04
devswha added a commit that referenced this pull request Nov 5, 2026
## 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.
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