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(agent-chat): stop the service build from deadlocking the event loop

rubenluetke10-beepde22c3df2645
diff --git a/jarvis/agent_chat/service.py b/jarvis/agent_chat/service.py
index cb5955bebd..2315dfdfe6 100644
--- a/jarvis/agent_chat/service.py
+++ b/jarvis/agent_chat/service.py
@@ -76,6 +76,25 @@ _ORPHANED_TURN_ERROR: Final = (
Subscriber = asyncio.Queue[dict[str, Any]]
+#: The app's loop, resolved by the route layer BEFORE it takes its build lock
+#: (``remember_app_loop``). Asking the loop from inside that lock deadlocked
+#: the whole app (2026-10-07): the worker waited for the loop while the loop
+#: waited for the lock in an ``async`` route.
+_KNOWN_LOOP: asyncio.AbstractEventLoop | None = None
+
+
+def remember_app_loop() -> None:
+ """Find and record the app's loop, so building the service never asks it.
+
+ Call it without holding any lock the loop could be waiting for: from a
+ worker thread, finding the loop waits until the loop answers.
+ """
+ global _KNOWN_LOOP # one process-wide reference, set before the service is built
+ loop, _on_loop = _app_loop()
+ if loop is not None:
+ _KNOWN_LOOP = loop
+
+
def _app_loop() -> tuple[asyncio.AbstractEventLoop | None, bool]:
"""The app's event loop, and whether this code runs on it.
@@ -89,6 +108,9 @@ def _app_loop() -> tuple[asyncio.AbstractEventLoop | None, bool]:
return asyncio.get_running_loop(), True
except RuntimeError:
pass
+ known = _KNOWN_LOOP
+ if known is not None and known.is_running():
+ return known, False
try:
from anyio.from_thread import run_sync
diff --git a/jarvis/ui/web/agent_chat_routes.py b/jarvis/ui/web/agent_chat_routes.py
index a4ad8275f5..0edc0f1929 100644
--- a/jarvis/ui/web/agent_chat_routes.py
+++ b/jarvis/ui/web/agent_chat_routes.py
@@ -298,6 +298,13 @@ def _service_from_state(state: Any) -> AgentChatService | None:
svc = getattr(state, "agent_chat", None)
if svc is not None:
return svc
+ if getattr(state, "agent_chat_factory", None) is not None:
+ # Resolve the app's loop while NOT holding the lock: from a worker
+ # thread this waits for the loop, and the loop may itself be waiting
+ # for the lock in an async route — a deadlock that froze every route.
+ from jarvis.agent_chat.service import remember_app_loop
+
+ remember_app_loop()
with _SERVICE_BUILD_LOCK:
svc = getattr(state, "agent_chat", None)
if svc is not None:
diff --git a/tests/unit/agent_chat/test_service_build_deadlock.py b/tests/unit/agent_chat/test_service_build_deadlock.py
new file mode 100644
index 0000000000..6afa3322f4
--- /dev/null
+++ b/tests/unit/agent_chat/test_service_build_deadlock.py
@@ -0,0 +1,52 @@
+"""Building the chat service from a worker thread never deadlocks the app.
+
+Live 2026-10-07 the whole app froze for minutes after boot: a sync route's
+worker thread built the service under ``_SERVICE_BUILD_LOCK`` and, while
+sealing a restart's turns, asked the event loop for itself; the loop was
+blocked on the same lock in the async ``get_catalog`` route. The loop is now
+found before the lock is taken.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import threading
+import time
+from types import SimpleNamespace
+from typing import Any
+
+import anyio
+
+import jarvis.agent_chat.service as service_module
+from jarvis.ui.web.agent_chat_routes import _service_from_state
+
+
+def test_an_async_route_waiting_for_a_worker_build_does_not_deadlock(monkeypatch) -> None:
+ monkeypatch.setattr(service_module, "_KNOWN_LOOP", None)
+ outcome: list[Any] = []
+
+ async def scenario() -> None:
+ app_loop = asyncio.get_running_loop()
+ entered = asyncio.Event()
+
+ def factory() -> SimpleNamespace:
+ app_loop.call_soon_threadsafe(entered.set)
+ # Let the loop thread reach the build lock first, as it did live.
+ time.sleep(0.1)
+ found, _on_loop = service_module._app_loop()
+ return SimpleNamespace(loop=found)
+
+ state = SimpleNamespace(agent_chat=None, agent_chat_factory=factory)
+ worker = asyncio.ensure_future(anyio.to_thread.run_sync(_service_from_state, state))
+ await entered.wait()
+ on_loop = _service_from_state(state) # the async route, on the loop thread
+ built = await worker
+ outcome.append((built, on_loop, asyncio.get_running_loop()))
+
+ runner = threading.Thread(target=lambda: asyncio.run(scenario()), daemon=True)
+ runner.start()
+ runner.join(timeout=10)
+ assert not runner.is_alive(), "the loop and the service build waited on each other"
+ built, on_loop, loop = outcome[0]
+ assert built is on_loop
+ assert built.loop is loop