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
This commit is contained in:
evanpelle
2025-07-24 16:32:34 -07:00
committed by GitHub
parent 419aef7751
commit d010fdbda0
+7 -4
View File
@@ -61,6 +61,8 @@ export class GameServer {
private kickedClients: Set<ClientID> = new Set(); private kickedClients: Set<ClientID> = new Set();
private outOfSyncClients: Set<ClientID> = new Set(); private outOfSyncClients: Set<ClientID> = new Set();
private websockets: Set<WebSocket> = new Set();
constructor( constructor(
public readonly id: string, public readonly id: string,
readonly log_: Logger, readonly log_: Logger,
@@ -107,6 +109,7 @@ export class GameServer {
} }
public addClient(client: Client, lastTurn: number) { public addClient(client: Client, lastTurn: number) {
this.websockets.add(client.ws);
if (this.kickedClients.has(client.clientID)) { if (this.kickedClients.has(client.clientID)) {
this.log.warn(`cannot add client, already kicked`, { this.log.warn(`cannot add client, already kicked`, {
clientID: client.clientID, clientID: client.clientID,
@@ -401,10 +404,10 @@ export class GameServer {
async end() { async end() {
// Close all WebSocket connections // Close all WebSocket connections
clearInterval(this.endTurnIntervalID); clearInterval(this.endTurnIntervalID);
this.allClients.forEach((client) => { this.websockets.forEach((ws) => {
client.ws.removeAllListeners(); ws.removeAllListeners();
if (client.ws.readyState === WebSocket.OPEN) { if (ws.readyState === WebSocket.OPEN) {
client.ws.close(1000, "game has ended"); ws.close(1000, "game has ended");
} }
}); });
if (!this._hasPrestarted && !this._hasStarted) { if (!this._hasPrestarted && !this._hasStarted) {