From d010fdbda009e16cb84264bb79ac0c8fc71d65bd Mon Sep 17 00:00:00 2001 From: evanpelle Date: Thu, 24 Jul 2025 16:32:34 -0700 Subject: [PATCH] memory leak fix attempt: store websockets and remove listeners when game ends (#1564) ## Description: This is a temporary fix to remove websocket memory leaks. ## 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 - [x] I have read and accepted the CLA aggreement (only required once). ## Please put your Discord username so you can be contacted if a bug or regression is found: evan --- src/server/GameServer.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/server/GameServer.ts b/src/server/GameServer.ts index 16d894ba0..73d44195c 100644 --- a/src/server/GameServer.ts +++ b/src/server/GameServer.ts @@ -61,6 +61,8 @@ export class GameServer { private kickedClients: Set = new Set(); private outOfSyncClients: Set = new Set(); + private websockets: Set = new Set(); + constructor( public readonly id: string, readonly log_: Logger, @@ -107,6 +109,7 @@ export class GameServer { } public addClient(client: Client, lastTurn: number) { + this.websockets.add(client.ws); if (this.kickedClients.has(client.clientID)) { this.log.warn(`cannot add client, already kicked`, { clientID: client.clientID, @@ -401,10 +404,10 @@ export class GameServer { async end() { // Close all WebSocket connections clearInterval(this.endTurnIntervalID); - this.allClients.forEach((client) => { - client.ws.removeAllListeners(); - if (client.ws.readyState === WebSocket.OPEN) { - client.ws.close(1000, "game has ended"); + this.websockets.forEach((ws) => { + ws.removeAllListeners(); + if (ws.readyState === WebSocket.OPEN) { + ws.close(1000, "game has ended"); } }); if (!this._hasPrestarted && !this._hasStarted) {