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-engine): worker hung on Windows while loading Pocket TTS

rubenluetke10-beep1144cd91bf32
diff --git a/jarvis/voice_engine/client.py b/jarvis/voice_engine/client.py
index 15812ae01a..3e44f50f49 100644
--- a/jarvis/voice_engine/client.py
+++ b/jarvis/voice_engine/client.py
@@ -9,6 +9,7 @@ from __future__ import annotations
import asyncio
import contextlib
+import logging
import os
import sys
from collections.abc import Callable
@@ -17,6 +18,8 @@ from typing import Any
from jarvis.voice_engine import protocol as p
+log = logging.getLogger("jarvis.voice_engine.client")
+
# Same flag as jarvis.core.process_utils.NO_WINDOW_CREATIONFLAGS (AP-1).
_NO_WINDOW = 0x08000000 if sys.platform == "win32" else 0
EXITED = "_exited"
@@ -31,6 +34,10 @@ def worker_env(*, package_root: Path | None = None, home: Path | None = None,
"PYTHONUNBUFFERED": "1",
"HF_HUB_DISABLE_SYMLINKS_WARNING": "1",
"HF_HUB_DISABLE_PROGRESS_BARS": "1",
+ # After setup the engine never touches the network (plan R8). Pocket TTS
+ # otherwise probes a gated repository on every load and, offline, sat in
+ # retries before falling back to the cached weights.
+ "HF_HUB_OFFLINE": "1",
})
if package_root is not None:
env["PYTHONPATH"] = str(package_root)
@@ -83,6 +90,12 @@ class EngineClient:
self.audio_frames.put_nowait(frame)
else:
self.messages.put_nowait(frame)
+ except p.ProtocolError as exc:
+ # A corrupt stream cannot be resynchronised; stop the worker so the
+ # caller sees an exit instead of waiting on a silent pipe.
+ log.error("voice engine sent an invalid frame: %s", exc)
+ if self._process is not None and self._process.returncode is None:
+ self._process.kill()
finally:
self.messages.put_nowait({"type": EXITED})
diff --git a/jarvis/voice_engine/stt.py b/jarvis/voice_engine/stt.py
index 9c663ef48b..98592f2604 100644
--- a/jarvis/voice_engine/stt.py
+++ b/jarvis/voice_engine/stt.py
@@ -17,7 +17,10 @@ from jarvis.voice_engine.audio import STT_RATE
def default_threads() -> int:
- return max(1, min(4, (os.cpu_count() or 2) // 2))
+ """All cores on small machines (measured: half of 4 vCPUs left Piper slower
+ than Pocket in the Linux container), half of them, at most 4, elsewhere."""
+ cores = os.cpu_count() or 2
+ return cores if cores <= 4 else min(4, cores // 2)
class ParakeetStt:
diff --git a/jarvis/voice_engine/tts/piper.py b/jarvis/voice_engine/tts/piper.py
index 6deb97921d..0cebe0e490 100644
--- a/jarvis/voice_engine/tts/piper.py
+++ b/jarvis/voice_engine/tts/piper.py
@@ -6,7 +6,6 @@ voice files come from Piper.
from __future__ import annotations
-import os
import threading
from collections.abc import Iterator
from pathlib import Path
@@ -14,6 +13,7 @@ from pathlib import Path
import numpy as np
from jarvis.voice_engine.models import is_present, model_path
+from jarvis.voice_engine.stt import default_threads
VOICES = {"de": "piper-de-thorsten-medium", "en": "piper-en-ryan-medium"}
@@ -32,7 +32,7 @@ class PiperTts:
tokens=str(voice_dir / "tokens.txt"),
data_dir=str(voice_dir / "espeak-ng-data"),
),
- num_threads=threads or max(1, min(4, (os.cpu_count() or 2) // 2)),
+ num_threads=threads or default_threads(),
provider="cpu",
),
)
diff --git a/jarvis/voice_engine/worker.py b/jarvis/voice_engine/worker.py
index b66bd16b64..60139ddbbb 100644
--- a/jarvis/voice_engine/worker.py
+++ b/jarvis/voice_engine/worker.py
@@ -14,6 +14,8 @@ import os
import queue
import sys
import threading
+import time
+from collections.abc import Callable
from typing import Any, BinaryIO
from jarvis.voice_engine import ENGINE_VERSION
@@ -43,11 +45,10 @@ class StdioTransport:
def _read(self, loop: asyncio.AbstractEventLoop, inbox: asyncio.Queue[Any]) -> None:
reader = p.FrameReader()
+ read = _windows_pipe_reader(self._source) or self._blocking_read
try:
while True:
- data = self._source.read1(65536) if hasattr(self._source, "read1") else (
- self._source.read(65536)
- )
+ data = read()
if not data:
break
for frame in reader.feed(data):
@@ -57,6 +58,11 @@ class StdioTransport:
finally:
loop.call_soon_threadsafe(inbox.put_nowait, None)
+ def _blocking_read(self) -> bytes:
+ if hasattr(self._source, "read1"):
+ return self._source.read1(65536)
+ return self._source.read(65536)
+
def _write(self) -> None:
while True:
data = self._outbox.get()
@@ -76,6 +82,45 @@ class StdioTransport:
self._outbox.put(None)
+def _windows_pipe_reader(source: BinaryIO) -> Callable[[], bytes] | None:
+ """A reader that never leaves a blocking read pending on a Windows pipe.
+
+ While one thread sits in a synchronous ReadFile on the inherited stdin
+ pipe, Windows blocks other threads that touch the standard handles while
+ loading a DLL; importing scipy's BLAS for Pocket TTS hung the worker for
+ good. Peeking first and reading only what is already there avoids it, at
+ the cost of a 5 ms poll while idle.
+ """
+ if sys.platform != "win32":
+ return None
+ import ctypes # noqa: PLC0415 - Windows only
+ import msvcrt # noqa: PLC0415 - Windows only
+ from ctypes import wintypes # noqa: PLC0415
+
+ try:
+ handle = msvcrt.get_osfhandle(source.fileno())
+ except (OSError, ValueError, AttributeError):
+ return None
+ kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
+ peek = kernel32.PeekNamedPipe
+ peek.argtypes = [wintypes.HANDLE, ctypes.c_void_p, wintypes.DWORD, ctypes.c_void_p,
+ ctypes.POINTER(wintypes.DWORD), ctypes.c_void_p]
+ peek.restype = wintypes.BOOL
+ available = wintypes.DWORD(0)
+ if not peek(handle, None, 0, None, ctypes.byref(available), None):
+ return None # not a pipe (a file or console): plain reads are safe
+
+ def read() -> bytes:
+ while True:
+ if not peek(handle, None, 0, None, ctypes.byref(available), None):
+ return b"" # the writer closed the pipe: end of input
+ if available.value:
+ return os.read(source.fileno(), min(available.value, 65536))
+ time.sleep(0.005)
+
+ return read
+
+
class _Emitter:
def __init__(self, transport: StdioTransport) -> None:
self._transport = transport
@@ -243,9 +288,24 @@ def main() -> int:
if not sys.flags.utf8_mode:
log.warning("Python UTF-8 mode is off; start the worker with PYTHONUTF8=1 "
"(some voice packages read their configs in the locale encoding)")
- transport = StdioTransport(sys.stdin.buffer, sys.stdout.buffer)
+ transport = StdioTransport(sys.stdin.buffer, _private_stdout())
return asyncio.run(Worker(transport).run())
+def _private_stdout() -> BinaryIO:
+ """Keep the protocol pipe to ourselves and send every other write to stderr.
+
+ Voice packages print to stdout (Pocket TTS does while loading); one stray
+ line in the frame stream breaks the protocol, so the pipe is duplicated
+ for the transport and file descriptor 1 (and ``sys.stdout``) now point at
+ stderr, which also covers native code writing to fd 1.
+ """
+ sys.stdout.flush()
+ pipe = os.fdopen(os.dup(sys.stdout.fileno()), "wb", buffering=0)
+ os.dup2(sys.stderr.fileno(), sys.stdout.fileno())
+ sys.stdout = sys.stderr
+ return pipe
+
+
if __name__ == "__main__":
raise SystemExit(main())