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(audio): abort stalled wake mic before recovery

Ruben Lütkeec2ce9eb8b56
diff --git a/jarvis/audio/capture.py b/jarvis/audio/capture.py
index 35350f33cd..e49b89e976 100644
--- a/jarvis/audio/capture.py
+++ b/jarvis/audio/capture.py
@@ -923,10 +923,26 @@ class MicrophoneCapture:
old_stream = self._stream
self._stream = None
if old_stream is not None:
+ # A stalled callback stream cannot drain gracefully. In
+ # particular, CoreAudio may leave ``InputStream.stop()``
+ # blocked inside PortAudio for minutes after an endpoint or
+ # power-state transition. Abort discards the dead native
+ # stream immediately, matching AudioPlayer's established
+ # device-wedge recovery contract. Every sounddevice stream
+ # exposes ``abort``; the ``stop`` fallback keeps lightweight
+ # test/third-party stream doubles compatible.
+ discard_method = "abort"
try:
- old_stream.stop()
+ abort = getattr(old_stream, "abort", None)
+ if callable(abort):
+ abort()
+ else:
+ discard_method = "stop"
+ old_stream.stop()
except Exception as exc: # noqa: BLE001
- _log.debug("Mic-Restart: stop() ignored ({}).", exc)
+ _log.debug(
+ "Mic-Restart: {}() ignored ({}).", discard_method, exc
+ )
try:
old_stream.close()
except Exception as exc: # noqa: BLE001
diff --git a/tests/unit/audio/test_capture_recovery.py b/tests/unit/audio/test_capture_recovery.py
index bcf35e7c64..e9c58ee173 100644
--- a/tests/unit/audio/test_capture_recovery.py
+++ b/tests/unit/audio/test_capture_recovery.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+import asyncio
from types import SimpleNamespace
import pytest
@@ -157,6 +158,65 @@ async def test_reopen_after_working_wake_capture_uses_physical_fallback(
]
+async def test_stall_watchdog_aborts_dead_stream_before_physical_fallback(
+ monkeypatch,
+) -> None:
+ """CoreAudio recovery must never wait for a stalled stream to drain."""
+ _install_device_enumeration(monkeypatch, "Core Audio")
+ preferred_is_poisoned = False
+ original_stream = None
+ recovered = asyncio.Event()
+ teardown_calls: list[tuple[str, int]] = []
+
+ class FakeStream:
+ def __init__(self, device: int) -> None:
+ self.device = device
+
+ def start(self) -> None:
+ if self.device == 0 and preferred_is_poisoned:
+ raise RuntimeError("Internal PortAudio error [PaErrorCode -9986]")
+ if self.device == 2:
+ recovered.set()
+
+ def abort(self) -> None:
+ teardown_calls.append(("abort", self.device))
+
+ def stop(self) -> None:
+ raise AssertionError("a stalled native input stream must not drain")
+
+ def close(self) -> None:
+ teardown_calls.append(("close", self.device))
+
+ def fake_input_stream(**kwargs):
+ nonlocal original_stream
+ stream = FakeStream(kwargs["device"])
+ if original_stream is None:
+ original_stream = stream
+ return stream
+
+ monkeypatch.setattr(capture.sd, "InputStream", fake_input_stream)
+
+ mic = capture.MicrophoneCapture(device="auto-headset", access_gate=lambda: True)
+ await mic._try_open_stream()
+ assert mic._stream is original_stream
+
+ preferred_is_poisoned = True
+ mic._STALL_THRESHOLD_S = 0.0
+ mic._WATCHDOG_TICK_S = 0.001
+ watchdog = asyncio.create_task(mic._stream_watchdog())
+ try:
+ await asyncio.wait_for(recovered.wait(), timeout=1.0)
+ await asyncio.sleep(0)
+ finally:
+ mic._closed = True
+ await asyncio.wait_for(watchdog, timeout=1.0)
+
+ assert teardown_calls[:2] == [("abort", 0), ("close", 0)]
+ assert mic._device == 2
+ assert mic._using_physical_fallback is True
+
+
@pytest.mark.asyncio
async def test_explicit_numeric_microphone_does_not_switch_hardware(
monkeypatch,