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): bound ctranslate2 OpenMP pool to harden the CPU stt_match path

Ruben Lütke1129223b4708
diff --git a/jarvis/plugins/stt/__init__.py b/jarvis/plugins/stt/__init__.py
index 5dc6abf082..61f9e7ef51 100644
--- a/jarvis/plugins/stt/__init__.py
+++ b/jarvis/plugins/stt/__init__.py
@@ -570,7 +570,14 @@ def build_wake_whisper(
``FasterWhisperProvider.__init__`` does not apply to them. A blank phrase is
treated as no bias.
"""
- from jarvis.plugins.stt.fwhisper import FasterWhisperProvider
+ from jarvis.plugins.stt.fwhisper import FasterWhisperProvider, _bound_ct2_threads
+
+ # Bound the ctranslate2/OpenMP CPU thread pool at the environment level
+ # BEFORE the wake FasterWhisperProvider is constructed, so it is set
+ # ahead of ctranslate2's first import on this path (AP-24/AP-25/BUG-036,
+ # defensive only — see _bound_ct2_threads docstring). Never clobbers an
+ # explicit user setting.
+ _bound_ct2_threads(default=2)
model = getattr(stt_cfg, "wake_model", "base")
device = getattr(stt_cfg, "wake_device", "cpu")
diff --git a/jarvis/plugins/stt/fwhisper.py b/jarvis/plugins/stt/fwhisper.py
index aa4ccd9a5d..ab53aec40a 100644
--- a/jarvis/plugins/stt/fwhisper.py
+++ b/jarvis/plugins/stt/fwhisper.py
@@ -70,6 +70,34 @@ def inference_only_import_shield() -> Iterator[None]:
sys.modules.pop(name, None)
+def _bound_ct2_threads(default: int = 2) -> None:
+ """Bound the ctranslate2/OpenMP CPU thread pool BEFORE ctranslate2 is imported.
+
+ Defensive hardening for the CPU stt_match wake path (AP-24/AP-25/BUG-036):
+ ctranslate2's auto thread-pool can deadlock against another OpenMP consumer
+ sharing the process (see ``_new_whisper_model`` / ``cpu_threads``). Setting
+ ``OMP_NUM_THREADS`` and ``CT2_FORCE_CPU_THREADS`` caps that pool at the
+ environment level, one layer below the per-instance ``cpu_threads=2``
+ constructor pin already in place.
+
+ Uses ``os.environ.setdefault`` so an operator's own explicit setting is
+ NEVER clobbered. This is DEFENSIVE ONLY: it does not claim to cure the
+ constellation-specific ctranslate2<->OpenMP deadlock documented in AP-25
+ — it only narrows the CPU thread-pool's blast radius. The real fix is the
+ vosk_kws engine bypassing this ctranslate2 path entirely.
+
+ Must be called before ctranslate2's first import on a given path (its
+ thread pool reads these env vars at that point), and NEVER at module
+ import time here — that would also throttle the utterance STT model,
+ which intentionally keeps auto threads (see ``FasterWhisperProvider``'s
+ default ``cpu_threads=0``).
+ """
+ import os
+
+ for var in ("OMP_NUM_THREADS", "CT2_FORCE_CPU_THREADS"):
+ os.environ.setdefault(var, str(default))
+
+
def _normalize_model_name(model: str) -> str:
"""Map known-invalid OpenAI-style aliases to faster-whisper model ids.
diff --git a/tests/unit/stt/test_ct2_thread_env.py b/tests/unit/stt/test_ct2_thread_env.py
new file mode 100644
index 0000000000..3f9ba44be0
--- /dev/null
+++ b/tests/unit/stt/test_ct2_thread_env.py
@@ -0,0 +1,34 @@
+"""Defensive hardening for the CPU stt_match wake path (AP-24/AP-25/BUG-036):
+``_bound_ct2_threads`` caps the ctranslate2/OpenMP CPU thread pool via
+environment variables, without ever clobbering an explicit user setting.
+
+This is defensive only — it does not claim to cure the constellation-specific
+ctranslate2<->OpenMP deadlock (AP-25); the real fix is the vosk_kws engine
+bypassing this path entirely.
+"""
+
+import os
+
+
+def test_bound_ct2_threads_sets_env_when_unset(monkeypatch):
+ monkeypatch.delenv("OMP_NUM_THREADS", raising=False)
+ monkeypatch.delenv("CT2_FORCE_CPU_THREADS", raising=False)
+
+ from jarvis.plugins.stt.fwhisper import _bound_ct2_threads
+
+ _bound_ct2_threads(default=2)
+
+ assert os.environ["OMP_NUM_THREADS"] == "2"
+ assert os.environ["CT2_FORCE_CPU_THREADS"] == "2"
+
+
+def test_bound_ct2_threads_respects_user_override(monkeypatch):
+ monkeypatch.setenv("OMP_NUM_THREADS", "8")
+ monkeypatch.delenv("CT2_FORCE_CPU_THREADS", raising=False)
+
+ from jarvis.plugins.stt.fwhisper import _bound_ct2_threads
+
+ _bound_ct2_threads(default=2)
+
+ assert os.environ["OMP_NUM_THREADS"] == "8" # never clobber an explicit setting
+ assert os.environ["CT2_FORCE_CPU_THREADS"] == "2"