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(jarvisbar): harden delayed level delivery against the barge-in race

Personal Jarvis Maintainerbd2237a71e18
diff --git a/jarvis/audio/level_tap.py b/jarvis/audio/level_tap.py
index 0b6337cf1f..bdfb89bc8e 100644
--- a/jarvis/audio/level_tap.py
+++ b/jarvis/audio/level_tap.py
@@ -53,25 +53,43 @@ _norm = LevelNormalizer()
# sorted; a mid-stream latency change can misorder neighbors by a few ms, which
# is visually irrelevant. The dispatcher thread is created lazily on the first
# delayed feed and then parks on the condition while idle.
+#
+# Cancellation ordering: ``reset_playing`` bumps ``_pending_gen`` (killing every
+# queued item) and publishes its zero under ``_deliver_lock`` — the same lock
+# the dispatcher publishes under after re-checking the generation. Whatever the
+# interleaving, no cancelled level can land AFTER the barge-in zero: either the
+# in-flight item publishes first and the zero overwrites it, or the generation
+# re-check drops it.
_pending: deque[tuple[float, float]] = deque()
_pending_cond = threading.Condition(_lock)
+_pending_gen = 0
+_deliver_lock = threading.Lock()
_dispatcher: threading.Thread | None = None
def _dispatch_loop() -> None:
while True:
- with _pending_cond:
- while not _pending:
- _pending_cond.wait()
- due, level = _pending[0]
- wait_s = due - time.monotonic()
- if wait_s > 0.0:
- # Re-check after the timed wait: reset_playing() may have
- # cleared the queue, or an earlier-due item may have arrived.
- _pending_cond.wait(timeout=wait_s)
- continue
- _pending.popleft()
- publish(level)
+ try:
+ with _pending_cond:
+ while not _pending:
+ _pending_cond.wait()
+ due, level = _pending[0]
+ wait_s = due - time.monotonic()
+ if wait_s > 0.0:
+ # Re-check after the timed wait: reset_playing() may have
+ # cleared the queue, or an earlier-due item may have
+ # arrived.
+ _pending_cond.wait(timeout=wait_s)
+ continue
+ _pending.popleft()
+ gen = _pending_gen
+ with _deliver_lock:
+ with _lock:
+ cancelled = gen != _pending_gen
+ if not cancelled:
+ publish(level)
+ except Exception: # noqa: BLE001 — the dispatcher must never die silently
+ _log.exception("level_tap dispatcher iteration failed")
def _schedule(level: float, delay_s: float) -> None:
@@ -161,24 +179,28 @@ def playback_active() -> bool:
def reset_playing() -> None:
"""Clear the playback window (barge-in / stop): audio was aborted, so the
UI must not keep showing the speaking equalizer for the cancelled tail.
- Pending delayed levels belong to that cancelled tail too — drop them and
- push an honest zero so the bars collapse with the sound."""
- global _audible_until
+ Pending delayed levels belong to that cancelled tail too — drop them
+ (generation bump kills any in-flight item, see the module ordering note)
+ and push an honest zero so the bars collapse with the sound. The zero is
+ unconditional: even with nothing queued, the last DELIVERED level may be
+ nonzero and would otherwise linger until the renderer's staleness clamp."""
+ global _audible_until, _pending_gen
_audible_until = 0.0
with _pending_cond:
- had_pending = bool(_pending)
_pending.clear()
+ _pending_gen += 1
_pending_cond.notify()
- if had_pending:
+ with _deliver_lock:
publish(0.0)
def reset() -> None:
"""Test helper: drop all subscribers and reset the normalizer."""
- global _audible_until
+ global _audible_until, _pending_gen
with _lock:
_subscribers.clear()
_pending.clear()
+ _pending_gen += 1
_pending_cond.notify()
_norm.reset()
_audible_until = 0.0
diff --git a/jarvis/audio/player.py b/jarvis/audio/player.py
index e28c6cda61..51433faed2 100644
--- a/jarvis/audio/player.py
+++ b/jarvis/audio/player.py
@@ -826,6 +826,10 @@ class AudioPlayer:
# Bluetooth). Deliver each block's level with that delay so the bar
# moves WITH the heard voice — including the final ~latency of every
# sentence, which previously played with the bars already collapsed.
+ # The uniform shift preserves the ~60 ms inter-sample cadence, so the
+ # overlay's audible-hold window is latency-independent. Read ONCE per
+ # buffer: the property takes the stream-state lock stop() also uses —
+ # bounded, benign contention, deliberately not per sub-block.
level_delay_s = self.output_latency_s if feed_level else 0.0
# Master output volume: scale the whole buffer once via the shared gain
# helper (makeup boost + soft limiter above unity, plain attenuation
diff --git a/tests/unit/audio/test_level_tap.py b/tests/unit/audio/test_level_tap.py
index b40f9717e3..afe632e1b5 100644
--- a/tests/unit/audio/test_level_tap.py
+++ b/tests/unit/audio/test_level_tap.py
@@ -135,6 +135,40 @@ def test_reset_playing_drops_pending_levels_and_zeroes_the_bars():
level_tap.reset()
+def test_reset_playing_zeroes_even_with_nothing_pending():
+ # The zero must be unconditional: with an empty queue the last DELIVERED
+ # level may still be nonzero on the sink, and a barge-in has to collapse
+ # the bars immediately — not after the renderer's staleness clamp.
+ level_tap.reset()
+ got: list[float] = []
+ level_tap.subscribe(got.append)
+ try:
+ level_tap.publish(0.6) # synchronously delivered, nothing queued
+ level_tap.reset_playing()
+ assert got == [0.6, 0.0]
+ finally:
+ level_tap.reset()
+
+
+def test_no_cancelled_level_ever_lands_after_the_bargein_zero():
+ # Ordering contract of the generation guard + delivery lock: once
+ # reset_playing() returns, its zero is the LAST word — a level scheduled
+ # before the barge-in may at worst land before it, never after.
+ level_tap.reset()
+ got: list[float] = []
+ level_tap.subscribe(got.append)
+ try:
+ for k in range(5):
+ level_tap.publish(0.5 + k * 0.05, delay_s=0.02 + k * 0.02)
+ level_tap.reset_playing()
+ zero_at = len(got) - 1
+ assert got[zero_at] == 0.0
+ time.sleep(0.3) # give any (buggy) survivor time to fire
+ assert got[zero_at + 1:] == [], "cancelled level delivered after the zero"
+ finally:
+ level_tap.reset()
+
+
def test_zero_delay_publish_stays_synchronous():
level_tap.reset()
got: list[float] = []