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(realtime): re-cancel the pump on a bounded wait — end() hung forever when its one cancel was lost (BUG-081)

Ruben Lütke1f928b2a0988
diff --git a/docs/BUGS.md b/docs/BUGS.md
index 82482b666c..ce19c21d28 100644
--- a/docs/BUGS.md
+++ b/docs/BUGS.md
@@ -5623,3 +5623,38 @@ unboundedly: bound every hold by the moment the withheld content would
reach the user anyway (here: the finalize() flush), and let the kill
switch — not the hold — be the actual safety mechanism. An unbounded hold
converts a provider lag into a user-facing outage.
+
+---
+
+## BUG-081: Hanging up a realtime call can hang forever — end() loses its one pump cancel to an asyncio race after a transport rebuild (HIGH, FIXED 2026-07-18)
+
+**Symptom.** The full unit suite froze at ~61 % on
+`test_idle_stream_end_with_capability_rebuilds_instead_of_ending` — the test
+(and the same code path live) hung in `RealtimeVoiceSession.end()`
+indefinitely; per-test timeouts killed the whole run. Live equivalent: after
+a provider transport rebuild (BUG-071 path), hanging up the call never
+completes.
+
+**Root cause.** `end()` cancelled the pump task exactly once and then
+awaited it unbounded. When that single `cancel()` lands while the pump's
+current waiter future is ALREADY finished (observed: `end()` arriving just
+as `_rebuild_transport`'s `_open()` completed — waiter showed
+`<Future finished>`), asyncio absorbs the cancellation without ever raising
+`CancelledError` inside the coroutine: the task reports `cancelling()=1`,
+`_must_cancel` resets, and the pump keeps waiting on the next provider
+event. The bare `await self._pump_task` then waits forever. A second
+cancel — e.g. the loop teardown — was proven to deliver fine, which
+confirmed delivery, not handling, was the failure.
+
+**Fix (2026-07-18).** `end()` re-cancels on a bounded wait (up to 3 ×
+`cancel()` + 2 s `asyncio.wait`), logging and abandoning the task only if it
+survives all retries; a retry hits the task in a plain suspended await,
+where delivery is reliable. Non-cancelled outcomes have their exception
+retrieved so nothing is silently lost. Guard: the previously hanging
+rebuild tests in `tests/unit/realtime/test_session.py` (133 green, 10 s).
+
+**Class rule.** Never pair a single `task.cancel()` with an UNBOUNDED
+`await task` in teardown: cancellation delivery is not guaranteed on the
+first attempt when the target's waiter future has already completed.
+Teardown must re-cancel on a bounded wait (or otherwise wake the awaited
+resource) so a lost cancel degrades to a retry, never a permanent hang.
diff --git a/jarvis/realtime/session.py b/jarvis/realtime/session.py
index c72988d5d3..ad82a04efb 100644
--- a/jarvis/realtime/session.py
+++ b/jarvis/realtime/session.py
@@ -3970,12 +3970,36 @@ class RealtimeVoiceSession:
safe_preview(lost.text, max_chars=200),
)
self._late_delegate_results.clear()
- if self._pump_task is not None and not self._pump_task.done():
- self._pump_task.cancel()
- try:
- await self._pump_task
- except asyncio.CancelledError:
- pass
+ pump = self._pump_task
+ if pump is not None and not pump.done():
+ # A single cancel() can be LOST to an asyncio race (BUG-081): when
+ # cancel() lands while the pump's current waiter future is already
+ # finished — observed live with end() arriving as
+ # _rebuild_transport's _open() completed — the cancellation is
+ # absorbed without ever raising inside the coroutine. The task
+ # keeps pumping, and a bare ``await pump`` here waits forever, so
+ # the hangup itself hangs. Re-cancel on a bounded wait instead:
+ # the retry hits the task in a plain suspended await, where
+ # delivery is reliable.
+ for _ in range(3):
+ pump.cancel()
+ done, _ = await asyncio.wait({pump}, timeout=2.0)
+ if done:
+ break
+ else:
+ log.warning(
+ "realtime[%s] pump task survived repeated cancellation "
+ "during end() — abandoning it",
+ self.session_id,
+ )
+ if pump.done() and not pump.cancelled():
+ exc = pump.exception()
+ if exc is not None:
+ log.debug(
+ "realtime[%s] pump task ended with %r during end()",
+ self.session_id,
+ exc,
+ )
# A provider/socket can disappear after either side has already emitted
# transcript text but before its turn_complete marker. Freeze the
# accumulated values into VoiceTurnCompleted before the logical session