From 4dda53b564a4ccc11e818a5346094601487e8b45 Mon Sep 17 00:00:00 2001 From: evanpelle Date: Sat, 19 Jul 2025 09:35:13 -0700 Subject: [PATCH] remove all ws listeners (#1498) ## Description: v24 introduced a memory leak in the worker. It may be caused by improperly cleaning up websockets. ## 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/Worker.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/server/Worker.ts b/src/server/Worker.ts index b15e5262b..c916530b2 100644 --- a/src/server/Worker.ts +++ b/src/server/Worker.ts @@ -316,6 +316,7 @@ export function startWorker() { error: error.toString(), } satisfies ServerErrorMessage), ); + ws.removeAllListeners(); ws.close(1002, "ClientJoinMessageSchema"); return; } @@ -333,6 +334,7 @@ export function startWorker() { error, } satisfies ServerErrorMessage), ); + ws.removeAllListeners(); ws.close(1002, "ClientJoinMessageSchema"); return; } @@ -350,6 +352,7 @@ export function startWorker() { const result = await verifyClientToken(clientMsg.token, config); if (result === false) { log.warn("Unauthorized: Invalid token"); + ws.removeAllListeners(); ws.close(1002, "Unauthorized"); return; } @@ -362,6 +365,7 @@ export function startWorker() { if (claims === null) { if (allowedFlares !== undefined) { log.warn("Unauthorized: Anonymous user attempted to join game"); + ws.removeAllListeners(); ws.close(1002, "Unauthorized"); return; } @@ -370,6 +374,7 @@ export function startWorker() { const result = await getUserMe(clientMsg.token, config); if (result === false) { log.warn("Unauthorized: Invalid session"); + ws.removeAllListeners(); ws.close(1002, "Unauthorized"); return; } @@ -384,6 +389,7 @@ export function startWorker() { log.warn( "Forbidden: player without an allowed flare attempted to join game", ); + ws.removeAllListeners(); ws.close(1002, "Forbidden"); return; } @@ -400,6 +406,7 @@ export function startWorker() { ); if (allowed !== true) { log.warn(`Custom flag ${allowed}: ${clientMsg.flag}`); + ws.removeAllListeners(); ws.close(1002, `Custom flag ${allowed}`); return; } @@ -415,6 +422,7 @@ export function startWorker() { ); if (allowed !== true) { log.warn(`Pattern ${allowed}: ${clientMsg.pattern}`); + ws.removeAllListeners(); ws.close(1002, `Pattern ${allowed}`); return; } @@ -460,6 +468,7 @@ export function startWorker() { ); ws.on("error", (error: Error) => { + ws.removeAllListeners(); if ((error as any).code === "WS_ERR_UNEXPECTED_RSV_1") { ws.close(1002, "WS_ERR_UNEXPECTED_RSV_1"); }