mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-05 23:36:26 +00:00
fix ws mem leak (again) (#1591)
## Description: The initial memory leak where websockets had references to GameServers has been fixed. But a new memory leak has been introduced. A memory dump showed that WebSockerServer was hanging onto 10s of thousands of closed websockets. I believe it's because removeAllListeners was removing internal clean up listeners, from the comment: * It is bad practice to remove listeners added elsewhere in the code, * particularly when the EventEmitter instance was created by some other * component or module (e.g. sockets or file streams). This PR ensures that removeAllListeners is only called in the close listener, which is executed after internal cleanup. ## 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 agreement (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:
@@ -316,7 +316,6 @@ export function startWorker() {
|
||||
error: error.toString(),
|
||||
} satisfies ServerErrorMessage),
|
||||
);
|
||||
ws.removeAllListeners();
|
||||
ws.close(1002, "ClientJoinMessageSchema");
|
||||
return;
|
||||
}
|
||||
@@ -334,7 +333,6 @@ export function startWorker() {
|
||||
error,
|
||||
} satisfies ServerErrorMessage),
|
||||
);
|
||||
ws.removeAllListeners();
|
||||
ws.close(1002, "ClientJoinMessageSchema");
|
||||
return;
|
||||
}
|
||||
@@ -352,7 +350,6 @@ 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;
|
||||
}
|
||||
@@ -365,7 +362,6 @@ 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;
|
||||
}
|
||||
@@ -374,7 +370,6 @@ 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;
|
||||
}
|
||||
@@ -389,7 +384,6 @@ export function startWorker() {
|
||||
log.warn(
|
||||
"Forbidden: player without an allowed flare attempted to join game",
|
||||
);
|
||||
ws.removeAllListeners();
|
||||
ws.close(1002, "Forbidden");
|
||||
return;
|
||||
}
|
||||
@@ -406,7 +400,6 @@ export function startWorker() {
|
||||
);
|
||||
if (allowed !== true) {
|
||||
log.warn(`Custom flag ${allowed}: ${clientMsg.flag}`);
|
||||
ws.removeAllListeners();
|
||||
ws.close(1002, `Custom flag ${allowed}`);
|
||||
return;
|
||||
}
|
||||
@@ -422,7 +415,6 @@ export function startWorker() {
|
||||
);
|
||||
if (allowed !== true) {
|
||||
log.warn(`Pattern ${allowed}: ${clientMsg.pattern}`);
|
||||
ws.removeAllListeners();
|
||||
ws.close(1002, `Pattern ${allowed}`);
|
||||
return;
|
||||
}
|
||||
@@ -457,7 +449,6 @@ export function startWorker() {
|
||||
|
||||
// Handle other message types
|
||||
} catch (error) {
|
||||
ws.removeAllListeners();
|
||||
ws.close(1011, "Internal server error");
|
||||
log.warn(
|
||||
`error handling websocket message for ${ipAnonymize(ip)}: ${error}`.substring(
|
||||
@@ -470,7 +461,6 @@ 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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user