Fix two podcast failures: dead TTS server after reboot + CUDA OOM

Podcast generation failed twice at the audio stage, for two unrelated reasons
that look similar from the UI but need opposite fixes.

1) TTS/STT ran as nohup background processes and silently did not survive a
   reboot. Podcasts then failed with "Failed to generate speech: All connection
   attempts failed" (httpx.ConnectError) even though outline and transcript had
   generated fine.

   Both now run as systemd user units. The unit files are versioned in
   services/systemd/ (using %h, not a hardcoded home) and installed by
   scripts/start_services.sh, which also enables linger so they start on boot
   without a login session. They pin GPU 2 by UUID, not by index: CUDA orders
   devices "fastest first", so index 2 can resolve to the T600.

2) GPU 2 is shared by three processes (TTS, STT and the separate chatterbox-tts
   MCP service on :9999), leaving ~12 GB of headroom. podcast_creator sends
   TTS_BATCH_SIZE (default 5) clips concurrently, and since /audio/speech is a
   sync FastAPI handler, they generated genuinely in parallel on one shared
   model. Activation memory multiplied, the TTS process hit 16.7 GB and threw
   torch.OutOfMemoryError, surfacing as "HTTP 500" from the endpoint.

   tts_server.py now serializes generation behind a lock (GPU-bound work, so
   parallelism buys no throughput — it only multiplies peak VRAM) and frees the
   cache afterwards. TTS_BATCH_SIZE=1 keeps the client from queuing requests in
   that lock and running into esperanto's 300s TTS timeout; ESPERANTO_TTS_TIMEOUT
   is raised to 600s as headroom.

Verified: 5 concurrent /audio/speech requests all return 200 with GPU 2 peaking
at ~11.5 GB (was 16.7 GB for the TTS process alone), and the previously failed
episode now completes end to end — 38/38 batches, 10:56 min of audio, zero OOM.

Docs record both failure signatures side by side, since ConnectError (server
dead) and HTTP 500 (server alive, out of VRAM) have very different remedies.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Dieter Schlüter 2026-07-11 13:02:32 +02:00
commit 03877588e1
8 changed files with 244 additions and 52 deletions

View file

