Skip to content

Fix async websocket reader starvation and dispatch races - #249

Open
adamlogan73 wants to merge 1 commit into
mainfrom
fix/async-reader-dispatch
Open

adamlogan73 wants to merge 1 commit into
mainfrom
fix/async-reader-dispatch

Conversation

@adamlogan73

Copy link
Copy Markdown
Collaborator

Summary

  • Alternative to Fix handling of multiple async readers #248: replaces the racing recv() polling loop with a single background reader task that is the only thing allowed to touch the socket, coordinated via asyncio.Condition instead of racing _async_recv() calls
  • Errors are routed to the specific msg_id they belong to when attributable (e.g. an HA error response for a known request), and only broadcast to every pending waiter when the connection itself is dead
  • Bumps nimax to >=1.1.1 and enables ws_id_extractor = "id" so test cassettes correctly correlate concurrent, out-of-order request/response replay instead of relying on send position

Related

Addresses the reader starvation / interleaved-fragment issues raised in #248 (bugs 2 & 3). Does not address bug 1 (send blocked behind a concurrent recv in the underlying urllib3.future transport) — that remains an upstream issue.

Test plan

  • uv run ruff check homeassistant_api/asyncwebsocket.py
  • uv run zuban check homeassistant_api/asyncwebsocket.py
  • uv run pytest -q — 175 passed

Replace the racing-recv() polling loop with a single background reader
task that is the only thing allowed to touch the socket. recv() calls
now wait on a shared Condition and get notified per dispatched message,
instead of each concurrently calling _async_recv() and starving each
other. Errors are routed to the specific msg_id they belong to when
attributable, and broadcast to all waiters only when the connection
itself is dead.

Bump nimax to 1.1.1 and enable id-aware WebSocket replay gating so test
cassettes correctly model concurrent, out-of-order request/response
correlation instead of relying on positional ordering.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant