Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Keeps the model resident so each subsequent call is instant:
stackvox serve # foreground; run with `nohup stackvox serve &` to background
stackvox status # is the daemon up? also shows version + any pending PyPI update
stackvox say "Hello" # send text to the daemon (fails if not running)
stackvox stop # graceful shutdown
stackvox stop # graceful shutdown, force-killed if it acknowledges but is still up after 5s
```

stackvox checks PyPI for newer versions but only at two moments — when you run `stackvox status` and at daemon startup. The script-heavy paths (`say`, `speak`, `stackvox-say`, hooks, CI) never make a network call. To see notices on every invocation set `STACKVOX_UPDATE_NOTICE=1`. To disable the check entirely set `STACKVOX_NO_UPDATE_CHECK=1`. The check is auto-skipped when common CI env vars (`CI`, `GITHUB_ACTIONS`, etc.) are set so build logs stay clean.
Expand Down Expand Up @@ -227,6 +227,8 @@ Socket lives at `~/.cache/stackvox/daemon.sock` (override with `STACKVOX_SOCKET`

Queue depth is 2 — rapid-fire requests beyond that get `busy` rather than piling up.

If playback hangs the daemon exits rather than answering `busy` forever. Each utterance gets 60s plus half a second per character, slower than any voice reads it even at speed 0.5. Past that the daemon removes its socket and exits with status 70, so `say` reports it isn't running and anything that starts the daemon on demand gets a fresh one. On macOS this has been PortAudio deadlocked stopping a stream, or stuck writing to an output device that stopped taking audio, and `cancel` can't break either.

Before each utterance the daemon resets PortAudio so it picks up the current system default output device. Swap from speakers to Bluetooth headphones mid-session and the next `say` follows you — no daemon restart needed. The refresh costs ~10–50ms per play, which is invisible next to synthesis time.

## Requirements
Expand Down
98 changes: 93 additions & 5 deletions stackvox/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,23 @@
CLIENT_TIMEOUT_SECONDS = 1.0
PING_TIMEOUT_SECONDS = 0.5
# How long `stop` waits for the daemon process to actually exit after it has
# acknowledged the request, and how often it re-checks.
# acknowledged the request, and how often it re-checks. Past the timeout it
# sends SIGKILL and waits up to KILL_TIMEOUT_SECONDS for that to take.
STOP_TIMEOUT_SECONDS = 5.0
STOP_POLL_SECONDS = 0.05
KILL_TIMEOUT_SECONDS = 1.0
RECV_BYTES = 1024

# Watchdog budget for one utterance: a fixed allowance for synthesis and stream
# setup and teardown, plus the text read slower than any voice actually reads
# it. Dense text (numbers, acronyms) at kokoro's minimum speed of 0.5 measures
# about 3.4 chars/s, so 2 chars/s leaves headroom even there. A worker still on
# one utterance past its budget is treated as wedged. The cases seen so far hang
# in native audio code, which no Python-level cancel can interrupt.
WATCHDOG_GRACE_SECONDS = 60.0
WATCHDOG_MIN_CHARS_PER_SECOND = 2.0
WATCHDOG_POLL_SECONDS = 5.0


# Set when PortAudio's cached default-output device is suspected stale and
# needs Pa_Terminate / Pa_Initialize before the next playback. Initial state
Expand Down Expand Up @@ -171,15 +183,45 @@ def _on_device_change(obj_id: int, n: int, addrs: Any, data: Any) -> int:
logger.debug("audio device watcher started")


def _utterance_budget(text: str) -> float:
"""Seconds the worker may spend on one request before it counts as wedged."""
return WATCHDOG_GRACE_SECONDS + len(text) / WATCHDOG_MIN_CHARS_PER_SECOND


def _remove_runtime_files() -> None:
SOCKET_PATH.unlink(missing_ok=True)
PID_PATH.unlink(missing_ok=True)


def _exit_wedged() -> None:
"""Exit hard so a fresh daemon can take over from a wedged one.

On macOS the worker has been seen deadlocked inside PortAudio's stream stop,
waiting on a CoreAudio lock held by CoreAudio's own IO thread, and parked
for days in a blocking write to an output device that stopped taking audio,
which `cancel`'s stream abort didn't release. A normal shutdown didn't get
out of the first (`stop` timed out with the process still alive), so this
uses `os._exit`. The socket goes first: clients that start
the daemon on demand do so when the socket is missing, and until it is gone
they keep getting `busy`.
"""
_remove_runtime_files()
Comment thread
StuBehan marked this conversation as resolved.
Resolved
os._exit(os.EX_SOFTWARE)


class _DaemonState:
def __init__(
self, voice: str, speed: float, lang: str, custom_voices: Mapping[str, VoiceMix] | None = None
) -> None:
self.tts = Stackvox(voice=voice, speed=speed, lang=lang, custom_voices=custom_voices)
self.queue: queue.Queue[dict] = queue.Queue(maxsize=MAX_QUEUE)
self.stop_event = threading.Event()
# Monotonic time by which the worker must finish its current request;
# None while idle.
self.deadline: float | None = None
self.worker = threading.Thread(target=self._worker, daemon=True)
self.worker.start()
threading.Thread(target=self._watchdog, daemon=True, name="stackvox-watchdog").start()
_start_device_watcher()

def _worker(self) -> None:
Expand All @@ -188,6 +230,9 @@ def _worker(self) -> None:
req = self.queue.get(timeout=WORKER_POLL_SECONDS)
except queue.Empty:
continue
# Armed before the audio refresh as well as playback: both call into
# PortAudio, and either can block on a CoreAudio lock for good.
self.deadline = time.monotonic() + _utterance_budget(req["text"])
Comment thread
StuBehan marked this conversation as resolved.
Resolved
if _audio_dirty.is_set():
_refresh_audio_devices()
_audio_dirty.clear()
Expand All @@ -203,6 +248,25 @@ def _worker(self) -> None:
# Failed playback might be a stale audio context; mark dirty
# so the next request refreshes before trying again.
_audio_dirty.set()
self.deadline = None

def overrun(self, now: float) -> float | None:
"""Seconds the current request has run past its deadline, or None if
the worker is idle or still within budget."""
deadline = self.deadline
if deadline is None or now <= deadline:
return None
return now - deadline

def _watchdog(self) -> None:
while not self.stop_event.wait(WATCHDOG_POLL_SECONDS):
overrun = self.overrun(time.monotonic())
Comment thread
StuBehan marked this conversation as resolved.
Resolved
if overrun is not None:
logger.error(
"playback worker is %.1fs past its deadline; exiting so a fresh daemon can start",
overrun,
)
_exit_wedged()

def submit(self, req: dict) -> bool:
try:
Expand Down Expand Up @@ -262,6 +326,11 @@ def handle(self) -> None:
if not text:
self.wfile.write(b"err: missing text\n")
return
# The worker sizes its watchdog budget from len(text) before playback's
# try, so anything else queued here would kill the worker.
if not isinstance(text, str):
self.wfile.write(b"err: text must be a string\n")
return

if state.submit(req):
self.wfile.write(b"ok\n")
Expand Down Expand Up @@ -344,8 +413,7 @@ def handle_signal(signum: int, frame: object) -> None:
try:
server.serve_forever()
finally:
SOCKET_PATH.unlink(missing_ok=True)
PID_PATH.unlink(missing_ok=True)
_remove_runtime_files()


def send(req: dict, timeout: float = CLIENT_TIMEOUT_SECONDS) -> tuple[bool, str]:
Expand Down Expand Up @@ -387,19 +455,39 @@ def stop(timeout: float = STOP_TIMEOUT_SECONDS) -> tuple[bool, str]:
to start, and the shutdown then completed, leaving nothing running at all.
Reporting success before the process has exited makes that sequence, the one
`status` recommends, silently useless.

A daemon still alive after `timeout` gets SIGKILL. One with its worker
deadlocked in PortAudio (see `_exit_wedged`) has been seen to acknowledge
and then never exit.
"""
pid = _read_pid()
ok, resp = send({"command": "stop"})
if not ok or pid is None:
# Nothing acknowledged, or no pid to watch: the caller's own
# `is_running` check is as good as it gets.
return ok, resp
if _wait_for_exit(pid, timeout):
return True, resp
try:
os.kill(pid, signal.SIGKILL)
except ProcessLookupError:
return True, resp # exited on its own just after the last check
if not _wait_for_exit(pid, KILL_TIMEOUT_SECONDS):
return False, f"daemon acknowledged stop but pid {pid} is still alive after SIGKILL"
# The killed daemon can't remove its own files. Leave them if a new daemon
# has started since and rewritten the pid file.
if _read_pid() == pid:
_remove_runtime_files()
return True, f"daemon did not exit within {timeout:g}s of acknowledging stop; killed pid {pid}"


def _wait_for_exit(pid: int, timeout: float) -> bool:
deadline = time.monotonic() + timeout
while True:
if not _pid_alive(pid):
return True, resp
return True
if time.monotonic() >= deadline:
return False, f"daemon acknowledged stop but pid {pid} is still alive after {timeout:g}s"
return False
time.sleep(STOP_POLL_SECONDS)


Expand Down
Loading