@ -81,15 +81,61 @@ Open Notebook's `openai_compatible` provider type talks to any endpoint implemen
- `services/tts_server.py` — wraps `chatterbox-tts` (via `~/chatterbox-tts-cli/chatterbox_cli_v4.py`), run with the `chatterbox` conda env (`~/miniforge3/envs/chatterbox/bin/python`).
- `services/stt_server.py` — wraps `faster-whisper` (`large-v3` model), run with the base `python3`.
Both are plain background processes (`nohup ... &`), **not** systemd units — they don't survive a reboot; restart manually if needed:
Both run as **systemd user units**. The unit files live in the repo (`services/systemd/*.service`,
using `%h` rather than a hardcoded home) and `scripts/start_services.sh` installs them into
`~/.config/systemd/user/`, enables `linger` and starts them — so they come up on boot without a
login session. Re-run that script after editing a unit; it's idempotent. They log to the journal,
not to `services/logs/` (those files are leftovers from the earlier `nohup` setup):
```bash
cd ~/open-notebook/services
CUDA_VISIBLE_DEVICES=2 TTS_PORT=8901 nohup ~/miniforge3/envs/chatterbox/bin/python tts_server.py > logs/tts_server.log 2>&1 &
CUDA_VISIBLE_DEVICES=2 STT_PORT=8902 nohup python3 stt_server.py > logs/stt_server.log 2>&1 &
systemctl --user status open-notebook-tts open-notebook-stt
systemctl --user restart open-notebook-tts # after editing tts_server.py or adding voices
journalctl --user -u open-notebook-tts -f
```
They're pinned to `CUDA_VISIBLE_DEVICES=2` deliberately — GPU 1 tends to fill up with Ollama's resident models (`OLLAMA_KEEP_ALIVE=-1` keeps them loaded), so TTS/STT get GPU 2 to themselves rather than risking an OOM race with Ollama.
They were previously plain `nohup ... &` background processes, which silently did not survive a
reboot — a podcast then failed at the audio stage with `Failed to generate speech: All connection
attempts failed` (`httpx.ConnectError` against `host.docker.internal:8901`) even though outline
and transcript had generated fine. That's the signature of a dead TTS server, not a model problem.
Both units pin GPU 2 **by UUID** (`CUDA_VISIBLE_DEVICES=GPU-83ba6d1f-…`), not by index: GPU 1 is
occupied by Ollama's resident models (`OLLAMA_KEEP_ALIVE=-1` keeps them loaded), so TTS/STT go to
GPU 2 rather than racing Ollama for VRAM. The index form (`CUDA_VISIBLE_DEVICES=2`) is **not**
reliable here — CUDA's default device order is "fastest first", not nvidia-smi's PCI order, so
index 2 can resolve to the T600. Re-check the UUID with
`nvidia-smi --query-gpu=index,name,uuid --format=csv` if the GPUs are ever reseated.
#### GPU 2 is shared — TTS concurrency is capped on purpose
GPU 2 does **not** belong to the TTS server alone. Three processes sit on it: `open-notebook-tts`
(~4 GB idle-loaded), `open-notebook-stt` (faster-whisper `large-v3`, ~4 GB), and the unrelated
`chatterbox-tts.service` MCP server on port 9999 (~3.4 GB). That leaves roughly 12 GB of working
headroom, and TTS inference must fit inside it.
`podcast_creator` requests `TTS_BATCH_SIZE` clips **concurrently** (`asyncio.gather` per batch,
`nodes.py`; upstream default **5**). `tts_server.py`'s `/audio/speech` is a *sync* FastAPI handler,
so FastAPI runs each request in a threadpool thread — five clips therefore generate genuinely in
parallel on one shared model, and the activation memory multiplies. That drove the TTS process to
**16.7 GB** and produced `torch.OutOfMemoryError` inside chatterbox's `s3gen.embed_ref`, surfacing
in the app as `Failed to generate speech: OpenAI-compatible TTS endpoint error: HTTP 500`. Note
this is a *different* failure from the dead-server `ConnectError` above — a 500 means the server is
alive and rejecting the work, so check the TTS journal for the CUDA OOM before assuming a restart
will help.
Two changes keep it bounded, and both matter:
- **`tts_server.py` serializes generation behind `_GPU_LOCK`** (a `threading.Lock` around model load
+ the chunk loop, plus `torch.cuda.empty_cache()` afterwards). Generation is GPU-bound, so running
clips in parallel buys no throughput on a single card — it only multiplies peak VRAM. This is the
hard guarantee: it holds no matter which client calls, and no matter what batch size they use.
- **`TTS_BATCH_SIZE=1` in `docker-compose.yml`** so the client sends clips one at a time. Without
this, the lock still prevents the OOM, but 4 of the 5 batched requests sit queued in it — and with
~40-60 s per clip the last one approaches esperanto's **300 s** TTS timeout. `ESPERANTO_TTS_TIMEOUT=600`
is set alongside it as extra headroom for long segments.
Measured after the fix: 5 concurrent `/audio/speech` requests all return 200, GPU 2 peaks at
~11.5 GB total (vs. 16.7 GB for the TTS process alone before), i.e. ~12 GB of headroom left.
If you add another GPU-resident service to GPU 2, re-check that budget.
Reachability from the `open_notebook` container requires **two things**, both already done: the `extra_hosts: host.docker.internal:host-gateway` entry in `docker-compose.yml`, and UFW rules allowing inbound `8901/tcp` and `8902/tcp` (same pattern as the existing `11434/tcp` rule for Ollama — UFW defaults to deny-incoming, so any new host-side port a container needs to reach must be explicitly opened).
@ -107,7 +153,7 @@ Three reference clips exist in `services/voices/` (gitignored, not committed):
piper --model de_DE-thorsten-medium.onnx --output_file male_thorsten.wav <<< "some German sentence"
```
Podcast speaker profiles are assigned across these three: `business_panel` (Marcus→male_thorsten, Elena→female_kerstin, Johny→default), `tech_experts` (Alex→male_thorsten, Jamie→female_kerstin), `solo_expert`/`test_speaker_local` (→female_kerstin/Anna). To add another distinct voice, drop a new `<name>.wav` (~10-30s, clean single-speaker audio, licensing-checked) into `services/voices/`, restart `tts_server.py` (voices are scanned once at startup), and set the speaker's `voice_id` to `<name>` via `PUT /api/speaker-profiles/{id}`.
Podcast speaker profiles are assigned across these three: `business_panel` (Marcus→male_thorsten, Elena→female_kerstin, Johny→default), `tech_experts` (Alex→male_thorsten, Jamie→female_kerstin), `solo_expert`/`test_speaker_local` (→female_kerstin/Anna). To add another distinct voice, drop a new `<name>.wav` (~10-30s, clean single-speaker audio, licensing-checked) into `services/voices/`, restart the TTS server (`systemctl --user restart open-notebook-tts`voices are scanned once at startup), and set the speaker's `voice_id` to `<name>` via `PUT /api/speaker-profiles/{id}`.
## Secrets