Skip to content

Remove a subscription that's interrupted while starting - #256

Merged
natekspencer merged 3 commits into
mainfrom
subscription-cleanup
Nov 16, 2026
Merged

natekspencer merged 3 commits into
mainfrom
subscription-cleanup

Conversation

@natekspencer

@natekspencer natekspencer commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Carries forward the subscription cleanup from #235, by @tn9design (credited as co-author), ported to the current start_subscription.

The bug

WebSocketMonitor.start_subscription adds the subscription to _subscriptions before sending the subscribe request. If that send raises or is cancelled, the caller never gets an unsubscribe handle, but the registration stays:

  • every reconnect resubscribes it (_resubscribe_all), so its callback keeps getting messages;
  • the web socket never closes, because it always has a subscription.

Similarly, a subscribe cancelled in Rivian._start_subscription while connecting or waiting for the ack skipped the "close an unused connection" step.

The fix

  • start_subscription removes 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.
  • The client counts the subscriptions it's still starting. _close_unused_ws closes 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_subscription use it.
  • The cancel cleanup is bounded to 5 seconds and re-raises the cancellation.

Testing

New tests in tests/test_ws_monitor.py run against the real monitor and the fake server:

  • an interrupted send (a send error and a cancellation, each with and without another subscription) removes only its own registration;
  • the client then closes the connection only if nothing else is on it;
  • a subscribe cancelled while waiting for the ack closes the unused connection;
  • with two subscribes waiting for the ack, cancelling one leaves the other to succeed. FakeServer has a new ack_gate to 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

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>
@natekspencer
natekspencer merged commit def23a5 into main Nov 16, 2026
6 checks passed
@natekspencer
natekspencer deleted the subscription-cleanup branch November 16, 2026 03:47
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.

1 participant