fix(server): guard #send against closed sockets — no uncaught throw on abrupt sub - #41
Merged
Conversation
…n abrupt sub A client that subscribes then closes before the snapshot finishes streaming (dispose, navigate-away, StrictMode teardown, forced reconnect) drove `#send` to call `ws.send()` on a CLOSING/CLOSED socket, surfacing `Can't call send() after close()` as an uncaught exception. The socket's `webSocketClose` already tears down its subs, so a frame it can no longer read is a benign no-op. Skip sends on a non-OPEN socket and wrap `ws.send` in try/catch for the OPEN→closed race the check can't cover. Outbound-only; no state impact, no behavior change for OPEN sockets. The `Broadcaster` egress path is covered too — it routes through the same `#send` closure. Fixes #40 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Fixes #40
Problem
#send(src/server/mixin.ts) calledws.send(encoded)unconditionally — noreadyStatecheck, no try/catch. When a client sendssuband the socket transitions to CLOSING/CLOSED before the snapshot finishes streaming (normal churn: dispose, navigate-away, StrictMode teardown, forced reconnect drop),ws.send()throwsCan't call WebSocket send() after close(), surfacing as an uncaught exception.Fix
Guard
#send: skip whenreadyState !== WebSocket.OPEN, and wrapws.sendin try/catch to cover the OPEN→closed race the pre-check can't. A post-close send is treated as a benign no-op — the socket'swebSocketClosealready tears down its subs (#dropSocketSubs), so no additional cleanup is needed (stated in a code comment).Broadcasteregress path is covered too — it routes every send through the same#sendclosure (new Broadcaster((ws, frame) => this.#send(ws, frame), …)), the single raw-send chokepoint.Test
New test in
tests/ws-lifecycle.test.tspins the abrupt-close-mid-snapshot shape against a real hibernatable DO socket: open the socket, close the server-side socket, then hand it thesubframe so the terminalsnap-end#sendlands post-close. It assertswebSocketMessageresolves rather than rejecting.Verified to fail on unpatched
main— before the fix it rejected with the exact issue symptom:After the fix: passes.
Validation
npm test— full suite green (265 passed).npm run typecheck— clean.This is a bug fix within existing decisions (outbound-only, ADR-0002/0006 unaffected) — no ADR. CHANGELOG entry added under
[Unreleased] → Fixed.🤖 Generated with Claude Code