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 session persistence off audio loop

Ruben Lütke560dbd12829f
diff --git a/jarvis/sessions/recorder.py b/jarvis/sessions/recorder.py
index 9a028f0386..d55bb84435 100644
--- a/jarvis/sessions/recorder.py
+++ b/jarvis/sessions/recorder.py
@@ -32,7 +32,9 @@ replay.
"""
from __future__ import annotations
+import asyncio
import logging
+import threading
import time
from dataclasses import dataclass, field
from typing import Any
@@ -166,6 +168,16 @@ class SessionRecorder:
def __init__(self, store: SessionStore) -> None:
self._store = store
self._state: _SessionState | None = None
+ # SQLite is synchronous and can briefly wait on another reader/writer.
+ # Keep those waits off the asyncio loop so microphone handoff, live
+ # level metering, and the native overlay remain responsive while a
+ # session event is persisted. The lock preserves event order and keeps
+ # this recorder's in-memory state machine single-threaded.
+ self._dispatch_lock = asyncio.Lock()
+ # ``to_thread`` work cannot be force-cancelled. If the EventBus timeout
+ # cancels an awaiting coroutine, this second lock keeps the still-live
+ # worker serialized with the next dispatch.
+ self._dispatch_thread_lock = threading.Lock()
# Fallback when the pipeline forgets VoiceTurnStarted — we assign
# our own turn_id at the first turn-relevant event.
self._auto_turn_counter: int = 0
@@ -194,7 +206,8 @@ class SessionRecorder:
but we definitely don't want error-log spam per voice turn.
"""
try:
- self._dispatch(event)
+ async with self._dispatch_lock:
+ await asyncio.to_thread(self._dispatch_serialized, event)
except Exception as exc: # noqa: BLE001
log.warning(
"SessionRecorder dispatch failed",
@@ -202,6 +215,10 @@ class SessionRecorder:
extra={"event_type": type(event).__name__},
)
+ def _dispatch_serialized(self, event: Event) -> None:
+ with self._dispatch_thread_lock:
+ self._dispatch(event)
+
def _dispatch(self, event: Event) -> None:
kind = type(event).__name__
diff --git a/tests/unit/sessions/test_recorder_lifecycle.py b/tests/unit/sessions/test_recorder_lifecycle.py
index 268049584a..640f2d95da 100644
--- a/tests/unit/sessions/test_recorder_lifecycle.py
+++ b/tests/unit/sessions/test_recorder_lifecycle.py
@@ -1,5 +1,9 @@
from __future__ import annotations
+import asyncio
+import threading
+import time
+
import pytest
from jarvis.core.bus import EventBus
@@ -17,6 +21,48 @@ from jarvis.sessions.recorder import SessionRecorder
from jarvis.sessions.store import SessionStore
+class _SlowStartStore:
+ """Minimal store whose session insert models a contended SQLite write."""
+
+ def __init__(self) -> None:
+ self.started = threading.Event()
+ self.release = threading.Event()
+
+ def upsert_session(self, **_kwargs) -> None:
+ self.started.set()
+ self.release.wait(timeout=0.5)
+
+ def append_event(self, **_kwargs) -> int:
+ return 1
+
+
+async def test_slow_session_write_does_not_block_live_audio_loop() -> None:
+ """Persistence contention must not stall mic pumping or bar level frames."""
+ store = _SlowStartStore()
+ recorder = SessionRecorder(store) # type: ignore[arg-type]
+ event = VoiceSessionStarted(
+ source_layer="speech.pipeline",
+ session_id="session-slow-store",
+ wake_keyword="hey_jarvis",
+ language="en",
+ )
+
+ started_at = time.perf_counter()
+ dispatch = asyncio.create_task(recorder._on_event(event)) # noqa: SLF001
+ try:
+ # If the synchronous store runs on the event-loop thread, this sleep
+ # cannot resume until the store's 500 ms timeout. Off-loop dispatch
+ # leaves ample time for the microphone buffer and Tk level channel.
+ await asyncio.sleep(0.05)
+ elapsed = time.perf_counter() - started_at
+ assert store.started.is_set()
+ assert elapsed < 0.25, f"event loop was blocked for {elapsed:.3f}s"
+ finally:
+ store.release.set()
+ await dispatch
+
+
def _final(text: str, lang: str = "de", *, continues: bool = False) -> TranscriptFinal:
return TranscriptFinal(
source_layer="speech.stt",