Skip to content

Commit c4497e9

Browse files
authored
fix(ui-web): refuse a held rpc call whose socket already closed (#1348)
## Summary `whenOpen` is where a call waits while the socket is still connecting: it attaches an `open` and a `close` listener and settles on whichever fires. A socket that is already closing or closed when those listeners go on never fires either event for that call, so the call stays pending for the life of the page. - Read the socket's state after both listeners are registered and reject with the same `not connected` an absent socket answers. A socket still connecting, or already open, stays with the listeners. This check cannot fire on the browser path today: `send` only enters `whenOpen` while `readyState` is `CONNECTING`, the registrations and the state read run without an intervening task, and a native socket changes `readyState` only between tasks. It is kept as a guard for the `WebSocketLike` contract: an implementation that settles synchronously, or a future `await` between the check and the registrations, can hand over a closed socket whose close event has already gone out. The comments say that rather than claiming a browser race. Found while investigating #1347. The desk Tasks tab staying blank after a reconnect is #1347's subject; this change hardens the held-call path and does not claim to fix that symptom. ## Verification - `node node_modules/vitest/vitest.mjs run src/rpc/wsTransport.test.ts`: 28 tests passed, 0 failed. - The new case fails without the fix: the same call stays `pending` after its socket closes as the listeners attach. - `npm run type-check`: passed. - `npm run lint`: 0 errors, 4 warnings, the same pre-existing ones in `SubagentsPage`, `ConnectionsPage` and `CronPage`. - The comment revision is comment-only (plus the case title): 28 tests still pass, type-check and lint unchanged. - `scripts/check_commit_messages.py origin/main..HEAD`: exit 0. - `scripts/check_source_language.py origin/main..HEAD`: exit 0. - `git diff --check origin/main...HEAD`: exit 0. - The run read the tree under test: `tsc --listFiles` resolves that tree's own `src`.
1 parent c249c03 commit c4497e9

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎ui-web/src/rpc/wsTransport.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,34 @@ describe('a call made before the socket is open', () => {
250250
expect((await failure(answer)).message).toBe('not connected')
251251
})
252252

253+
/* What this pins is the `WebSocketLike` shape, not a race a browser runs: an
254+
implementation may settle the socket while its listeners are being
255+
registered, so the close event goes out to an empty list and waiting for
256+
the event alone would leave the call pending. The state read after the
257+
registrations rejects it. The fake flips `readyState` inside
258+
`addEventListener` to stand in for such an implementation; a native socket
259+
changes state only between tasks and cannot reach this window. */
260+
it('fails a held call whose socket is already closed when the state is read', async () => {
261+
const { h, ws } = connecting()
262+
const register = ws.addEventListener.bind(ws)
263+
ws.addEventListener = (type, fn, options) => {
264+
if (type === 'open') ws.readyState = FakeSocket.CLOSED
265+
register(type, fn, options)
266+
}
267+
268+
let outcome = 'pending'
269+
void h.transport.call('session.list', {}).then(
270+
() => { outcome = 'resolved' },
271+
(e: unknown) => { outcome = (e as Error).message }
272+
)
273+
/* `call` rejects a turn after `send` does, so the hand-off needs two
274+
turns of the queue before the outcome is visible here. */
275+
await Promise.resolve()
276+
await Promise.resolve()
277+
278+
expect(outcome).toBe('not connected')
279+
})
280+
253281
it('still fails fast when there is no socket at all', async () => {
254282
const h = harness()
255283

‎ui-web/src/rpc/wsTransport.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ const REJOIN_MAX_WAIT_MS = 8_000
3131
const REJOIN_FIRST_WAIT_MS = 1_500
3232
const REJOIN_GROWTH = 1.6
3333

34-
/* Not `WebSocket.CONNECTING`: the transport must be constructible where no
35-
global WebSocket exists, which is how its own tests drive it. */
34+
/* Not `WebSocket.CONNECTING` / `WebSocket.OPEN`: the transport must be
35+
constructible where no global WebSocket exists, which is how its own tests
36+
drive it. */
3637
const CONNECTING = 0
38+
const OPEN = 1
3739

3840
/** The slice of a WebSocket this transport uses, so a test can supply one. */
3941
export interface WebSocketLike {
@@ -293,6 +295,19 @@ export class WsTransport implements RpcTransport {
293295
{ once: true }
294296
)
295297
ws.addEventListener('close', () => reject(new RpcError(-1, 'not connected')), { once: true })
298+
/* Both listeners are on before the state is read. This read guards the
299+
`WebSocketLike` contract, not a race the browser can run: a native
300+
socket moves `readyState` only between tasks, so it cannot change
301+
between `send`'s CONNECTING check and this line. An implementation
302+
that settles synchronously, or a future `await` before these
303+
registrations, can hand over a socket already closing or closed whose
304+
close event has gone out, and waiting for the event alone would leave
305+
that call pending for the life of the page. Only a connecting socket
306+
is left to the listeners; an `OPEN` read is trusted because the
307+
browser path cannot deliver the open event before `connect()` marks
308+
the socket opened. */
309+
const state = ws.readyState
310+
if (state !== CONNECTING && state !== OPEN) reject(new RpcError(-1, 'not connected'))
296311
})
297312
}
298313

0 commit comments

Comments
 (0)