Skip to content

Handle per-connection WebSocket errors in the game server - #58

Merged
JacobStephens2 merged 2 commits into
masterfrom
issue-57
Dec 2, 2026
Merged

JacobStephens2 merged 2 commits into
masterfrom
issue-57

Conversation

@JacobStephens2

@JacobStephens2 JacobStephens2 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Summary

A seat socket error now ends that connection and removes its client entry at error time, instead of waiting for the close event - which ws can 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 ws is already closing the socket (1009 over maxPayload, 1007 on invalid UTF-8), the handler still drops the entry immediately but leaves that close to finish, so framing closes keep their codes. dropClient is shared by both listeners and broadcasts presence only on first removal.

Seam under test: attachGameSockets over a real HTTP server with real ws clients in packages/server/tests/action-frame.test.ts - the existing seat-socket integration suite. The new test seats both players, plays one action, emits 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 on the same server.

Evidence

Execution evidence

  • Before: npx vitest run packages/server/tests/action-frame.test.ts with 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, and bash ops/test-server-start.sh all 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.ts plus 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-review skill against refs/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):

  • Duplicated Code in sockets.ts: the delete-and-broadcast cleanup now lives in one dropClient closure called by both the error and close listeners.
  • Duplicated Code in action-frame.test.ts: one shared seatUrl builder and awaitOpen helper 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.

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.
@JacobStephens2
JacobStephens2 merged commit 58c5b35 into master Dec 2, 2026
6 checks passed
@JacobStephens2
JacobStephens2 deleted the issue-57 branch December 2, 2026 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle per-connection WebSocket errors in the game server

1 participant