fix: recover the daemon from a wedged audio worker

Merged
ST
1mo ago
+297-10

Summary

My local daemon wedged and every say came back busy, so stack-nudge went quiet. Sampling it showed the worker deadlocked in PortAudio's stream stop against CoreAudio's own IO thread (each holding a lock the other wants). cancel can't reach it since _stream is already cleared by the time the finally in speak runs, and stop timed out with the process still alive. Only kill -9 got it back, so this has the daemon get itself out, and stop finish the job when it can't.

Hit it again a week later somewhere else, the worker sat in PortAudio's WriteStream for nearly two days writing to a FiiO DAC that had stopped taking audio. stack-nudge kept cancelling and retrying, cancel does abort the stream there but the write never came back, so every nudge was accepted and nothing played. The watchdog catches that one too since it doesn't care where the worker is stuck.

Changes

  • watchdog thread gives each utterance 60s plus half a second per character, and exits with status 70 once the worker overruns. Socket and pid file go first so say reports not running instead of busy, and anything that starts the daemon on demand gets a fresh one
  • budget scales with the text so a long say --file isn't killed when a nudge queues behind it. Dense text (numbers, acronyms) at speed 0.5 measured about 3.4 chars/s, hence the 2
  • stop sends SIGKILL if the daemon acked but is still up after 5s, then clears the socket and pid file unless a newer daemon owns them
  • handler refuses a non-string text with err: text must be a string. The worker arms its deadline from len(text) outside playback's try, so {"text": 5} would have killed it (cubic caught this)
  • README

Testing

  • pytest (291), ruff check, ruff format --check and mypy stackvox all clean. The 17 new tests fail against main's daemon.py
  • ran a real daemon off this branch with a speak that never returns: three accepted, fourth busy, then it exited 70 after about 5s with the socket gone
  • same again with an exit handler that hangs, stop killed it after 5s and exited 0

stack-nudge still drops a nudge or two while the new daemon loads its model, that's a separate fix on the stack-nudge side.