fix: single-reader CDP session; prime walk no longer fails silently

prime ran the in-page click walk as a task while drain() also read the
websocket. `websockets` forbids concurrent recv(), so this was always broken --
it just failed differently by version: loudly on websockets 15.0.1, and
silently on 16.0, where the JS still executed but its return value was lost.
The `walk: None` in every prime summary was that failure being swallowed by a
bare `except Exception`, and I read past it.

CDPSession now runs exactly one reader coroutine that dispatches command
replies to futures and events to a queue, so call() and drain() can safely run
concurrently -- which priming genuinely needs, since the requests being observed
are triggered by the script being awaited.

call() also gains a timeout so a lost reply fails instead of hanging, and the
walk now reports its error rather than reporting None.

Tests use a fake websocket that asserts the socket never has more than one
concurrent reader.

129 tests, no network required.

Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-07-25 09:27:24 -07:00
1 parent 5a61890011
commit 06122c66dd
4 files changed
+226 -31

No files matched your search

+12 -5
View File
@@ -161,14 +161,21 @@ async def _run(walk=WALK, workout_date: str | None = None) -> dict[str, Any]:
await session.drain(wait, lambda ev: _observe(ev, found, csrf))
# Walk to a real workout screen so GetAllWorkoutData (and the class
# detail actions) actually fire.
# detail actions) actually fire. The walk and the request-observer run
# concurrently on purpose: the requests we want are triggered *by* the
# walk. CDPSession multiplexes one socket reader, so this is safe.
script = WORKOUT_WALK_JS.replace("__DATE_LABEL__", _date_label(workout_date))
walk_task = asyncio.create_task(session.evaluate(script, await_promise=True))
await session.drain(26, lambda ev: _observe(ev, found, csrf))
drain_task = asyncio.create_task(
session.drain(30, lambda ev: _observe(ev, found, csrf))
)
try:
walk_log = await asyncio.wait_for(walk_task, timeout=10)
except (asyncio.TimeoutError, Exception): # noqa: BLE001 - best effort
walk_log = None
walk_log = await asyncio.wait_for(asyncio.shield(walk_task), timeout=45)
except asyncio.TimeoutError:
walk_log = {"error": "walk timed out"}
except Exception as exc: # noqa: BLE001 - report, do not hide
walk_log = {"error": f"{type(exc).__name__}: {exc}"}
await drain_task
module_version = await session.evaluate(
"fetch('/WodifyClient/moduleservices/moduleversioninfo')"