From 78c5121aabbbe5093fc5e306f6f80185dbcd4b94 Mon Sep 17 00:00:00 2001 From: Nathan Spencer Date: Mon, 16 Nov 2026 02:35:21 +0000 Subject: [PATCH 1/3] Remove a subscription that's interrupted while starting 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 --- src/rivian/rivian.py | 12 +++++++++ src/rivian/ws_monitor.py | 17 ++++++++++++- tests/test_ws_monitor.py | 53 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/rivian/rivian.py b/src/rivian/rivian.py index 1560f92..563d734 100644 --- a/src/rivian/rivian.py +++ b/src/rivian/rivian.py @@ -1285,6 +1285,18 @@ async def _start_subscription( reason = f"the web socket connection closed ({close_message})" else: reason = "the web socket connection closed" + except asyncio.CancelledError: + # Cancelled while connecting or waiting for the ack, before there's an + # unsubscribe handle: don't leave a connection open with nothing on it + if self._ws_monitor and not self._ws_monitor.has_subscriptions: + try: + async with asyncio.timeout(5): + await self._ws_monitor.close() + except (Exception, asyncio.CancelledError): # noqa: BLE001 - keep the cancellation + _LOGGER.warning( + "Couldn't close the web socket after a cancelled subscribe" + ) + raise except Exception as ex: # pylint: disable=broad-except # noqa: BLE001 reason = ex # Expected while backing off, so less severe than other failures diff --git a/src/rivian/ws_monitor.py b/src/rivian/ws_monitor.py index dd2578d..f50f684 100644 --- a/src/rivian/ws_monitor.py +++ b/src/rivian/ws_monitor.py @@ -247,7 +247,22 @@ async def start_subscription( return None _id = str(uuid4()) self._subscriptions[_id] = (callback, payload, on_error) - await self._subscribe(_id, payload) + try: + await self._subscribe(_id, payload) + except (Exception, asyncio.CancelledError): + # Registered before the send, and the caller has no unsubscribe + # handle yet: remove it so a reconnect doesn't resubscribe it + self._subscriptions.pop(_id, None) + try: + async with asyncio.timeout(5): + if not self._subscriptions: + await self.close() + elif self.connected: + assert self._ws + await self._ws.send_json({"id": _id, "type": "complete"}) + except (Exception, asyncio.CancelledError): # noqa: BLE001 - keep the original error + _LOGGER.warning("Couldn't clean up an interrupted subscription") + raise async def unsubscribe() -> None: """Unsubscribe.""" diff --git a/tests/test_ws_monitor.py b/tests/test_ws_monitor.py index 3ea5bbe..7b3b433 100644 --- a/tests/test_ws_monitor.py +++ b/tests/test_ws_monitor.py @@ -637,3 +637,56 @@ def failing(_: dict[str, Any]) -> None: assert "Error in a subscription callback" in caplog.text assert monitor.connected assert server.connections == 1 + + +@pytest.mark.parametrize("keep_existing", [False, True]) +@pytest.mark.parametrize("failure", ["send_error", "cancel"]) +async def test_interrupted_subscribe_is_removed( + server: FakeServer, monitor: WebSocketMonitor, failure: str, keep_existing: bool +) -> None: + """A subscribe that fails or is cancelled while sending isn't resubscribed later. + + Only its own registration goes: the connection closes if nothing else is on it. + """ + await monitor.new_connection(start_monitor=True) + await wait_for(lambda: monitor.connection_ack.is_set()) + if keep_existing: + add_subscription(monitor) + existing = dict(monitor._subscriptions) + sending = asyncio.Event() + + async def subscribe(*_: Any) -> None: + sending.set() + if failure == "send_error": + raise ConnectionResetError("closing") + await asyncio.Event().wait() + + with patch.object(monitor, "_subscribe", side_effect=subscribe): + task = asyncio.create_task(monitor.start_subscription({}, lambda _: None)) + await sending.wait() + if failure == "cancel": + task.cancel() + with pytest.raises( + ConnectionResetError if failure == "send_error" else asyncio.CancelledError + ): + await task + + assert monitor._subscriptions == existing + assert monitor.connected is keep_existing + + +async def test_cancelled_subscribe_before_ack_closes( + server: FakeServer, monitor: WebSocketMonitor, rivian: Rivian +) -> None: + """A subscribe cancelled while waiting for the ack leaves nothing reconnecting.""" + server.silent_connections = 1 + task = asyncio.create_task( + rivian.subscribe_for_vehicle_updates("id", lambda _: None) + ) + await wait_for(lambda: server.connections == 1) + task.cancel() + with pytest.raises(asyncio.CancelledError): + await task + + assert not monitor.connected + assert monitor.monitor and monitor.monitor.done() From 0f51a72123563861381a3de56848f470a7e3da48 Mon Sep 17 00:00:00 2001 From: Nathan Spencer Date: Mon, 16 Nov 2026 02:48:49 +0000 Subject: [PATCH 2/3] Keep the connection for subscribes still starting 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 --- src/rivian/rivian.py | 70 ++++++++++++++++++++++++---------------- src/rivian/ws_monitor.py | 16 ++++----- tests/test_ws_monitor.py | 66 ++++++++++++++++++++++++++++++++++++- 3 files changed, 115 insertions(+), 37 deletions(-) diff --git a/src/rivian/rivian.py b/src/rivian/rivian.py index 563d734..0eb2ea2 100644 --- a/src/rivian/rivian.py +++ b/src/rivian/rivian.py @@ -159,6 +159,8 @@ def __init__( self._ws_monitor: WebSocketMonitor | None = None self._subscriptions: dict[str, str] = {} + # Subscriptions still being started, which need the connection kept open + self._starting_subscriptions = 0 @property def ws_backoff_remaining(self) -> float: @@ -1269,33 +1271,35 @@ async def _start_subscription( Returns the unsubscribe function, or None (after logging why) on failure. """ reason: str | Exception + self._starting_subscriptions += 1 try: - ws_monitor = await self._ws_connect() - # Ends early if the connection closes, e.g. when rate limited - if await ws_monitor.wait_for_ack() and ( - unsubscribe := await ws_monitor.start_subscription( - payload, callback, on_error=on_error - ) - ): - _LOGGER.debug("Subscribed to %s", description) - return unsubscribe - if backoff_reason := ws_monitor.backoff_reason: - reason = f"the web socket connection {backoff_reason}" - elif close_message := ws_monitor.close_message: - reason = f"the web socket connection closed ({close_message})" - else: - reason = "the web socket connection closed" - except asyncio.CancelledError: - # Cancelled while connecting or waiting for the ack, before there's an - # unsubscribe handle: don't leave a connection open with nothing on it - if self._ws_monitor and not self._ws_monitor.has_subscriptions: - try: - async with asyncio.timeout(5): - await self._ws_monitor.close() - except (Exception, asyncio.CancelledError): # noqa: BLE001 - keep the cancellation - _LOGGER.warning( - "Couldn't close the web socket after a cancelled subscribe" + try: + ws_monitor = await self._ws_connect() + # Ends early if the connection closes, e.g. when rate limited + if await ws_monitor.wait_for_ack() and ( + unsubscribe := await ws_monitor.start_subscription( + payload, callback, on_error=on_error ) + ): + _LOGGER.debug("Subscribed to %s", description) + return unsubscribe + if backoff_reason := ws_monitor.backoff_reason: + reason = f"the web socket connection {backoff_reason}" + elif close_message := ws_monitor.close_message: + reason = f"the web socket connection closed ({close_message})" + else: + reason = "the web socket connection closed" + finally: + self._starting_subscriptions -= 1 + except asyncio.CancelledError: + # Cancelled before there's an unsubscribe handle + try: + async with asyncio.timeout(5): + await self._close_unused_ws() + except (Exception, asyncio.CancelledError): # noqa: BLE001 - keep the cancellation + _LOGGER.warning( + "Couldn't close the web socket after a cancelled subscribe" + ) raise except Exception as ex: # pylint: disable=broad-except # noqa: BLE001 reason = ex @@ -1306,11 +1310,21 @@ async def _start_subscription( description, reason, ) - if self._ws_monitor and not self._ws_monitor.has_subscriptions: - # Don't leave a connection open, or reconnecting, with nothing on it - await self._ws_monitor.close() + await self._close_unused_ws() return None + async def _close_unused_ws(self) -> None: + """Close the web socket if nothing is on it or still being started on it. + + Don't leave a connection open, or reconnecting, with nothing on it. + """ + if ( + self._ws_monitor + and not self._ws_monitor.has_subscriptions + and not self._starting_subscriptions + ): + await self._ws_monitor.close() + async def _ws_connect(self) -> WebSocketMonitor: """Connect the web socket if needed and return its monitor.""" diff --git a/src/rivian/ws_monitor.py b/src/rivian/ws_monitor.py index f50f684..b5c34b2 100644 --- a/src/rivian/ws_monitor.py +++ b/src/rivian/ws_monitor.py @@ -251,17 +251,17 @@ async def start_subscription( await self._subscribe(_id, payload) except (Exception, asyncio.CancelledError): # Registered before the send, and the caller has no unsubscribe - # handle yet: remove it so a reconnect doesn't resubscribe it + # handle yet: remove it so a reconnect doesn't resubscribe it. Closing + # an unused connection is up to the caller, which knows of others + # still starting. self._subscriptions.pop(_id, None) - try: - async with asyncio.timeout(5): - if not self._subscriptions: - await self.close() - elif self.connected: + if self.connected: + try: + async with asyncio.timeout(5): assert self._ws await self._ws.send_json({"id": _id, "type": "complete"}) - except (Exception, asyncio.CancelledError): # noqa: BLE001 - keep the original error - _LOGGER.warning("Couldn't clean up an interrupted subscription") + except (Exception, asyncio.CancelledError): # noqa: BLE001 - keep the original error + _LOGGER.warning("Couldn't end an interrupted subscription") raise async def unsubscribe() -> None: diff --git a/tests/test_ws_monitor.py b/tests/test_ws_monitor.py index 7b3b433..59b310d 100644 --- a/tests/test_ws_monitor.py +++ b/tests/test_ws_monitor.py @@ -32,6 +32,8 @@ def __init__(self) -> None: self.connections = 0 self.next_on_subscribe = False self.error_on_subscribe = False + # Set to hold the connection_ack until the event is set + self.ack_gate: asyncio.Event | None = None self.url = "" async def handler(self, request: web.Request) -> web.WebSocketResponse: @@ -67,6 +69,8 @@ async def handler(self, request: web.Request) -> web.WebSocketResponse: code=self.close_code, message=self.close_reasons.pop(0).encode() ) else: + if self.ack_gate: + await self.ack_gate.wait() await ws.send_json({"type": "connection_ack"}) # Take subscribe requests until the client closes async for msg in ws: @@ -646,7 +650,7 @@ async def test_interrupted_subscribe_is_removed( ) -> None: """A subscribe that fails or is cancelled while sending isn't resubscribed later. - Only its own registration goes: the connection closes if nothing else is on it. + Only its own registration goes; closing an unused connection is the client's. """ await monitor.new_connection(start_monitor=True) await wait_for(lambda: monitor.connection_ack.is_set()) @@ -672,6 +676,42 @@ async def subscribe(*_: Any) -> None: await task assert monitor._subscriptions == existing + assert monitor.connected + + +@pytest.mark.parametrize("keep_existing", [False, True]) +@pytest.mark.parametrize("failure", ["send_error", "cancel"]) +async def test_interrupted_subscribe_closes_unused_connection( + server: FakeServer, + monitor: WebSocketMonitor, + rivian: Rivian, + failure: str, + keep_existing: bool, +) -> None: + """The client closes the connection when an interrupted subscribe leaves it unused.""" + if keep_existing: + assert await rivian.subscribe_for_vehicle_updates("id", lambda _: None) + sending = asyncio.Event() + + async def subscribe(*_: Any) -> None: + sending.set() + if failure == "send_error": + raise ConnectionResetError("closing") + await asyncio.Event().wait() + + with patch.object(monitor, "_subscribe", side_effect=subscribe): + task = asyncio.create_task( + rivian.subscribe_for_parallax_messages("id", lambda _: None) + ) + await sending.wait() + if failure == "cancel": + task.cancel() + with pytest.raises(asyncio.CancelledError): + await task + else: + assert await task is None + + assert len(monitor._subscriptions) == int(keep_existing) assert monitor.connected is keep_existing @@ -690,3 +730,27 @@ async def test_cancelled_subscribe_before_ack_closes( assert not monitor.connected assert monitor.monitor and monitor.monitor.done() + + +async def test_cancelled_subscribe_keeps_another_waiting_for_the_ack( + server: FakeServer, monitor: WebSocketMonitor, rivian: Rivian +) -> None: + """Cancelling one of two subscribes waiting for the ack leaves the other working.""" + server.ack_gate = asyncio.Event() + cancelled = asyncio.create_task( + rivian.subscribe_for_vehicle_updates("id", lambda _: None) + ) + await wait_for(lambda: server.connections == 1) + # Joins the connection the first opened, and waits for its ack too + waiting = asyncio.create_task( + rivian.subscribe_for_parallax_messages("id", lambda _: None) + ) + await asyncio.sleep(0.1) + cancelled.cancel() + with pytest.raises(asyncio.CancelledError): + await cancelled + server.ack_gate.set() + + assert await waiting + assert monitor.connected + assert len(monitor._subscriptions) == 1 From 217b3ab5398bb1818d6ce0e2b07961de05eac081 Mon Sep 17 00:00:00 2001 From: Nathan Spencer Date: Mon, 16 Nov 2026 03:11:11 +0000 Subject: [PATCH 3/3] Tidy the interrupted subscription cleanup and its tests 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 --- src/rivian/rivian.py | 5 +---- src/rivian/ws_monitor.py | 2 +- tests/test_ws_monitor.py | 10 +++++++++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/rivian/rivian.py b/src/rivian/rivian.py index 0eb2ea2..66c2931 100644 --- a/src/rivian/rivian.py +++ b/src/rivian/rivian.py @@ -1314,10 +1314,7 @@ async def _start_subscription( return None async def _close_unused_ws(self) -> None: - """Close the web socket if nothing is on it or still being started on it. - - Don't leave a connection open, or reconnecting, with nothing on it. - """ + """Close the web socket unless something is on it or still starting on it.""" if ( self._ws_monitor and not self._ws_monitor.has_subscriptions diff --git a/src/rivian/ws_monitor.py b/src/rivian/ws_monitor.py index b5c34b2..6af9139 100644 --- a/src/rivian/ws_monitor.py +++ b/src/rivian/ws_monitor.py @@ -256,9 +256,9 @@ async def start_subscription( # still starting. self._subscriptions.pop(_id, None) if self.connected: + assert self._ws try: async with asyncio.timeout(5): - assert self._ws await self._ws.send_json({"id": _id, "type": "complete"}) except (Exception, asyncio.CancelledError): # noqa: BLE001 - keep the original error _LOGGER.warning("Couldn't end an interrupted subscription") diff --git a/tests/test_ws_monitor.py b/tests/test_ws_monitor.py index 59b310d..bbb7ddf 100644 --- a/tests/test_ws_monitor.py +++ b/tests/test_ws_monitor.py @@ -34,6 +34,8 @@ def __init__(self) -> None: self.error_on_subscribe = False # Set to hold the connection_ack until the event is set self.ack_gate: asyncio.Event | None = None + # Messages received after the connection_ack + self.received: list[dict[str, Any]] = [] self.url = "" async def handler(self, request: web.Request) -> web.WebSocketResponse: @@ -75,6 +77,7 @@ async def handler(self, request: web.Request) -> web.WebSocketResponse: # Take subscribe requests until the client closes async for msg in ws: data = msg.json() + self.received.append(data) if self.next_on_subscribe and data.get("type") == "subscribe": await ws.send_json({"id": data["id"], "type": "next"}) if self.error_on_subscribe and data.get("type") == "subscribe": @@ -677,6 +680,11 @@ async def subscribe(*_: Any) -> None: assert monitor._subscriptions == existing assert monitor.connected + # The server is told the interrupted one is over, in case its subscribe got through + await wait_for(lambda: any(m["type"] == "complete" for m in server.received)) + completed = [m["id"] for m in server.received if m["type"] == "complete"] + assert len(completed) == 1 + assert completed[0] not in existing @pytest.mark.parametrize("keep_existing", [False, True]) @@ -745,7 +753,7 @@ async def test_cancelled_subscribe_keeps_another_waiting_for_the_ack( waiting = asyncio.create_task( rivian.subscribe_for_parallax_messages("id", lambda _: None) ) - await asyncio.sleep(0.1) + await wait_for(lambda: rivian._starting_subscriptions == 2) cancelled.cancel() with pytest.raises(asyncio.CancelledError): await cancelled