Fix work recovery and reduce engine and chart overhead
Docs / docs (push) Successful in 33s
Playwright Tests / test-playwright (1, 2) (push) Failing after 1m37s
Playwright Tests / test-playwright (2, 2) (push) Failing after 27s
pre-commit / pre-commit (push) Failing after 2m37s
Test Backend / test-backend (3.12) (push) Successful in 5m7s
Test Backend / test-backend (3.14) (push) Successful in 5m2s
Compose Smoke Test / test-compose (push) Failing after 28s
Playwright Tests / merge-reports (push) Failing after 2m29s

This commit is contained in:
2026-10-29 14:24:17 +01:00
parent 20a0853f69
commit 75b3a93094
25 changed files with 1316 additions and 301 deletions
+42 -32
View File
@@ -722,16 +722,6 @@ class CloudConnector:
# The cameras a person may not see, looked at again per event.
hidden = hidden_from(user, self._app, device=only is not None)
controller = getattr(self._app.state, "flow_controller", None)
told: list[str] = []
if controller is not None:
# A device here sends its frames over HTTP: the tunnel carries
# text only.
snapshot = snapshot_payload(controller, only, device, "http", hidden)
told = list(snapshot.get("demand", []))
await socket.send(
_dump({"op": "ws_msg", "id": stream_id, "text": _dump(snapshot)})
)
# The media this client's tiles are drawing count as demand on the
# devices sending them, as a direct socket's do. The bytes themselves
# are fetched one by one through the portal.
@@ -743,6 +733,20 @@ class CloudConnector:
outbox: asyncio.Task[str] | None = None
sender: asyncio.Task[dict[str, Any]] | None = None
async def rescope() -> bool:
nonlocal only, wanted
if only is not None:
if device and revoked(token, self._app):
await socket.send(
_dump({"op": "ws_close", "id": stream_id, "code": 1008})
)
return False
# A deleted panel scopes to nothing, never to every message.
only = panel_scope(token, self._app) or set()
wanted &= only
demand.want(viewer, wanted)
return True
def heard(text: str) -> None:
nonlocal wanted
asked = wanted_names(text, only, hidden)
@@ -768,6 +772,19 @@ class CloudConnector:
live_view.sessions.attach(device, endpoint)
try:
async with event_bus.subscribe() as queue:
# Subscribe before the snapshot so changes during its send
# cannot fall between the snapshot and the live stream.
told: list[str] = []
if controller is not None:
snapshot = snapshot_payload(
controller, only, device, "http", hidden
)
told = list(snapshot.get("demand", []))
await socket.send(
_dump(
{"op": "ws_msg", "id": stream_id, "text": _dump(snapshot)}
)
)
outbox = asyncio.create_task(endpoint.next())
if device and (ports := demand.ports(device)) != told:
await socket.send(
@@ -779,6 +796,7 @@ class CloudConnector:
}
)
)
catching_up = False
while True:
sender = asyncio.create_task(queue.get())
done, _ = await asyncio.wait(
@@ -796,11 +814,13 @@ class CloudConnector:
if sender not in done:
continue
event = sender.result()
# Behind far enough to lose events, a device catches up
# as after a dashboard: the lost one may have been its
# demand or its unpairing. A person whose cameras opened
# or closed is sent what may be seen now.
resync = only is not None and event_bus.dropped(queue)
# A browser needs the same recovery as a device after
# overflow: the lost value may never be published again.
lost = event_bus.dropped(queue)
catching_up = catching_up or lost
if lost and not await rescope():
return
resync = False
if only is None:
fresh = hidden_from(user, self._app)
if fresh != hidden:
@@ -824,24 +844,10 @@ class CloudConnector:
{"op": "ws_msg", "id": stream_id, "text": _dump(event)}
)
)
if not resync:
if not resync and not (catching_up and queue.empty()):
continue
if only is not None:
if device and revoked(token, self._app):
# Unpaired or deleted: 1008 sends it to check.
await socket.send(
_dump({"op": "ws_close", "id": stream_id, "code": 1008})
)
return
# What this screen shows may have just changed under
# it. Rescope, then resend the snapshot so a dashboard
# it has only now been assigned draws values instead of
# blanks. ``or set()`` is the point: a panel that was
# deleted scopes to nothing, and None would widen this
# socket to everything on the bus.
only = panel_scope(token, self._app) or set()
wanted &= only
demand.want(viewer, wanted)
if not await rescope():
return
if controller is not None:
catch_up = snapshot_payload(
controller, only, device, "http", hidden
@@ -855,6 +861,10 @@ class CloudConnector:
}
)
)
# The snapshot follows the retained backlog. If sending
# it queued more events, finish those before recovering
# again so an older event cannot leave the client stale.
catching_up = catching_up and not queue.empty()
if kind == "dashboard_changed" and only is not None:
# After the snapshot, as the device socket sends it.
await socket.send(