Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(runs): bound live-view sends so a stalled tab cannot slow the bus

rubenluetke10-beepee2383d3988b
diff --git a/jarvis/runs/runs_ws.py b/jarvis/runs/runs_ws.py
index 0a4c9e6249..bf25cc9485 100644
--- a/jarvis/runs/runs_ws.py
+++ b/jarvis/runs/runs_ws.py
@@ -23,6 +23,8 @@ where the Run Inspector view is opened and closed repeatedly.
"""
from __future__ import annotations
+import asyncio
+import contextlib
import logging
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
@@ -38,6 +40,12 @@ log = logging.getLogger(__name__)
router = APIRouter()
+# A frame that cannot be handed to the socket within this bound means the tab
+# stopped reading. The forwarder is a wildcard bus observer and every publish
+# awaits it (up to the bus's own 5 s cap), so a wedged inspector would slow
+# EVERY event in the app until the tab closed. Same bound as the main /ws.
+_SEND_TIMEOUT_S = 3.0
+
def _resolve_bus(ws: WebSocket):
"""Pull the shared EventBus off ``app.state``.
@@ -88,15 +96,26 @@ async def runs_live(ws: WebSocket) -> None:
if kind not in _LIVE_KINDS:
return
try:
- await ws.send_json(
- {
- "type": "event",
- "kind": kind,
- "ts_ms": getattr(event, "timestamp_ns", 0) // 1_000_000,
- "session_id": getattr(event, "session_id", None),
- "trace_id": str(getattr(event, "trace_id", "")),
- }
+ await asyncio.wait_for(
+ ws.send_json(
+ {
+ "type": "event",
+ "kind": kind,
+ "ts_ms": getattr(event, "timestamp_ns", 0) // 1_000_000,
+ "session_id": getattr(event, "session_id", None),
+ "trace_id": str(getattr(event, "trace_id", "")),
+ }
+ ),
+ timeout=_SEND_TIMEOUT_S,
)
+ except TimeoutError:
+ # Stop observing for good: the next publish must not wait on this
+ # tab again. The receive loop's finally still closes the socket.
+ log.warning("runs_ws: send stalled >%ss — detaching the live view", _SEND_TIMEOUT_S)
+ bus.unsubscribe_all(_forward)
+ # Close rather than go silently stale; 1013 = try again later.
+ with contextlib.suppress(Exception):
+ await asyncio.wait_for(ws.close(code=1013), timeout=1.0)
except Exception: # noqa: BLE001,S110 — socket gone; recv loop will terminate
pass
diff --git a/tests/unit/runs/test_runs_ws.py b/tests/unit/runs/test_runs_ws.py
index 9552f5aa3c..54b375cd94 100644
--- a/tests/unit/runs/test_runs_ws.py
+++ b/tests/unit/runs/test_runs_ws.py
@@ -11,7 +11,6 @@ from fastapi.testclient import TestClient
from jarvis.runs.runs_ws import router as runs_ws_router
-
# ---------------------------------------------------------------------------
# Fake bus — matches the real EventBus.subscribe_all / unsubscribe_all API
# (subscribe_all has no typed removal; the implementation uses
@@ -97,3 +96,64 @@ def test_ws_subscribes_to_bus() -> None:
with client.websocket_connect("/api/runs/live") as ws:
ws.receive_json() # welcome
assert len(bus._wildcard) == 1
+
+
+def test_stalled_send_detaches_instead_of_slowing_every_publish(monkeypatch) -> None:
+ """A tab that stops reading must not hold up the bus on every event.
+
+ The forwarder is a wildcard observer; the real bus awaits it (5 s cap) on
+ each publish. Without a send bound it stayed attached and every event in
+ the app paid that wait until the tab closed.
+ """
+ import asyncio
+ from types import SimpleNamespace
+
+ from jarvis.runs import runs_ws
+ from jarvis.runs.runs_ws import _LIVE_KINDS
+
+ monkeypatch.setattr(runs_ws, "_SEND_TIMEOUT_S", 0.05)
+ bus = _FakeBus()
+
+ class _StalledSocket:
+ def __init__(self) -> None:
+ self.scope = {"app": SimpleNamespace(state=SimpleNamespace(bus=bus))}
+ self.closed_with: int | None = None
+ self.disconnected = asyncio.Event()
+
+ async def accept(self) -> None:
+ return None
+
+ async def send_json(self, frame: dict) -> None:
+ if frame.get("type") == "event":
+ await asyncio.Event().wait() # the tab never reads again
+
+ async def receive_text(self) -> str:
+ await self.disconnected.wait()
+ raise runs_ws.WebSocketDisconnect()
+
+ async def close(self, code: int = 1000) -> None:
+ self.closed_with = code
+ self.disconnected.set()
+
+ kind = next(iter(_LIVE_KINDS))
+ event = type(kind, (), {"timestamp_ns": 0, "session_id": "s", "trace_id": "t"})()
+
+ async def _run() -> None:
+ subscribed = asyncio.Event()
+ subscribe_all = bus.subscribe_all
+
+ def _subscribe_all(cb) -> None: # noqa: ANN001
+ subscribe_all(cb)
+ subscribed.set()
+
+ bus.subscribe_all = _subscribe_all # type: ignore[method-assign]
+ ws = _StalledSocket()
+ endpoint = asyncio.create_task(runs_ws.runs_live(ws))
+ await asyncio.wait_for(subscribed.wait(), timeout=1.0)
+ forward = bus._wildcard[0]
+ await asyncio.wait_for(forward(event), timeout=1.0)
+ assert bus._wildcard == []
+ assert ws.closed_with == 1013
+ await asyncio.wait_for(endpoint, timeout=1.0)
+
+ asyncio.run(_run())