From 48b957c297a29bb70839b408b2a4a9cfce0e3da0 Mon Sep 17 00:00:00 2001 From: Berk Date: Sat, 16 May 2026 21:17:05 +0300 Subject: [PATCH] fix: guard all ws.send() calls with readyState check to prevent server crashes (#3936) ## Description: Several ws.send() calls in GameServer.ts were missing WebSocket.OPEN readyState guards. This can lead to server crashes if a client disconnects precisely between a check and the send. Added guards to prestart, kickClient, and handleSynchronization. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: barfires Co-authored-by: 22314621 <22314621@student.ciu.edu.tr> --- src/server/GameServer.ts | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/server/GameServer.ts b/src/server/GameServer.ts index 43f26ce38..a34448994 100644 --- a/src/server/GameServer.ts +++ b/src/server/GameServer.ts @@ -672,7 +672,9 @@ export class GameServer { clientID: c.clientID, persistentID: c.persistentID, }); - c.ws.send(msg); + if (c.ws.readyState === WebSocket.OPEN) { + c.ws.send(msg); + } }); } @@ -999,13 +1001,15 @@ export class GameServer { persistentID: client.persistentID, reasonKey, }); - client.ws.send( - JSON.stringify({ - type: "error", - error: reasonKey, - } satisfies ServerErrorMessage), - ); - client.ws.close(1000, reasonKey); + if (client.ws.readyState === WebSocket.OPEN) { + client.ws.send( + JSON.stringify({ + type: "error", + error: reasonKey, + } satisfies ServerErrorMessage), + ); + client.ws.close(1000, reasonKey); + } this.activeClients = this.activeClients.filter( (c) => c.clientID !== clientID, ); @@ -1137,7 +1141,9 @@ export class GameServer { clientID: c.clientID, persistentID: c.persistentID, }); - c.ws.send(desyncMsg); + if (c.ws.readyState === WebSocket.OPEN) { + c.ws.send(desyncMsg); + } } }