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): reap session tasks so a swallowed cancel cannot deafen the mic

rubenluetke10-beep9cce24c7b6b7
diff --git a/jarvis/audio/capture.py b/jarvis/audio/capture.py
index d95a3644c9..d1440084f8 100644
--- a/jarvis/audio/capture.py
+++ b/jarvis/audio/capture.py
@@ -863,6 +863,22 @@ class MicrophoneCapture:
except RuntimeError: # The event loop closed between the guard and handoff.
self._drops += 1
+ def _wake_parked_reader(self) -> None:
+ """Wake a consumer blocked in ``stream()`` after the capture closed.
+
+ ``__aexit__`` stops the native stream, so no further frame will ever
+ arrive — and a consumer parked on ``queue.get()`` would wait for one
+ forever (BUG-185: the session's abandoned microphone pump). An empty
+ marker frame wakes it; ``stream()`` sees ``_closed`` and returns. A
+ full queue needs no marker: the reader wakes on the next queued frame.
+ """
+ try:
+ self._queue.put_nowait(
+ AudioChunk(pcm=b"", sample_rate=self._sample_rate, timestamp_ns=0)
+ )
+ except asyncio.QueueFull:
+ return
+
def _safe_put(self, chunk: AudioChunk) -> None:
"""Runs in the event loop — safe put with drop-OLDEST on full."""
# Heartbeat update for the stall watchdog. Even if the queue is full,
@@ -1228,6 +1244,7 @@ class MicrophoneCapture:
async def __aexit__(self, *exc_info) -> None:
topology.unregister_capture(self)
self._closed = True
+ self._wake_parked_reader()
watchdog = self._watchdog_task
self._watchdog_task = None
if watchdog is not None and not watchdog.done():
@@ -1266,14 +1283,22 @@ class MicrophoneCapture:
chunk = await self._queue.get()
else:
try:
- chunk = await asyncio.wait_for(
- self._queue.get(), timeout=self._ACCESS_RECHECK_S
- )
+ # ``asyncio.timeout``, not ``wait_for``: the latter can
+ # swallow a consumer's cancellation on Python 3.11 when a
+ # frame arrives in the same step (CPython gh-86296,
+ # BUG-185), and this loop runs per frame.
+ async with asyncio.timeout(self._ACCESS_RECHECK_S):
+ chunk = await self._queue.get()
except TimeoutError:
# No audio frame is still a live interval: recheck TCC so a
# revoke closes a stalled/silent stream without waiting for
# PortAudio to produce another callback.
continue
+ if self._closed:
+ # Woken by ``__aexit__``: the device is gone and so is the
+ # reason for this consumer to exist. A reader parked here on an
+ # empty queue used to sit forever after close (BUG-185).
+ return
self._require_microphone_access()
yield chunk
diff --git a/jarvis/core/cancellation.py b/jarvis/core/cancellation.py
new file mode 100644
index 0000000000..41a4d696a5
--- /dev/null
+++ b/jarvis/core/cancellation.py
@@ -0,0 +1,130 @@
+"""Cancellation that sticks — tearing down long-lived asyncio tasks.
+
+A plain ``task.cancel(); await task`` is not a teardown on this codebase's
+Python floor (3.11). Two things can leave the task running with the cancel
+counted but never delivered again:
+
+* ``asyncio.wait_for`` swallows a cancellation when the awaited future
+ completes in the same loop step the cancel lands in — it returns the result
+ instead of raising (CPython gh-86296 / bpo-42130; only the 3.12 rewrite fixed
+ it). A pump that awaits ``wait_for`` once per frame — a microphone feed at
+ thirty frames a second, under a loop that stalls for hundreds of
+ milliseconds — sits in that window most of the time.
+* On the Windows proactor loop a cancel landing while the loop has no timer
+ armed can be lost in the IOCP poll (BUG-081's general form).
+
+Both look identical from outside: ``Task.cancelling() > 0`` and the task keeps
+going. Both are repaired the same way: re-deliver the cancel until it sticks,
+inside a budget, so a task that never yields to it cannot wedge its owner.
+
+BUG-185 is the live incident: the voice session's teardown awaited its
+microphone pump unbounded after two swallowed cancels, so the pipeline never
+returned to IDLE, the dictation key refused every press with
+``handover_failed``, and only an app restart freed the microphone.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import logging
+from typing import Any
+
+log = logging.getLogger(__name__)
+
+#: How long ``cancel_and_reap`` keeps re-delivering a cancel before it gives
+#: the task up as unkillable and returns to its caller.
+DEFAULT_REAP_BUDGET_S = 3.0
+
+#: The re-delivery interval. Also the timer that guarantees the loop wakes up
+#: on Windows, where a cancel can otherwise be lost (BUG-081).
+DEFAULT_REAP_HEARTBEAT_S = 0.5
+
+
+async def cancel_and_reap(
+ task: asyncio.Task[Any] | asyncio.Future[Any],
+ *,
+ budget_s: float = DEFAULT_REAP_BUDGET_S,
+ heartbeat_s: float = DEFAULT_REAP_HEARTBEAT_S,
+ label: str | None = None,
+) -> bool:
+ """Cancel ``task`` and wait for it to actually finish — bounded.
+
+ Re-issues the cancel every ``heartbeat_s`` until the task is done or the
+ budget is spent, then consumes the outcome so a cancelled or failed task
+ never warns about an unretrieved exception. Returns ``True`` when the task
+ finished within the budget and ``False`` when it was abandoned; the caller
+ decides what an abandoned task means for it (usually: log, and rely on the
+ resource the task reads from being closed a moment later).
+
+ Never raises for the task's sake. The CALLER's own cancellation is honoured:
+ if this coroutine is cancelled while waiting, that propagates.
+ """
+ name = label or _task_name(task)
+ if task.done():
+ _consume(task)
+ return True
+ loop = asyncio.get_running_loop()
+ deadline = loop.time() + max(0.0, float(budget_s))
+ beat = max(0.01, float(heartbeat_s))
+ attempts = 0
+ while True:
+ task.cancel()
+ attempts += 1
+ remaining = deadline - loop.time()
+ done, _pending = await asyncio.wait({task}, timeout=min(beat, max(0.0, remaining)))
+ if done:
+ _consume(task)
+ if attempts > 1:
+ log.info(
+ "%s needed %d cancel deliveries before it stopped "
+ "(a swallowed cancellation, BUG-185).",
+ name,
+ attempts,
+ )
+ return True
+ if loop.time() >= deadline:
+ log.warning(
+ "%s ignored %d cancel deliveries over %.1fs — abandoning it "
+ "so the teardown can finish (BUG-185).",
+ name,
+ attempts,
+ float(budget_s),
+ )
+ # Whatever it eventually raises must not surface as "exception was
+ # never retrieved" after everyone has moved on.
+ task.add_done_callback(_consume)
+ return False
+
+
+def raise_if_cancelling() -> None:
+ """Re-honour a cancel the current task swallowed.
+
+ For long-running pumps: call once per iteration. ``Task.cancelling()`` is
+ the number of cancel requests nobody has ``uncancel()``-ed — a value above
+ zero after an await that returned normally means the cancellation was
+ eaten on the way (the ``wait_for`` race above). Raising here delivers it
+ where the runtime failed to.
+ """
+ current = asyncio.current_task()
+ if current is not None and current.cancelling() > 0:
+ raise asyncio.CancelledError()
+
+
+def _task_name(task: asyncio.Task[Any] | asyncio.Future[Any]) -> str:
+ getter = getattr(task, "get_name", None)
+ if callable(getter):
+ try:
+ return str(getter())
+ except Exception: # noqa: BLE001, S110 — a name is cosmetic
+ pass
+ return repr(task)[:120]
+
+
+def _consume(task: asyncio.Task[Any] | asyncio.Future[Any]) -> None:
+ """Retrieve a finished task's outcome without letting it raise here."""
+ if not task.done() or task.cancelled():
+ return
+ try:
+ task.exception()
+ except Exception: # noqa: BLE001, S110 — consuming, not handling
+ pass
diff --git a/jarvis/speech/pipeline.py b/jarvis/speech/pipeline.py
index 47c8f17de7..1fe00a5ab0 100644
--- a/jarvis/speech/pipeline.py
+++ b/jarvis/speech/pipeline.py
@@ -51,6 +51,7 @@ from jarvis.audio.vad_reasons import FORCED_CUT_REASONS
from jarvis.brain.output_filter import FALLBACK_PHRASES, ScrubResult, scrub_for_voice
from jarvis.brain.scrub_verdict import is_harmless_scrub_residue
from jarvis.brain.turn_planner import plan_turn
+from jarvis.core.cancellation import cancel_and_reap, raise_if_cancelling
from jarvis.core.events import (
CU_PROGRESS_EVENTS,
DICTATION_REFUSAL_REASONS,
@@ -882,6 +883,24 @@ _DICTATION_WAKE_RELEASE_TIMEOUT_S = 3.0
# was never delivered. See ``SpeechPipeline._on_dictate_press``.
_DICTATE_HOLD_REPEAT_GRACE_S = 2.0
+# Teardown of a voice session's child tasks (the microphone pump, the provider
+# wait, the hangup waiter): how long each one is re-cancelled before the
+# teardown abandons it and moves on (BUG-185). On Python 3.11 a cancel can be
+# swallowed by ``asyncio.wait_for`` (CPython gh-86296); a bare ``await task``
+# after ONE cancel then waits forever, which is exactly how the pipeline stood
+# stuck in its teardown for an entire afternoon with the microphone deaf. The
+# budget is short on purpose — the dictation handover gives a session
+# ``_DICTATION_HANDOVER_TIMEOUT_S`` to release the device — and the heartbeat
+# doubles as the timer that keeps a cancel from being lost on the Windows
+# proactor loop (BUG-081).
+_SESSION_TASK_REAP_BUDGET_S = 3.0
+_SESSION_TASK_REAP_HEARTBEAT_S = 0.5
+
+# A hangup request older than this with the session still active is not "one
+# already pending" — it is a teardown that has not completed, and the next
+# explicit stop gesture must say so instead of being ignored at debug level.
+_HANGUP_LATCH_STALE_S = 10.0
+
# The voice hold key (push-to-talk) has the same physics and the same lost
# key-up failure mode as the dictation hold; sharing the window keeps the two
# self-healing latches from drifting apart. See ``SpeechPipeline._on_ptt_press``.
@@ -7101,10 +7120,27 @@ class SpeechPipeline:
and not handoff_ready
and not candidate_active
):
- log.debug("request_hangup ignored: external hangup already pending")
- return
+ # A second stop gesture while the first is still being honoured
+ # is idempotent — unless the first one is OLD. A hangup that has
+ # been "pending" for many seconds with the session still active
+ # is a teardown that never finished (BUG-185: a swallowed cancel
+ # left the pipeline stuck in its own finally for an afternoon).
+ # That must not vanish at debug level: say it, and let the
+ # request through so the chokepoint runs again.
+ requested_at = float(getattr(self, "_hangup_requested_at", 0.0) or 0.0)
+ age = time.monotonic() - requested_at if requested_at else 0.0
+ if age < _HANGUP_LATCH_STALE_S:
+ log.debug("request_hangup ignored: external hangup already pending")
+ return
+ log.warning(
+ "request_hangup: a hangup has been pending for %.1fs and the "
+ "voice session is still active — the teardown did not finish; "
+ "delivering the stop again (BUG-185).",
+ age,
+ )
if pending is not None:
pending.set()
+ self._hangup_requested_at = time.monotonic()
log.info("📵 request_hangup — closing the voice session")
def _dispatch() -> None:
@@ -9511,6 +9547,10 @@ class SpeechPipeline:
nonlocal post_output_echo_guard_until, preroll_bytes
nonlocal tail_pending_bytes, thinking_barge_quiet_since
async for chunk in self._session_input_stream(input_chunks):
+ # ``handle_audio_frame`` bounds its provider send with a
+ # timeout; on 3.11 that can eat the teardown's cancel
+ # (BUG-185). One frame later it is honoured here.
+ raise_if_cancelling()
if not provider_ready.is_set():
if (
preroll_bytes + len(chunk.pcm)
@@ -9839,16 +9879,27 @@ class SpeechPipeline:
for task in wait_tasks:
if not task.done():
task.cancel()
+ # Re-delivered and BOUNDED, never a bare ``await task`` (BUG-185).
+ # The mic pump awaits ``wait_for`` per frame, and on Python 3.11
+ # that swallows a cancel landing in the same loop step the frame
+ # completes (CPython gh-86296) — live 2026-08-25 20:23: two
+ # cancels eaten, this finally then awaited the pump for as long
+ # as the app ran, ``_state`` never returned to IDLE, and every
+ # dictation press refused ``handover_failed`` until a restart.
+ # A pump that outlives the budget is abandoned here; the capture
+ # context below closes its source, which ends the generator.
for task in wait_tasks:
- try:
- await task
- except (asyncio.CancelledError, Exception): # noqa: S110 - teardown
- pass
+ await cancel_and_reap(
+ task,
+ budget_s=_SESSION_TASK_REAP_BUDGET_S,
+ heartbeat_s=_SESSION_TASK_REAP_HEARTBEAT_S,
+ )
if microphone_task is not None:
- try:
- await microphone_task
- except (asyncio.CancelledError, Exception): # noqa: S110 - teardown
- pass
+ await cancel_and_reap(
+ microphone_task,
+ budget_s=_SESSION_TASK_REAP_BUDGET_S,
+ heartbeat_s=_SESSION_TASK_REAP_HEARTBEAT_S,
+ )
if session is not None:
# Defense in depth around the two bounded closes inside end()
# (provider socket 5 s + turn-completed 3 s): should end() ever
@@ -10418,13 +10469,25 @@ class SpeechPipeline:
# now would leave a dictation nobody is holding a key for,
# running to the duration cap — so say what happened instead.
# The hangup itself already went through, which is exactly why
- # pressing again works.
- self._refuse_dictation(
- "handover_failed",
- "The voice conversation was still shutting down when the "
- "dictation key was released, so nothing was recorded. The "
- "microphone is free now — press the key again.",
- )
+ # pressing again works — USUALLY. The sentence checks rather
+ # than assumes: a session whose teardown is wedged still holds
+ # the device at this moment (BUG-185), and "the microphone is
+ # free now" was exactly the lie the user read ten times in a
+ # row while nothing they pressed could work.
+ if self._voice_session_holds_microphone():
+ detail = (
+ "The voice conversation is still shutting down, so "
+ "nothing was recorded. Hold the key a little longer, "
+ "or press it again once the voice bar has closed."
+ )
+ else:
+ detail = (
+ "The voice conversation was still shutting down when "
+ "the dictation key was released, so nothing was "
+ "recorded. The microphone is free now — press the key "
+ "again."
+ )
+ self._refuse_dictation("handover_failed", detail)
return
error = task.exception()
if error is not None: