Repository navigation
Handle per-connection WebSocket errors in the game server - #58
Merged
Merged
Conversation
The per-connection error listener logged a socket error and left cleanup to the close event. ws starts closing before it emits error for bad frames, but that close can wait on the peer (up to the 30s close timer), so a dead seat kept its clients entry - and kept reading as connected to its partner - long after its error. An error ws had not already started closing left the connection open outright, and a connection rejected for missing seat credentials never got an error listener at all, so its error would have thrown unhandled. The listener now runs for every connection before the seat check, drops the client entry and broadcasts presence itself, and terminates the socket when ws has not already started closing it. The close listener keeps the same cleanup, guarded so presence broadcasts only once. Framing closes keep their 1007/1009 codes: when ws is already closing, the handler leaves that close to finish. Tests: a server integration regression test seats both players over real sockets, emits an error on the lead's server-side socket, and checks that only that connection ends (1006), the partner is told the seat is away, and the partner can still play. It timed out before the fix because the errored connection never ended.
Share one dropClient cleanup between the error and close listeners so the broadcast-once invariant lives in one place, and give the frame tests one seat-URL builder and one open-wait helper instead of three copies.
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.
Summary
A seat socket error now ends that connection and removes its client entry at error time, instead of waiting for the
closeevent - whichwscan hold open for up to its 30s close timer when a peer never answers the close frame. Every connection gets the listener, including one rejected for missing seat credentials, whose error previously had no listener at all.sockets.on("connection", (socket) => { const client = clients.get(socket); - if (!client) return socket.close(4401, "Seat credentials missing"); + function dropClient(): void { + if (client && clients.delete(socket)) broadcastPresence(client.code); + } socket.on("error", (error) => { - // log only; cleanup waited for `close` + // log (length closes stay quiet), then: + dropClient(); + if (readyState is OPEN or CONNECTING) socket.terminate(); }); + if (!client) return socket.close(4401, "Seat credentials missing"); // ... - socket.on("close", () => { clients.delete(socket); broadcastPresence(...); }); + socket.on("close", dropClient); });When
wsis already closing the socket (1009 overmaxPayload, 1007 on invalid UTF-8), the handler still drops the entry immediately but leaves that close to finish, so framing closes keep their codes.dropClientis shared by both listeners and broadcasts presence only on first removal.Seam under test:
attachGameSocketsover a real HTTP server with realwsclients inpackages/server/tests/action-frame.test.ts- the existing seat-socket integration suite. The new test seats both players, plays one action, emitserroron the lead's server-side socket, and checks that only that connection ends (1006), the partner is told the seat is away, and the partner can still play on the same server.Evidence
Execution evidence
npx vitest run packages/server/tests/action-frame.test.tswith the new test against the old handler - 1 failed, 7 passed. The new test (ends only the errored connection and removes its client entry) timed out at 5000ms because the errored connection never ended.After: the same command - 8 passed, including the unchanged 1007/1009 framing-close tests.
Full validation on this branch:
npm test(405 passed),npm run typecheck,npm run lint,npm run format:check,npm run build, andbash ops/test-server-start.shall pass.Visual evidence
None - this changes server socket handling only; no rendered surface is touched.
Merge Danger
Door: two-way
The change is two small commits in
packages/server/src/sockets.tsplus its test file; reverting restores the previous log-and-wait-for-close behaviour.Blast Radius: seat sockets
Errored connections are now dropped and terminated promptly instead of lingering until
close. Partner presence updates at error time. Framing closes (1007/1009) and seated play are unchanged, as the existing tests show.Review
Reviewed with the
thirdshift-code-reviewskill againstrefs/remotes/origin/master. Standards raised 2 actionable findings, Spec raised 0. Both reviewers read both changed files; no changed file was left unread and no generated output was excluded.Addressed Standards findings (commit
5a10881):sockets.ts: the delete-and-broadcast cleanup now lives in onedropClientclosure called by both theerrorandcloselisteners.action-frame.test.ts: one sharedseatUrlbuilder andawaitOpenhelper replace the repeated copies.Unaddressed findings
None. Every Standards and Spec finding raised was addressed above; there are no skipped findings.
Security
None. Guidance-mode security review of this diff (including supporting socket and room code) found no unaddressed introduced findings.
Closes #57
Built with muse Muse Code 1.4.25 (1.4.25-R9361.1) · muse-wonder-full-1.0 · max
Security review outcome
No unaddressed introduced findings.