Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions src/rivian/rivian.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ def __init__(
self._otp_needed = False
self._otp_token = ""

# 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] = {}

Expand Down Expand Up @@ -1315,25 +1318,28 @@ 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
# 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(
"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]
Expand Down
58 changes: 38 additions & 20 deletions src/rivian/ws_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -189,23 +192,33 @@ 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."""
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()
"""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
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.
Expand Down Expand Up @@ -426,9 +439,14 @@ 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)
# 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:
await self._ws.close()
await cancel_task(self._monitor_task, self._receiver_task)

def _log_message(
self,
Expand Down
104 changes: 104 additions & 0 deletions tests/test_ws_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,110 @@ 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


@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_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,
Expand Down
Loading