Repository navigation
Coordinate web socket connects and closes - #257
Merged
Merged
Conversation
With no connection open, two subscribes started at the same time both
saw no web socket in _ws_connect and each opened one. The second
replaced the first, which was never closed, and the receivers collided
("Concurrent call to receive() is not allowed").
Connecting now holds a lock, so a subscribe started while another is
connecting waits for that connection and uses it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
WebSocketMonitor.close() awaits the socket's close, and aiohttp marks the socket closed partway through it. A subscribe started meanwhile either kept the closing connection and failed, or opened a replacement whose receiver and monitor the finishing close() then cancelled, leaving a subscription that never got a message. new_connection() and close() now hold one lifecycle lock, so a replacement waits until the old connection has finished closing and the close can't touch it. _ws_connect also replaces a connection that's closing (no longer `connected`), not only a missing or closed socket. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
close() waited for the lifecycle lock while the monitor held it for a reconnect, which can take as long as the session's timeout (5 minutes by default), so closing could hang behind an unreachable server. It now cancels the monitor first, as it did before the lock. A new_connection() queued behind another (e.g. a subscribe behind the monitor's reconnect) opened a second connection and replaced the first, leaving it open. It now reuses a connection opened while it waited. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Found while testing #256. Both bugs are races in starting a web socket connection alongside another subscribe or close. Both are on
maintoday.1. Subscribes started together open two connections
When no web socket is open, two subscribes started at the same time (e.g. with
asyncio.gather) both pass_ws_connect's "no web socket yet" check before either connection is up, so each callsnew_connection:_ws, so the first is never closed;RuntimeError: Concurrent call to receive() is not allowed), and a subscribe can end up on a connection that's never acknowledged.Fix:
_ws_connectholds a lock while it creates the monitor and connects. A subscribe started while another is connecting waits for that connection and then uses it.2. A closing web socket cancels its replacement
WebSocketMonitor.close()awaits the socket's close. aiohttp (3.14.5 here; the requirement is>=3.0.0) setsclosedpartway through: after breaking the receiver out ofreceive(), and before waiting up tows_close(10 s) for the close handshake. A subscribe started while the last unsubscribe is closing the connection:closedis set, kept the closing connection, then failed instart_subscription("the web socket connection closed");closedis set, opened a replacement. The finishingclose()then cancelled the replacement's receiver and the monitor, leaving a subscription that reported success but never got a message.Fix:
new_connection()andclose()hold one lifecycle lock. A replacement waits until the old connection has finished closing, so the close can't touch it. With no replacement, the cleanup works as before._ws_connectreplaces any connection that isn'tconnected, which includes one that's closing. Before, it only replaced a missing or closed socket.new_connection()that waited for the lock behind another (e.g. a subscribe behind the monitor's reconnect) reuses the connection that one opened, instead of opening a second and leaving the first open.close()cancels the monitor before taking the lock. Otherwise it would wait out a reconnect the monitor holds the lock for, which can take as long as the session's timeout (aiohttp's default is 5 minutes).Testing
test_simultaneous_subscribes_share_one_connectionstarts two subscribes withasyncio.gather. Both must succeed, on one connection.test_subscribe_while_closing_gets_a_working_connectionholds the close at each point (before and afterclosedis set), starts a subscribe meanwhile, then lets the close finish. The subscribe must succeed on a new connection with a live receiver and monitor.test_queued_new_connection_reuses_the_new_oneandtest_close_stops_a_stuck_reconnectcover the last two fixes.The three race cases fail on
mainand passed 10 runs in a row with the fix. This merges cleanly with #256, and the full suite passes on the two combined.🤖 Generated with Claude Code