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(voice): keep native frees and first SDK imports off the event loop

rubenluetke10-beepd25b9ff64ce5
diff --git a/jarvis/core/loop_executor.py b/jarvis/core/loop_executor.py
index 063a4f5808..d06d9b8b69 100644
--- a/jarvis/core/loop_executor.py
+++ b/jarvis/core/loop_executor.py
@@ -203,7 +203,21 @@ async def warm_anyio_worker_pool(workers: int = ANYIO_WARM_WORKERS) -> int:
loop.call_soon_threadsafe(all_up.set)
gate.wait(_PREWARM_TIMEOUT_S)
- tasks = [asyncio.ensure_future(anyio.to_thread.run_sync(_park)) for _ in range(workers)]
+ # Submitted in small batches with a loop yield in between, never all at
+ # once: each submission that finds no idle worker performs a
+ # ``Thread.start()`` ON the loop, and 32 of those back-to-back on a
+ # starved cold-boot box added up to one 15 s block (the BUG-189 stall
+ # shape, just moved to voice-ready+5 s). Between batches the loop runs
+ # its other callbacks; the already-parked workers keep waiting on the
+ # shared gate, so the all-up contract is unchanged.
+ tasks: list[asyncio.Future[None]] = []
+ batch = 4
+ for offset in range(0, workers, batch):
+ tasks.extend(
+ asyncio.ensure_future(anyio.to_thread.run_sync(_park))
+ for _ in range(min(batch, workers - offset))
+ )
+ await asyncio.sleep(0)
try:
await asyncio.wait_for(all_up.wait(), timeout=_PREWARM_TIMEOUT_S)
except TimeoutError:
diff --git a/jarvis/plugins/realtime/gemini_live.py b/jarvis/plugins/realtime/gemini_live.py
index b3b1af102f..e5069d985f 100644
--- a/jarvis/plugins/realtime/gemini_live.py
+++ b/jarvis/plugins/realtime/gemini_live.py
@@ -8,6 +8,7 @@ mono PCM at 16 kHz and emits the same format at 24 kHz.
from __future__ import annotations
+import asyncio
import logging
import time
from collections.abc import AsyncIterator
@@ -41,6 +42,17 @@ _COMPRESSION_TRIGGER_TOKENS = 120_000
_COMPRESSION_TARGET_TOKENS = 100_000
+def _import_genai_types() -> Any:
+ """Import google.genai and return its ``types`` module (worker helper).
+
+ lazy (AP-26); called via ``asyncio.to_thread`` so the FIRST import of the
+ SDK tree never runs on the event loop (BUG-189 class).
+ """
+ from google.genai import types
+
+ return types
+
+
def _compression_kwargs(types: Any) -> dict[str, Any]:
"""Sliding-window compression config, or {} on an SDK without it.
@@ -1258,7 +1270,11 @@ class GeminiLiveProvider:
if not await self.can_open_duplex_session():
raise RuntimeError(self._unconfigured_message())
- from google.genai import types # lazy (AP-26)
+ # lazy (AP-26), imported OFF the loop: the first google.genai import in
+ # the process reads a large SDK tree from a possibly cold disk — inline
+ # it is a BUG-189-class loop stall. Every later `from google.genai
+ # import types` in the session methods is then a sys.modules hit.
+ types = await asyncio.to_thread(_import_genai_types)
client = await self._build_client()
voice = str(getattr(cfg, "voice", "") or "").strip() or str(
diff --git a/jarvis/plugins/realtime/openai_realtime.py b/jarvis/plugins/realtime/openai_realtime.py
index 31317b6bf8..371ae8aac0 100644
--- a/jarvis/plugins/realtime/openai_realtime.py
+++ b/jarvis/plugins/realtime/openai_realtime.py
@@ -1372,6 +1372,18 @@ async def _open_realtime_session(
return session
+def _import_async_openai() -> Any:
+ """Import the SDK and return ``AsyncOpenAI`` (worker-thread helper).
+
+ lazy (AP-26); called via ``asyncio.to_thread`` so the FIRST import in the
+ process — hundreds of modules off a possibly cold disk — never runs on the
+ event loop (BUG-189 class).
+ """
+ from openai import AsyncOpenAI
+
+ return AsyncOpenAI
+
+
class OpenAIRealtimeProvider:
"""Structural provider entry point for the OpenAI Realtime family."""
@@ -1405,9 +1417,13 @@ class OpenAIRealtimeProvider:
if not self._api_key:
raise RuntimeError("OpenAI Realtime API key is not configured")
- from openai import AsyncOpenAI # lazy (AP-26)
+ # lazy (AP-26), and imported OFF the loop: the first `import openai` in
+ # the process reads hundreds of modules from a possibly cold disk —
+ # done inline it is a BUG-189-class loop stall. Later calls hit
+ # sys.modules and pay nothing.
+ client_cls = await asyncio.to_thread(_import_async_openai)
- client = AsyncOpenAI(api_key=self._api_key)
+ client = client_cls(api_key=self._api_key)
connect_model = str(getattr(cfg, "model", "") or _MODEL)
return await _open_realtime_session(client, cfg, model=connect_model)
@@ -2215,6 +2231,10 @@ class LocalRealtimeProvider:
"No server URL configured for the self-hosted realtime provider "
"— set it on the provider card first (e.g. http://127.0.0.1:8080)."
)
+ # Warm the SDK import off the loop before _shared_client (sync, on the
+ # loop) touches it — the first `import openai` on a cold disk is a
+ # BUG-189-class stall; afterwards it is a sys.modules hit.
+ await asyncio.to_thread(_import_async_openai)
managed_interactive = self._managed_interactive_preflight
self._managed_interactive_preflight = False
retry_window_s = (
diff --git a/jarvis/plugins/wake/vosk_native.py b/jarvis/plugins/wake/vosk_native.py
index b012f565e7..6b792eb41c 100644
--- a/jarvis/plugins/wake/vosk_native.py
+++ b/jarvis/plugins/wake/vosk_native.py
@@ -17,10 +17,51 @@ wrapped.
from __future__ import annotations
import threading
+from concurrent.futures import ThreadPoolExecutor
from typing import Any
_LOCK = threading.Lock()
+# One worker that does nothing but let recognizers die. ``vosk_recognizer_free``
+# (KaldiRecognizer.__del__) is a native call measured at 15 s on a cold, paging
+# box (2026-08-30 "Event loop STALLED" stack) — and it runs on WHICHEVER thread
+# drops the last reference, which was the asyncio loop during wake teardown.
+_RELEASE_POOL: ThreadPoolExecutor | None = None
+_RELEASE_POOL_LOCK = threading.Lock()
+
+
+def _release_pool() -> ThreadPoolExecutor:
+ global _RELEASE_POOL
+ with _RELEASE_POOL_LOCK:
+ if _RELEASE_POOL is None:
+ _RELEASE_POOL = ThreadPoolExecutor(max_workers=1, thread_name_prefix="vosk-release")
+ return _RELEASE_POOL
+
+
+def release_recognizer(rec: Any) -> None:
+ """Let *rec*'s native free run on the release worker, never on the caller.
+
+ The last reference is moved into a box and cleared on the worker under the
+ same process-wide lock every other native call holds (BUG-151 — the free is
+ a native call too). Never raises: during interpreter shutdown the executor
+ refuses new work, and the box is then cleared inline — exactly the pre-fix
+ behaviour, which is acceptable on the one path where no loop is left to
+ stall.
+ """
+ if rec is None:
+ return
+ box = [rec]
+ del rec
+
+ def _drop() -> None:
+ with _LOCK:
+ box.clear()
+
+ try:
+ _release_pool().submit(_drop)
+ except Exception: # noqa: BLE001 — shutdown fallback, see docstring
+ _drop()
+
def native_call(fn: Any, *args: Any, **kwargs: Any) -> Any:
"""Run one libvosk constructor or method behind the process-wide lock."""
@@ -78,6 +119,21 @@ class LockedRecognizer:
with _LOCK:
return self._rec.SetGrammar(grammar)
+ def __del__(self) -> None:
+ # The proxy is the ONLY holder of the native recognizer (wrap_recognizer
+ # hands out the proxy, never the inner object), so this is the one
+ # chokepoint where every recognizer death passes — a one-shot verify
+ # discarded mid-loop, stage-1 recognizers replaced by _fresh_recs, a
+ # cancelled wake task's closure. Hand the corpse to the release worker
+ # so the 15 s native free never runs on the thread that dropped it.
+ try:
+ rec = self.__dict__.pop("_rec", None)
+ if rec is not None and is_native_recognizer(rec):
+ release_recognizer(rec)
+ except Exception: # noqa: BLE001, S110 — __del__ must never raise; the
+ # worst case is the pre-fix behaviour (free on this thread).
+ pass
+
def __getattr__(self, name: str) -> Any:
"""Every OTHER native method stays reachable — and locked.
diff --git a/tests/unit/plugins/wake/test_vosk_native.py b/tests/unit/plugins/wake/test_vosk_native.py
index 3c86875f84..a13f70d9ff 100644
--- a/tests/unit/plugins/wake/test_vosk_native.py
+++ b/tests/unit/plugins/wake/test_vosk_native.py
@@ -161,3 +161,61 @@ def test_locked_recognizer_reports_a_missing_optional_method_honestly() -> None:
through the proxy — the provider's static-grammar degrade depends on it."""
rec = LockedRecognizer(_Contended())
assert getattr(rec, "SetMaxAlternatives", None) is None
+
+
+# --- the release worker (cold-boot loop stall, 2026-08-30) -------------------
+
+
+def test_release_recognizer_frees_on_the_worker_thread() -> None:
+ """vosk_recognizer_free runs wherever the last reference dies — measured at
+ 15 s on the asyncio loop during a cold boot. The release helper must move
+ that death onto its own worker."""
+ from jarvis.plugins.wake.vosk_native import release_recognizer
+
+ died_on: list[str] = []
+ done = threading.Event()
+
+ class _Corpse:
+ def __del__(self) -> None:
+ died_on.append(threading.current_thread().name)
+ done.set()
+
+ release_recognizer(_Corpse())
+ assert done.wait(2.0)
+ assert died_on and died_on[0].startswith("vosk-release")
+
+
+def test_dropping_the_proxy_hands_the_native_recognizer_to_the_worker() -> None:
+ """The proxy is the one chokepoint every recognizer death passes: deleting
+ it anywhere (a cancelled wake task, a replaced stage-1 list) must free the
+ native object on the release worker, not on the deleting thread."""
+ died_on: list[str] = []
+ done = threading.Event()
+
+ class KaldiRecognizer:
+ def __del__(self) -> None:
+ died_on.append(threading.current_thread().name)
+ done.set()
+
+ KaldiRecognizer.__module__ = "vosk"
+ wrapped = wrap_recognizer(KaldiRecognizer())
+ assert isinstance(wrapped, LockedRecognizer)
+ del wrapped
+ assert done.wait(2.0)
+ assert died_on and died_on[0].startswith("vosk-release")
+
+
+def test_test_doubles_are_not_routed_through_the_release_worker() -> None:
+ """A fake recognizer in a unit test dies inline, exactly as before."""
+ died_on: list[str] = []
+
+ class _Fake:
+ def __del__(self) -> None:
+ died_on.append(threading.current_thread().name)
+
+ proxy = LockedRecognizer(_Fake())
+ del proxy
+ import gc
+
+ gc.collect()
+ assert died_on == [threading.current_thread().name]