diff --git a/src/rivian/rivian.py b/src/rivian/rivian.py index 1560f92..66c2931 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,22 +1271,36 @@ 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 + 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" ) - ): - _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" + raise except Exception as ex: # pylint: disable=broad-except # noqa: BLE001 reason = ex # Expected while backing off, so less severe than other failures @@ -1294,11 +1310,18 @@ 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 unless something is on it or still starting 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 dd2578d..6af9139 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. Closing + # an unused connection is up to the caller, which knows of others + # still starting. + self._subscriptions.pop(_id, None) + if self.connected: + assert self._ws + try: + async with asyncio.timeout(5): + 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") + raise async def unsubscribe() -> None: """Unsubscribe.""" diff --git a/tests/test_ws_monitor.py b/tests/test_ws_monitor.py index 3ea5bbe..bbb7ddf 100644 --- a/tests/test_ws_monitor.py +++ b/tests/test_ws_monitor.py @@ -32,6 +32,10 @@ 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 + # Messages received after the connection_ack + self.received: list[dict[str, Any]] = [] self.url = "" async def handler(self, request: web.Request) -> web.WebSocketResponse: @@ -67,10 +71,13 @@ 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: 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": @@ -637,3 +644,121 @@ 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; closing an unused connection is the client's. + """ + 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 + # 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]) +@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 + + +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() + + +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 wait_for(lambda: rivian._starting_subscriptions == 2) + cancelled.cancel() + with pytest.raises(asyncio.CancelledError): + await cancelled + server.ack_gate.set() + + assert await waiting + assert monitor.connected + assert len(monitor._subscriptions) == 1