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(wake): serialize libvosk native calls (BUG-151)

Harald0ba1d9423985
diff --git a/jarvis/plugins/wake/vosk_kws_provider.py b/jarvis/plugins/wake/vosk_kws_provider.py
index 9dddaadbf4..361709dbf9 100644
--- a/jarvis/plugins/wake/vosk_kws_provider.py
+++ b/jarvis/plugins/wake/vosk_kws_provider.py
@@ -82,19 +82,15 @@ CPU shows it ~0.5 s after:
loaded for verify/rescue and return on the next ``detect()`` session. The
primary model is never demoted.
-**Native concurrency limit (found 2026-08-19, PRE-EXISTING, open).** libvosk
-0.3.44 is safe for many recognizers DECODING concurrently on one Model, but
-under a high rate of concurrent recognizer construction + short decode +
-``FinalResult`` it crashes the process with an access violation (Windows,
-reproduced in ~5-50 s with five threads; the real-model bench
-``scripts/vosk_wake_bench.py`` hit it 3/3 at HEAD before this change and
-2/4 after). No partial lock protocol tried (finals only; finals exclusive
-vs decodes; finals + constructions exclusive) was reliably enough; full
-serialisation of every native call survives. This module therefore adds no
-new concurrent decode-and-finalise cycle (the streamed confirm decodes in
-parallel but finalises sequentially on one thread) and keeps the rest of
-the pre-existing design; the fix proper is tracked in docs/BUGS.md
-(BUG-151).
+**Native concurrency (BUG-151, fixed 2026-08-20).** libvosk 0.3.44 is safe
+for many recognizers DECODING concurrently on one Model, but concurrent
+construction + short decode + ``FinalResult`` access-violates the process.
+Every native constructor and method now goes through
+``jarvis.plugins.wake.vosk_native`` (one process-wide lock). Python-level
+grammar/free submit still runs on two threads so the 2026-07-10 spawn-latency
+structure stays; the lock serialises the native calls themselves. Test
+doubles are not wrapped, so the concurrent-barrier unit test still proves
+the submit shape.
"""
from __future__ import annotations
@@ -113,6 +109,7 @@ from typing import Any
import numpy as np
from jarvis.core.protocols import AudioChunk
+from jarvis.plugins.wake.vosk_native import build_recognizer, native_call
from jarvis.speech.wake_constants import (
WAKE_PREFIXES,
normalize_phrase_for_match,
@@ -643,19 +640,18 @@ class _StreamingVerify:
internally, so feeding the ring and then 32 ms chunks decodes exactly what
one 3.6 s buffer would.)
- Threading, chosen against the libvosk limits measured 2026-08-19 (module
- note, BUG-151): FEEDS run on one single-worker executor per recognizer —
- ordered, never overlapping on one native recognizer (AP-24), and the two
- recognizers decode concurrently, which is the proven-safe pattern (many
- recognizers decoding at once on one Model). The two FINALS run
- sequentially on the finishing thread after every feed has landed — the
- concurrent short-decode-and-finalise cycle is the one that crashes that
- build, so this class never adds one. The window and the normalised pcm are
- accumulated here so the judgement sees exactly what the recognizers
- decoded. Peak normalisation uses the gain measured over the ring at
- partial time; the tail keeps that gain (clipped), which is the one place
- this differs from the from-scratch path — the phrase itself, the part the
- re-score and the span RMS judge, is the ring.
+ Threading (BUG-151): FEEDS run on one single-worker executor per
+ recognizer — ordered, never overlapping on one native recognizer (AP-24).
+ The two executors still submit in parallel (the 2026-07-10 spawn-latency
+ shape); ``vosk_native.LockedRecognizer`` serialises the native calls so
+ this is not the crashy concurrent short-decode-and-finalise cycle. The two
+ FINALS still run sequentially on the finishing thread after every feed has
+ landed. The window and the normalised pcm are accumulated here so the
+ judgement sees exactly what the recognizers decoded. Peak normalisation
+ uses the gain measured over the ring at partial time; the tail keeps that
+ gain (clipped), which is the one place this differs from the from-scratch
+ path — the phrase itself, the part the re-score and the span RMS judge, is
+ the ring.
"""
def __init__(
@@ -935,12 +931,10 @@ class VoskKwsProvider:
return model
def _new_grammar_rec(self, path: str | None = None) -> Any:
- from vosk import KaldiRecognizer
-
grammar = json.dumps([self._phrase.lower(), "[unk]"])
- rec = KaldiRecognizer(self._ensure_model(path), self._sample_rate, grammar)
- rec.SetWords(True)
- return rec
+ return build_recognizer(
+ self._ensure_model(path), self._sample_rate, grammar
+ )
def _build_verify_rec(self, path: str | None, kind: str, *, prewarm: bool) -> Any:
"""A fresh one-shot verify recognizer; optionally silence-prewarmed.
@@ -948,24 +942,22 @@ class VoskKwsProvider:
The prewarm decodes 0.3s of silence and Resets — it pre-pays Kaldi's
lazy first-decode init without touching decisions (parity-measured).
A prewarm error degrades to the cold recognizer, never fails a verify.
+ Construction and every native method go through ``vosk_native``
+ (BUG-151).
"""
- from vosk import KaldiRecognizer
-
if kind == "grammar":
rec = self._new_grammar_rec(path)
elif kind == _COMPETITION_KIND:
if self._competition_grammar is None: # unprefixed phrase
rec = self._new_grammar_rec(path)
else:
- rec = KaldiRecognizer(
+ rec = build_recognizer(
self._ensure_model(path),
self._sample_rate,
self._competition_grammar,
)
- rec.SetWords(True)
else:
- rec = KaldiRecognizer(self._ensure_model(path), self._sample_rate)
- rec.SetWords(True)
+ rec = build_recognizer(self._ensure_model(path), self._sample_rate)
if prewarm:
try:
silence = np.zeros(
@@ -1474,14 +1466,12 @@ class VoskKwsProvider:
model (data/wake_models/vosk/de/vosk-model-small-de-0.15): the free
pass costs 3-5x the grammar pass (e.g. 235ms vs 70ms over a 3 s
window), so running them sequentially pays their SUM even though the
- wall-clock floor is only their MAX. They run concurrently in two
- worker threads against ONE shared, read-only ``Model`` — Vosk's
- documented multi-client pattern (one Model, many independent
- KaldiRecognizer sessions decoding concurrently), not the AP-24 hazard
- (that guards a single recognizer's mutable per-call state shared
- across concurrent callers; here each thread owns its own fresh
- recognizer). This changes only wall-clock time, never the decision:
- both passes decode the identical ``pcm`` and every downstream
+ wall-clock floor is only their MAX. They submit concurrently in two
+ worker threads against ONE shared, read-only ``Model``. Each thread
+ owns its own fresh recognizer (AP-24). Native calls serialise on the
+ BUG-151 lock so this is not the crashy concurrent
+ short-decode-and-finalise cycle. Decision is unchanged: both passes
+ decode the identical ``pcm`` and every downstream
threshold/comparison is untouched.
Two entry shapes share the judgement below (``_judge_decoded``): this
@@ -2201,7 +2191,12 @@ def vosk_model_supports_phrase(model_path: str, phrase: str) -> bool:
tmp = tempfile.TemporaryFile(mode="w+")
old = os.dup(2)
os.dup2(tmp.fileno(), 2)
- KaldiRecognizer(Model(model_path), 16_000, json.dumps([phrase.lower(), "[unk]"]))
+ native_call(
+ KaldiRecognizer,
+ Model(model_path),
+ 16_000,
+ json.dumps([phrase.lower(), "[unk]"]),
+ )
except Exception: # noqa: BLE001 — probe failure must not reject a real word
return True
finally:
diff --git a/jarvis/plugins/wake/vosk_native.py b/jarvis/plugins/wake/vosk_native.py
new file mode 100644
index 0000000000..78fc43f4f2
--- /dev/null
+++ b/jarvis/plugins/wake/vosk_native.py
@@ -0,0 +1,86 @@
+"""Serialize every libvosk native call (BUG-151).
+
+libvosk 0.3.44 is safe for many long-lived recognizers decoding at once on
+one ``Model``. It is not safe under concurrent short-lived
+``KaldiRecognizer`` construction + ``AcceptWaveform`` + ``FinalResult`` —
+that pattern access-violates the process (Windows, 5-50 s in
+``scripts/vosk_native_stress.py churn``). Partial locks (finals only,
+finals exclusive vs decodes, finals + constructions exclusive) were not
+reliably enough. One process-wide lock around every native call survived.
+
+Fakes in unit tests are not native and must stay unlocked: the concurrent
+grammar/free barrier test deadlocks if a lock serialises two
+``FinalResult`` waits. Only real ``vosk.KaldiRecognizer`` instances are
+wrapped.
+"""
+
+from __future__ import annotations
+
+import threading
+from typing import Any
+
+_LOCK = threading.Lock()
+
+
+def native_call(fn: Any, *args: Any, **kwargs: Any) -> Any:
+ """Run one libvosk constructor or method behind the process-wide lock."""
+ with _LOCK:
+ return fn(*args, **kwargs)
+
+
+def is_native_recognizer(rec: Any) -> bool:
+ """True for a real ``vosk.KaldiRecognizer``, false for test doubles."""
+ cls = type(rec)
+ return cls.__name__ == "KaldiRecognizer" and cls.__module__ == "vosk"
+
+
+class LockedRecognizer:
+ """Proxy that holds the process lock for every method on a recognizer."""
+
+ def __init__(self, rec: Any) -> None:
+ self._rec = rec
+
+ def AcceptWaveform(self, data: Any) -> Any: # noqa: N802 — vosk API
+ with _LOCK:
+ return self._rec.AcceptWaveform(data)
+
+ def PartialResult(self) -> Any: # noqa: N802 — vosk API
+ with _LOCK:
+ return self._rec.PartialResult()
+
+ def Result(self) -> Any: # noqa: N802 — vosk API
+ with _LOCK:
+ return self._rec.Result()
+
+ def FinalResult(self) -> Any: # noqa: N802 — vosk API
+ with _LOCK:
+ return self._rec.FinalResult()
+
+ def Reset(self) -> Any: # noqa: N802 — vosk API
+ with _LOCK:
+ return self._rec.Reset()
+
+ def SetWords(self, flag: Any) -> Any: # noqa: N802 — vosk API
+ with _LOCK:
+ return self._rec.SetWords(flag)
+
+
+def wrap_recognizer(rec: Any) -> Any:
+ """Lock a native recognizer; leave test doubles untouched."""
+ if rec is None or not is_native_recognizer(rec):
+ return rec
+ return LockedRecognizer(rec)
+
+
+def build_recognizer(model: Any, sample_rate: int, grammar: str | None = None) -> Any:
+ """Construct a ``KaldiRecognizer``, ``SetWords(True)``, wrap if native."""
+ from vosk import KaldiRecognizer
+
+ with _LOCK:
+ rec = (
+ KaldiRecognizer(model, sample_rate, grammar)
+ if grammar is not None
+ else KaldiRecognizer(model, sample_rate)
+ )
+ rec.SetWords(True)
+ return wrap_recognizer(rec)
diff --git a/tests/unit/plugins/wake/test_vosk_native.py b/tests/unit/plugins/wake/test_vosk_native.py
new file mode 100644
index 0000000000..aad7604d38
--- /dev/null
+++ b/tests/unit/plugins/wake/test_vosk_native.py
@@ -0,0 +1,107 @@
+"""BUG-151: libvosk native calls serialise; test doubles stay unlocked."""
+
+from __future__ import annotations
+
+import threading
+import time
+
+from jarvis.plugins.wake.vosk_native import (
+ LockedRecognizer,
+ build_recognizer,
+ is_native_recognizer,
+ wrap_recognizer,
+)
+
+
+class _Contended:
+ """A stand-in whose methods overlap unless a lock serialises them."""
+
+ def __init__(self) -> None:
+ self.overlaps = 0
+ self._in = 0
+ self._guard = threading.Lock()
+
+ def _enter(self) -> None:
+ with self._guard:
+ if self._in:
+ self.overlaps += 1
+ self._in += 1
+ time.sleep(0.04)
+ with self._guard:
+ self._in -= 1
+
+ def AcceptWaveform(self, _data: bytes) -> bool: # noqa: N802
+ self._enter()
+ return False
+
+ def FinalResult(self) -> str: # noqa: N802
+ self._enter()
+ return "{}"
+
+ def SetWords(self, _flag: bool) -> None: # noqa: N802
+ return None
+
+
+def test_locked_recognizer_serializes_overlapping_native_calls() -> None:
+ rec = LockedRecognizer(_Contended())
+ errors: list[BaseException] = []
+
+ def _feed() -> None:
+ try:
+ rec.AcceptWaveform(b"\x00\x00")
+ rec.FinalResult()
+ except BaseException as exc: # noqa: BLE001 — collect for the assertion
+ errors.append(exc)
+
+ threads = [threading.Thread(target=_feed) for _ in range(4)]
+ for thread in threads:
+ thread.start()
+ for thread in threads:
+ thread.join(timeout=2.0)
+ assert not thread.is_alive()
+ assert errors == []
+ assert rec._rec.overlaps == 0 # noqa: SLF001 — the inner counter is the proof
+
+
+def test_wrap_recognizer_leaves_test_doubles_unlocked() -> None:
+ inner = _Contended()
+ assert is_native_recognizer(inner) is False
+ assert wrap_recognizer(inner) is inner
+
+
+def test_wrap_recognizer_locks_a_vosk_named_class() -> None:
+ class KaldiRecognizer(_Contended):
+ pass
+
+ KaldiRecognizer.__module__ = "vosk"
+ inner = KaldiRecognizer()
+ wrapped = wrap_recognizer(inner)
+ assert isinstance(wrapped, LockedRecognizer)
+ assert wrapped._rec is inner # noqa: SLF001
+
+
+def test_build_recognizer_uses_the_patched_vosk_factory(monkeypatch) -> None:
+ built: list[object] = []
+
+ class _FakeRec:
+ def __init__(self, model, rate, grammar=None): # noqa: ANN001
+ self.model = model
+ self.rate = rate
+ self.grammar = grammar
+ self.words = False
+ built.append(self)
+
+ def SetWords(self, flag): # noqa: ANN001, N802
+ self.words = flag
+
+ import sys
+ import types
+
+ mod = types.ModuleType("vosk")
+ mod.KaldiRecognizer = _FakeRec
+ monkeypatch.setitem(sys.modules, "vosk", mod)
+
+ rec = build_recognizer("model", 16_000, '["hey nova","[unk]"]')
+ assert rec is built[0]
+ assert rec.words is True
+ assert rec.grammar == '["hey nova","[unk]"]'