Repository navigation
Remove a subscription that's interrupted while starting - #256
Merged
Merged
Conversation
start_subscription registers a subscription before sending the subscribe request. If the send fails or is cancelled, the caller never gets an unsubscribe handle, but the registration stays: every reconnect resubscribes it, its callback keeps getting messages, and the web socket never closes because it always has a subscription. Remove the registration when the send is interrupted, and close the connection if nothing else is on it. A subscribe cancelled while connecting or waiting for the ack also closes an otherwise unused connection. Cleanup is bounded to 5 seconds and keeps the original error. From #235. Co-authored-by: tn9design <52539250+tn9design@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The cancel cleanup closed the web socket whenever it had no subscriptions, which includes while another subscribe is still waiting for the ack on the same connection, so that one failed too. The client now counts the subscriptions it's still starting and closes the connection only when nothing is subscribed or starting on it. The monitor no longer closes it when a subscribe send is interrupted: it removes its own registration and leaves the closing to the client. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Check the web socket before the guarded send, so an assertion can't be logged as a failed send, and shorten _close_unused_ws's docstring. The two-waiter test now waits for both subscribes to be starting rather than sleeping, and the interrupted-send test checks the server is told the subscription is complete. 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.
Carries forward the subscription cleanup from #235, by @tn9design (credited as co-author), ported to the current
start_subscription.The bug
WebSocketMonitor.start_subscriptionadds the subscription to_subscriptionsbefore sending the subscribe request. If that send raises or is cancelled, the caller never gets an unsubscribe handle, but the registration stays:_resubscribe_all), so its callback keeps getting messages;Similarly, a subscribe cancelled in
Rivian._start_subscriptionwhile connecting or waiting for the ack skipped the "close an unused connection" step.The fix
start_subscriptionremoves its own registration when the send is interrupted, and tells the server the subscription is complete if the connection is open. It doesn't close the connection itself._close_unused_wscloses the connection only when nothing is subscribed or still starting on it, so cancelling one subscribe doesn't fail another that's waiting for the same ack. Both the failure and the cancel paths of_start_subscriptionuse it.Testing
New tests in
tests/test_ws_monitor.pyrun against the real monitor and the fake server:FakeServerhas a newack_gateto hold the ack.Against
main, the nine leak tests fail, and the two-waiter test passes. It guards against the cleanup closing a connection still in use.🤖 Generated with Claude Code