Skip to content

fix: WebSocket reconnect loop causing memory leak#4

Open
neavra wants to merge 1 commit intoFrondEnt:mainfrom
neavra:fix/websocket-reconnect-loop
Open

fix: WebSocket reconnect loop causing memory leak#4
neavra wants to merge 1 commit intoFrondEnt:mainfrom
neavra:fix/websocket-reconnect-loop

Conversation

@neavra
Copy link

@neavra neavra commented Feb 17, 2026

When a WebSocket connection drops, both error and close events fire, each calling scheduleReconnect(). Since terminate() also triggers close, you get 2 reconnect timers instead of 1. After repeated disconnects this could compound exponentially (2→4→8→16...), potentially spawning many parallel connections.

I believe this may be causing memory/CPU spikes in long-running processes, though I haven't confirmed with profiling. Either way, this is a good defensive fix to prevent duplicate reconnect timers.

@manja316
Copy link

Good catch on the exponential reconnect. We hit the same pattern running 15-min market bots — after a few hours of intermittent Binance WS drops, the process was holding 12+ dead connections.

Our fix was similar but we also added a reconnect backoff cap:

const maxBackoff = 30000; // 30s ceiling
const backoff = Math.min(1000 * Math.pow(2, retryCount), maxBackoff);

Prevents the timer from growing unbounded even if the upstream is down for extended periods. Might be worth adding here too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants