mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-30 18:13:25 +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:
@@ -176,7 +176,6 @@ export class GameServer {
|
||||
client.isDisconnected = existing.isDisconnected;
|
||||
client.lastPing = existing.lastPing;
|
||||
|
||||
existing.ws.removeAllListeners();
|
||||
this.activeClients = this.activeClients.filter((c) => c !== existing);
|
||||
}
|
||||
|
||||
@@ -186,7 +185,7 @@ export class GameServer {
|
||||
|
||||
this.allClients.set(client.clientID, client);
|
||||
|
||||
client.ws.removeAllListeners();
|
||||
client.ws.removeAllListeners("message");
|
||||
client.ws.on(
|
||||
"message",
|
||||
gatekeeper.wsHandler(client.ip, async (message: string) => {
|
||||
@@ -205,7 +204,6 @@ export class GameServer {
|
||||
} satisfies ServerErrorMessage),
|
||||
);
|
||||
client.ws.close(1002, "ClientMessageSchema");
|
||||
client.ws.removeAllListeners();
|
||||
return;
|
||||
}
|
||||
const clientMsg = parsed.data;
|
||||
@@ -263,7 +261,6 @@ export class GameServer {
|
||||
});
|
||||
client.ws.on("error", (error: Error) => {
|
||||
if ((error as any).code === "WS_ERR_UNEXPECTED_RSV_1") {
|
||||
client.ws.removeAllListeners();
|
||||
client.ws.close(1002, "WS_ERR_UNEXPECTED_RSV_1");
|
||||
}
|
||||
});
|
||||
@@ -405,7 +402,6 @@ export class GameServer {
|
||||
// Close all WebSocket connections
|
||||
clearInterval(this.endTurnIntervalID);
|
||||
this.websockets.forEach((ws) => {
|
||||
ws.removeAllListeners();
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.close(1000, "game has ended");
|
||||
}
|
||||
@@ -463,7 +459,6 @@ export class GameServer {
|
||||
});
|
||||
if (client.ws.readyState === WebSocket.OPEN) {
|
||||
client.ws.close(1000, "no heartbeats received, closing connection");
|
||||
client.ws.removeAllListeners();
|
||||
}
|
||||
} else {
|
||||
alive.push(client);
|
||||
@@ -558,7 +553,6 @@ export class GameServer {
|
||||
this.activeClients = this.activeClients.filter(
|
||||
(c) => c.clientID !== clientID,
|
||||
);
|
||||
client.ws.removeAllListeners();
|
||||
this.kickedClients.add(clientID);
|
||||
} else {
|
||||
this.log.warn(`cannot kick client, not found in game`, {
|
||||
|
||||
Reference in New Issue
Block a user