Thanks for the detailed report! I can reproduce this. Looking at the heap snapshot, it seems we're not removing event listeners on the old WebSocket instance before creating a new one.
The issue is in src/websocket/client.ts:45-58. We need to call ws.removeAllListeners() before reassigning.
Good catch! I took a quick look and here's the problematic code:
private reconnect() {
console.log('Reconnecting WebSocket...');
this.ws = new WebSocket(this.url);
this.setupListeners(); // <-- Adds new listeners without removing old ones
}
private setupListeners() {
this.ws.addEventListener('open', this.onOpen);
this.ws.addEventListener('message', this.onMessage);
this.ws.addEventListener('error', this.onError);
this.ws.addEventListener('close', this.onClose);
}
Should I open a PR with the fix? I'm thinking:
private reconnect() {
if (this.ws) {
this.ws.removeEventListener('open', this.onOpen);
this.ws.removeEventListener('message', this.onMessage);
this.ws.removeEventListener('error', this.onError);
this.ws.removeEventListener('close', this.onClose);
this.ws.close();
}
this.ws = new WebSocket(this.url);
this.setupListeners();
}
Yes, that would fix it! Though we should also clear the reconnection timeout to avoid multiple concurrent reconnect attempts. Can you add:
if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = null;
}
Also, please add a test case that simulates multiple reconnections and asserts that event listeners don't accumulate.
Will do! Opening PR shortly. Should I target main or develop?
Target develop — we'll cherry-pick to main for the patch release. Thanks for jumping on this!
Describe the bug
The real-time dashboard WebSocket client has a memory leak when the connection is interrupted and reconnects multiple times. Memory usage increases by ~20MB per reconnection and is never garbage collected.
To Reproduce
Expected behavior
Memory should stabilize after reconnection. Old WebSocket instances and event listeners should be properly cleaned up.
Screenshots
(Chrome DevTools memory profile showing heap growth attached)
Environment: