From a315834e52bb690955f216e4f885b8216f378da2 Mon Sep 17 00:00:00 2001 From: Nathan Spencer Date: Mon, 16 Nov 2026 02:50:59 +0000 Subject: [PATCH 1/3] Open one web socket for subscribes started together 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 --- src/rivian/rivian.py | 39 +++++++++++++++++++++------------------ tests/test_ws_monitor.py | 14 ++++++++++++++ 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/rivian/rivian.py b/src/rivian/rivian.py index 1560f92..a528c84 100644 --- a/src/rivian/rivian.py +++ b/src/rivian/rivian.py @@ -157,6 +157,8 @@ def __init__( self._otp_needed = False self._otp_token = "" + # Held while connecting, so subscribes started together share a connection + self._ws_connect_lock = asyncio.Lock() self._ws_monitor: WebSocketMonitor | None = None self._subscriptions: dict[str, str] = {} @@ -1315,25 +1317,26 @@ async def connection_init(websocket: ClientWebSocketResponse[bool]) -> None: } ) - if not self._ws_monitor: - self._ws_monitor = WebSocketMonitor( - self, - GRAPHQL_WEBSOCKET, - connection_init, - backoff_key=self._user_session_token, - ) - ws_monitor = self._ws_monitor - if ws_monitor.websocket is None or ws_monitor.websocket.closed: - # A backoff only holds off new connections, not an open one - if remaining := ws_monitor.backoff_remaining: - raise RivianApiException( - "the web socket is waiting to reconnect " - f"({remaining:.0f} seconds left)" + async with self._ws_connect_lock: + if not self._ws_monitor: + self._ws_monitor = WebSocketMonitor( + self, + GRAPHQL_WEBSOCKET, + connection_init, + backoff_key=self._user_session_token, ) - await ws_monitor.new_connection(True) - if ws_monitor.monitor is None or ws_monitor.monitor.done(): - await ws_monitor.start_monitor() - return ws_monitor + ws_monitor = self._ws_monitor + if ws_monitor.websocket is None or ws_monitor.websocket.closed: + # A backoff only holds off new connections, not an open one + if remaining := ws_monitor.backoff_remaining: + raise RivianApiException( + "the web socket is waiting to reconnect " + f"({remaining:.0f} seconds left)" + ) + await ws_monitor.new_connection(True) + if ws_monitor.monitor is None or ws_monitor.monitor.done(): + await ws_monitor.start_monitor() + return ws_monitor async def __graphql_query( self, headers: dict[str, str], url: str, body: dict[str, Any] diff --git a/tests/test_ws_monitor.py b/tests/test_ws_monitor.py index 3ea5bbe..795b70c 100644 --- a/tests/test_ws_monitor.py +++ b/tests/test_ws_monitor.py @@ -452,6 +452,20 @@ async def test_unsubscribing_last_subscription_closes( assert server.connections == 2 +async def test_simultaneous_subscribes_share_one_connection( + server: FakeServer, monitor: WebSocketMonitor, rivian: Rivian +) -> None: + """Subscribes started together on a closed web socket open one connection.""" + first, second = await asyncio.gather( + rivian.subscribe_for_vehicle_updates("id", lambda _: None), + rivian.subscribe_for_parallax_messages("id", lambda _: None), + ) + + assert first and second + assert server.connections == 1 + assert len(monitor._subscriptions) == 2 + + async def test_failing_callback_keeps_connection( server: FakeServer, monitor: WebSocketMonitor, From 5d262aba33b6caf31690279f050ebae6e54762c8 Mon Sep 17 00:00:00 2001 From: Nathan Spencer Date: Mon, 16 Nov 2026 03:31:43 +0000 Subject: [PATCH 2/3] Don't let a closing web socket cancel its replacement 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 --- src/rivian/rivian.py | 4 ++- src/rivian/ws_monitor.py | 46 +++++++++++++++++-------------- tests/test_ws_monitor.py | 58 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 21 deletions(-) diff --git a/src/rivian/rivian.py b/src/rivian/rivian.py index a528c84..92f84f9 100644 --- a/src/rivian/rivian.py +++ b/src/rivian/rivian.py @@ -1326,7 +1326,9 @@ async def connection_init(websocket: ClientWebSocketResponse[bool]) -> None: backoff_key=self._user_session_token, ) ws_monitor = self._ws_monitor - if ws_monitor.websocket is None or ws_monitor.websocket.closed: + # Includes a connection that's closing: it's replaced once the close + # is done + if not ws_monitor.connected: # A backoff only holds off new connections, not an open one if remaining := ws_monitor.backoff_remaining: raise RivianApiException( diff --git a/src/rivian/ws_monitor.py b/src/rivian/ws_monitor.py index dd2578d..35ebaa5 100644 --- a/src/rivian/ws_monitor.py +++ b/src/rivian/ws_monitor.py @@ -98,6 +98,9 @@ def __init__( # Set while there's no open connection self._closed: asyncio.Event = asyncio.Event() self._closed.set() + # Held while connecting or closing, so a new connection never starts until + # an old one has finished closing, and that close can't touch it + self._lifecycle_lock = asyncio.Lock() self._disconnect = False # Why the current connection is backing off, at most once per connection self._backoff_reason: str | None = None @@ -190,22 +193,24 @@ def has_subscriptions(self) -> bool: async def new_connection(self, start_monitor: bool = False) -> None: """Create a new connection and, optionally, start the monitor.""" - await cancel_task(self._receiver_task) - self._disconnect = False - self._backoff_reason = None - self._close_message = None - self._forbidden = False - self._connection_ack.clear() - # pylint: disable=protected-access - assert self._account._session - self._ws = await self._account._session.ws_connect( - url=self._url, headers={"sec-websocket-protocol": "graphql-transport-ws"} - ) - await self._connection_init(self._ws) - self._closed.clear() - self._receiver_task = asyncio.ensure_future(self._receiver()) - if start_monitor: - await self.start_monitor() + async with self._lifecycle_lock: + await cancel_task(self._receiver_task) + self._disconnect = False + self._backoff_reason = None + self._close_message = None + self._forbidden = False + self._connection_ack.clear() + # pylint: disable=protected-access + assert self._account._session + self._ws = await self._account._session.ws_connect( + url=self._url, + headers={"sec-websocket-protocol": "graphql-transport-ws"}, + ) + await self._connection_init(self._ws) + self._closed.clear() + self._receiver_task = asyncio.ensure_future(self._receiver()) + if start_monitor: + await self.start_monitor() async def wait_for_ack(self) -> bool: """Wait for the connection to be acknowledged or to close. @@ -425,10 +430,11 @@ async def stop_monitor(self) -> None: async def close(self) -> None: """Close the web socket.""" - self._disconnect = True - if self._ws: - await self._ws.close() - await cancel_task(self._monitor_task, self._receiver_task) + async with self._lifecycle_lock: + self._disconnect = True + if self._ws: + await self._ws.close() + await cancel_task(self._monitor_task, self._receiver_task) def _log_message( self, diff --git a/tests/test_ws_monitor.py b/tests/test_ws_monitor.py index 795b70c..18e1bf7 100644 --- a/tests/test_ws_monitor.py +++ b/tests/test_ws_monitor.py @@ -466,6 +466,64 @@ async def test_simultaneous_subscribes_share_one_connection( assert len(monitor._subscriptions) == 2 +@pytest.mark.parametrize("phase", ["before_closed", "after_closed"]) +async def test_subscribe_while_closing_gets_a_working_connection( + server: FakeServer, monitor: WebSocketMonitor, rivian: Rivian, phase: str +) -> None: + """A subscribe started while the last unsubscribe closes the web socket works. + + It waits for the close, then gets its own connection, which the close can't + cancel. aiohttp marks the socket closed partway through its close, after + breaking the receiver out of receive() and before the close handshake. + """ + unsubscribe = await rivian.subscribe_for_vehicle_updates("id", lambda _: None) + assert unsubscribe + old_ws: Any = monitor.websocket + gate = asyncio.Event() + closing = asyncio.Event() + if phase == "after_closed": + # Hold the close handshake, when `closed` is already True + real_close = old_ws._writer.close + + async def slow_close(*args: Any, **kwargs: Any) -> None: + closing.set() + await gate.wait() + await real_close(*args, **kwargs) + + old_ws._writer.close = slow_close + else: + # Hold the close before `closed` is True + real_ws_close = old_ws.close + + async def held_close(*args: Any, **kwargs: Any) -> bool: + closing.set() + await gate.wait() + return await real_ws_close(*args, **kwargs) + + old_ws.close = held_close + + closer = asyncio.create_task(unsubscribe()) + await closing.wait() + starting = asyncio.create_task( + rivian.subscribe_for_parallax_messages("id", lambda _: None) + ) + try: + # Waiting for the close to finish, holding the connect lock + await wait_for(rivian._ws_connect_lock.locked) + finally: + gate.set() + await closer + assert await starting + + # The new subscription is on a live connection with a receiver and monitor + assert server.connections == 2 + assert monitor.connected + assert monitor.websocket is not old_ws + assert monitor._receiver_task and not monitor._receiver_task.done() + assert monitor.monitor and not monitor.monitor.done() + assert len(monitor._subscriptions) == 1 + + async def test_failing_callback_keeps_connection( server: FakeServer, monitor: WebSocketMonitor, From fc97d72c3b77881c4d9c7736faae40f766463d2e Mon Sep 17 00:00:00 2001 From: Nathan Spencer Date: Mon, 16 Nov 2026 04:28:08 +0000 Subject: [PATCH 3/3] Stop a stuck reconnect on close, and reuse a fresh connection 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 --- src/rivian/rivian.py | 3 ++- src/rivian/ws_monitor.py | 14 +++++++++++++- tests/test_ws_monitor.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/rivian/rivian.py b/src/rivian/rivian.py index 92f84f9..32faa38 100644 --- a/src/rivian/rivian.py +++ b/src/rivian/rivian.py @@ -157,7 +157,8 @@ def __init__( self._otp_needed = False self._otp_token = "" - # Held while connecting, so subscribes started together share a connection + # Held while connecting, so subscribes started together, or while a closing + # connection is replaced, share one connection self._ws_connect_lock = asyncio.Lock() self._ws_monitor: WebSocketMonitor | None = None self._subscriptions: dict[str, str] = {} diff --git a/src/rivian/ws_monitor.py b/src/rivian/ws_monitor.py index 35ebaa5..21d3990 100644 --- a/src/rivian/ws_monitor.py +++ b/src/rivian/ws_monitor.py @@ -192,8 +192,16 @@ def has_subscriptions(self) -> bool: return bool(self._subscriptions) async def new_connection(self, start_monitor: bool = False) -> None: - """Create a new connection and, optionally, start the monitor.""" + """Create a new connection and, optionally, start the monitor. + + Reuses a connection another caller opened while this one waited for it. + """ + previous = self._ws async with self._lifecycle_lock: + if self._ws is not previous and self.connected: + if start_monitor: + await self.start_monitor() + return await cancel_task(self._receiver_task) self._disconnect = False self._backoff_reason = None @@ -430,6 +438,10 @@ async def stop_monitor(self) -> None: async def close(self) -> None: """Close the web socket.""" + self._disconnect = True + # Stop a reconnect the monitor may be in first: it holds the lock, and a + # connect can take as long as the session's timeout (5 minutes by default) + await cancel_task(self._monitor_task) async with self._lifecycle_lock: self._disconnect = True if self._ws: diff --git a/tests/test_ws_monitor.py b/tests/test_ws_monitor.py index 18e1bf7..25956b0 100644 --- a/tests/test_ws_monitor.py +++ b/tests/test_ws_monitor.py @@ -524,6 +524,38 @@ async def held_close(*args: Any, **kwargs: Any) -> bool: assert len(monitor._subscriptions) == 1 +async def test_queued_new_connection_reuses_the_new_one( + server: FakeServer, monitor: WebSocketMonitor +) -> None: + """A connect that waited for another (e.g. the monitor's) uses its connection.""" + await asyncio.gather(monitor.new_connection(), monitor.new_connection()) + + assert server.connections == 1 + assert monitor.connected + + +async def test_close_stops_a_stuck_reconnect(monitor: WebSocketMonitor) -> None: + """Closing doesn't wait for a reconnect the monitor is stuck in.""" + connecting = asyncio.Event() + + async def hang(*_: Any, **__: Any) -> None: + connecting.set() + await asyncio.Event().wait() + + with patch.object(monitor._account._session, "ws_connect", hang): + # As the monitor's own reconnect, which holds the lifecycle lock + reconnect = asyncio.ensure_future(monitor.new_connection()) + monitor._monitor_task = reconnect + await connecting.wait() + try: + async with asyncio.timeout(2): + await monitor.close() + finally: + reconnect.cancel() + + assert reconnect.cancelled() + + async def test_failing_callback_keeps_connection( server: FakeServer, monitor: WebSocketMonitor,