Repository navigation
fix(server): ignore websocket messages received after close - #2373
Conversation
With binary Blob frames, `upgrade()` awaits `loadBytes` before calling `.message()`. If the socket closed meanwhile, `.message()` created a new `ServerPeer` and ran the procedure: no close event would ever abort it, and the peer entry leaked. Track closed sockets in a WeakSet so `.message()` ignores messages for a socket already passed to `.close()`. Apply the same fix to the crossws handler and delete its peer before awaiting `peer.close()`, matching the websocket and message-port adapters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VVaw7RKXMmm8LBoZPKXUZ5
Replace the separate `closed` WeakSet with a `null` entry in the existing `peers` WeakMap, so each handler keeps one structure per socket and `close()` no longer needs a delete-before-close ordering. Tests: wait on the actual `.message()` call instead of a fixed 50ms sleep, and let `createWs` capture listeners. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VVaw7RKXMmm8LBoZPKXUZ5
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
orpc | 7c231da | Commit Preview URL Branch Preview URL |
Nov 02 2026, 12:59 AM |
More templates
@orpc/ai-sdk
@orpc/arktype
@orpc/bun
@orpc/client
@orpc/cloudflare
@orpc/contract
@orpc/effect
@orpc/evlog
@orpc/hibernation
@orpc/json-schema
@orpc/experimental-lock
@orpc/mcp
@orpc/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! |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes — the full PR diff (both commits), tracing the message/close race across upgrade(), sequential, ServerPeer, and the two adapters.
- WebSocket adapter —
peersis nowWeakMap<WebSocketLike, ServerPeer | null>;close()writesnull, andmessage()returns the decoded{ matched }result without creating a peer or running a procedure when it seesnull. Decode was hoisted above the peer lookup so the closed branch can return it. - crossws adapter — same null-sentinel pattern;
close()now marks the peer closed beforeawait peer.close(), matching the websocket/message-port ordering and closing the half-closed-peer window. - Tests — message-after-close coverage for both adapters, plus a Blob-frame-after-close test through
upgrade().
The core race is closed correctly: the synchronous window from this.peers.get(ws) through await peer.message(...) is intact, so a close can no longer interleave once a live peer has been read, and a Blob that resolves after close hits the null guard. pnpm vitest run on the two adapter test files passes (19 tests).
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
Resolve conflicts with #2373: the websocket and crossws handlers keep the shared `PeerRegistry`, and their tests come from main, which covers the same after-close cases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VVaw7Qv8vxXaxrrzEYcVnb
`MessagePortHandler.close()` deleted the port from `peers`, so a later `.message()` created a new `ServerPeer` that was never closed or aborted. Mirror the WebSocket/crossws fix (#2373): store `null` on close before awaiting `peer.close()`, and ignore messages for a closed port. Also correct the PBKDF2 iteration comment in `helpers/encryption.ts`: OWASP recommends 600,000 for PBKDF2-HMAC-SHA256, not 60,000. The count is unchanged because raising it would break existing ciphertexts without a versioned format. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VVaw7TAdd8oB3QkggFBkM6
## Problem and fix ### 1. MessagePort message after close (`adapters/message-port/handler.ts`) `close()` removed the port from `peers`. A later `.message(port, ...)` then created a new `ServerPeer` that was never closed or aborted. The fix is the same one #2373 made for the WebSocket and crossws handlers: - `close()` now stores `null` for the port before awaiting `peer.close()`. - When `.message()` sees `null`, it returns `{ matched: true }`. It doesn't create a peer or run the procedure. `peers` is a `WeakMap`, so the `null` entry is freed when the port is garbage-collected. - Side effect: `.message()` now decodes the message before it looks up or creates the peer, so invalid messages no longer create a peer. ### 2. Wrong security comment (`helpers/encryption.ts`) The comment called 60,000 PBKDF2 iterations the "OWASP recommended minimum". OWASP's Password Storage Cheat Sheet recommends **600,000** for PBKDF2-HMAC-SHA256. This PR only corrects the comment and **does not change the iteration count**. The encrypted output stores the salt and IV but not the iteration count, so raising it would make every existing ciphertext undecryptable. Raising it safely needs a versioned format that records the parameters. That would be a separate change. ## Tests - message-port: `.message()` called after `.close()`, or while `.close()` is still pending, does not run the procedure or post anything. Both tests fail without the fix. `pnpm vitest run packages/server/src/plugins packages/server/src/helpers packages/server/src/adapters/message-port` (366 tests), `pnpm type:check` and `pnpm lint` all pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01VVaw7TAdd8oB3QkggFBkM6 Co-authored-by: Claude <noreply@anthropic.com>

Problem
With binary
Blobframes,WebSocketHandler.upgrade()awaitsloadBytes(event.data)before calling.message(). If the client sends a request and closes at once, thecloselistener runs.close(ws)during that await. When the Blob finishes loading,.message()finds no peer, creates and stores a newServerPeer, and runs the procedure.No
closeevent will ever come for that peer, so:AbortControllernever fires, and the procedure'ssignalstays un-aborted;sendon the closed socket;The same thing happens when
.message()is called by hand after.close().experimental_CrosswsHandler.close()also awaitedpeer.close()before removing the peer. While that await was pending, a message could be handed to a peer that was partway through closing.Fix
WebSocketHandlerandexperimental_CrosswsHandlernow storenullin the existingpeersWeakMap onclose(). When.message()seesnull, it returns the decoded{ matched }result without creating a peer or running a procedure. The map is weak, so thenullentry goes away when the socket is garbage-collected.close()now marks the peer closed before awaitingpeer.close(), the same order the websocket and message-port adapters already used.Tests
upgrade()that finishes loading afterclosedoes not run the procedure or send anything..message()after.close()does not run the procedure or send anything.All three tests fail without the fix.
pnpm vitest run packages/server/src/adapters,pnpm type:checkandpnpm lintpass.Not included
MessagePortHandleruses the same lazy-peer pattern but has no async gap before.message(), so this PR leaves it unchanged. A shared helper for all three adapters would be a good follow-up.Generated by Claude Code