fix #19: [10] WebSocket connections leak when connection closes abnormally - #41
Merged
Merged
Conversation
…mally Move active_connections cleanup from except WebSocketDisconnect into a finally block so stale connection objects are removed unconditionally on any disconnect type (clean, network failure, client crash, etc.). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #19
Problem
The WebSocket handler in
api/routes.pyonly cleaned upactive_connectionsinsideexcept WebSocketDisconnect:. Any other disconnect cause (network error,RuntimeError,ConnectionResetError, client crash) bypassed the cleanup, leaving staleWebSocketobjects in the dict. Over time this causes unbounded growth ofactive_connectionsand failed send attempts on dead connections.Fix
Moved cleanup to a
finallyblock that always executes regardless of how the receive loop exits. Added defensive guards (simulation_id in active_connections and websocket in active_connections[simulation_id]) to handle edge cases where the connection was never successfully appended. Changedexcept WebSocketDisconnecttoexcept Exception as excwith alogger.debugcall so abnormal disconnects remain traceable.Changes
api/routes.py—websocket_endpoint: finally-block cleanup, debug logging on exceptiontests/unit/test_ws_cleanup_regression.py— 5 offline unit tests covering clean disconnect, RuntimeError, ConnectionResetError, last-connection deletion, and multi-connection isolation.github/workflows/ci.yml— named CI step "Run WebSocket cleanup regression" added tounit-testsjobPre-existing issues noted (not in scope for #19)
active_connections[simulation_id]list atawaitpoints where the finally block can mutate it. Pre-existing; marginally widened by this fix. Suggested fix:list(active_connections.get(simulation_id, []))snapshot before iteration.receive_text()— pre-existing DoS vector.Test results
npm run build: succeeded