fix(client): reject WebSocket link calls after the socket closes - #2081
Merged
Merged
Conversation
With reconnect disabled (the default), the WebSocket link kept handing out the cached peer after its socket closed. `WebSocket.send` on a CLOSING/CLOSED socket silently discards data instead of throwing, so every later call stayed pending forever. The same happened when the socket closed before opening (server unreachable). The peer's send callback now throws the stored `WebSocket closed (code X: reason)` AbortError once the socket has closed, so those calls reject with the original close reason.
More templates
@orpc/ai-sdk
@orpc/arktype
@orpc/bun
@orpc/client
@orpc/cloudflare
@orpc/contract
@orpc/experimental-effect
@orpc/evlog
@orpc/hibernation
@orpc/json-schema
@orpc/experimental-lock
@orpc/experimental-msw
@orpc/nest
@orpc/next
@orpc/node
@orpc/openapi
@orpc/opentelemetry
@orpc/pinia-colada
@orpc/pino
@orpc/publisher
@orpc/ratelimit
@orpc/server
@orpc/shared
@orpc/swr
@orpc/tanstack-query
@orpc/trpc
@orpc/valibot
@orpc/zod
commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
Contributor
There was a problem hiding this comment.
ℹ️ Minor suggestion inline. The fix is correct and the new tests genuinely fail without it.
Reviewed changes
- Reject calls on a closed socket —
WebSocketLinkTransportnow captures a per-connectioncloseReasonset by thecloselistener, and theClientPeersend callback throws it so requests made after close reject instead of hanging (transport.ts:185-194,:220-230). - Corrected stale assumption — dropped the comment claiming
WebSocket.sendthrows on non-open state; per the WHATWG spec it silently discards on CLOSING/CLOSED. - Regression tests — two new cases in
rpc-link.test.tsfor close-after-open and close-before-open. I reverted the source fix locally and confirmed both time out (hang), so they actually pin the bug.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
dinwwwh
added a commit
that referenced
this pull request
Sep 25, 2026
) Calls made through the message port `RPCLink` after the port closes now reject with `AbortError('MessagePort closed')` instead of hanging forever. In-flight calls cut off by the close now reject with the same reason instead of the generic "Peer was closed". This is the message port counterpart of #2081. ## Fixes - Web `MessagePort`, Node `worker_threads` ports, and Electron `MessagePortMain` silently drop `postMessage` once closed, so any later call stayed pending unless the caller passed an abort signal. Those calls now reject right away and nothing is posted to the dead port. - Browser extension ports already rejected, because `postMessage` throws on a disconnected port in both Chrome and Firefox. A remote disconnect now surfaces the same `AbortError` as the other port kinds. ## Testing - New test: a call made after close rejects with `AbortError('MessagePort closed')` and never posts a message. It timed out before the fix. - The existing close test now asserts the exact abort reason. - Checked Node 24 directly: after close, `postMessage` drops messages without throwing and `close` fires on both ends. Electron, Chrome, and Firefox behavior was checked against their source. A client that calls `close()` on its own browser `MessagePort` can still hang, because the HTML spec fires `close` only at the other end of the channel. Node and Electron fire it on both ends.
dinwwwh
added a commit
that referenced
this pull request
Sep 25, 2026
…d socket (#2083) With reconnect disabled (the default), calls through the WebSocket link hung forever when `connect` returned a socket that was already closing or closed. They now reject immediately with `AbortError('WebSocket is already closing or closed')`. This happens when the socket is created ahead of time, as in the hibernation docs (`connect: () => websocket`): if the server is down and the socket closes before the first call, that call never settled. A `close` listener added after the socket closed never fires (confirmed on Node 24 for both the global `WebSocket` and `ws`), and `send` silently discards data on a closed socket, so the close handling from #2081 never ran. ## Fixes - Calls on a socket that was already closing or closed reject right away instead of hanging. - With reconnect enabled, the link still retries as before, but no longer attaches listeners to the dead socket. ## Notes - A custom `WebSocketLike` must report `readyState` as `CONNECTING` (0) or `OPEN` (1) to be used; any other value is treated as closed. ## Testing - New tests in `rpc-link.test.ts` cover an already closing and an already closed socket with reconnect disabled (both hang on `main`) and an already closed socket with reconnect enabled. - Checked with a real Node `WebSocket` closed by a refused connection: the call stays pending on `main` and rejects immediately with this change. - `pnpm vitest run packages/client/src/adapters/websocket tests/rpc`, eslint, and `@orpc/client` type-check pass.
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.

With reconnect disabled (the default), calls made through the WebSocket link after its socket closed never settled. They now reject with the original
WebSocket closed (code X: reason)AbortError.The link kept reusing the closed connection, and
WebSocket.sendon a CLOSING/CLOSED socket silently discards data instead of throwing (WHATWG spec; confirmed on Node 24 for both the globalWebSocketandws), so the request waited forever unless the caller passed an abort signal.Fixes
Performance
Testing
rpc-link.test.tscover close after open and close before open; both hang onmainand pass with this change.pnpm vitest run packages/client/src/adapters/websocket tests/rpcand eslint pass.