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

@ -25,6 +25,7 @@ import os
import subprocess
import sys
import tempfile
import threading
from pathlib import Path
sys.path.insert(0, str(Path.home() / "chatterbox-tts-cli"))
@ -40,6 +41,14 @@ app = FastAPI(title="Chatterbox TTS (OpenAI-compatible, voice cloning)", version
_DEVICE = tts.get_device(None)
_model_cache: dict[str, tuple] = {}
# Serializes model load + inference. FastAPI runs sync handlers in a threadpool, so
# without this every concurrent request generates in parallel on the same GPU and the
# activation memory multiplies — podcast_creator sends TTS_BATCH_SIZE (default 5) clips
# at once, which OOM'd this server (peak 16.7 GB) since GPU 2 is shared with the STT
# server and the chatterbox MCP service. Generation is GPU-bound, so serializing costs
# no real throughput; it just bounds peak VRAM to a single clip.
_GPU_LOCK = threading.Lock()
VOICES_DIR = Path(__file__).parent / "voices"
CLONE_LANG = "de" # language used for every cloned reference voice
VOICE_LANG_OVERRIDES: dict[str, str] = {} # e.g. {"john": "en"} if you add an English clip
@ -124,12 +133,18 @@ def speech(req: SpeechRequest):
if not chunks:
raise HTTPException(status_code=422, detail="Kein synthetisierbarer Text übrig.")
model, model_kind, sr = _get_model(lang)
wavs = []
for chunk in chunks:
wavs.append(tts.generate_chunk(model, model_kind, chunk, lang, voice_path))
final = wavs[0] if len(wavs) == 1 else torch.cat(wavs, dim=-1)
with _GPU_LOCK:
model, model_kind, sr = _get_model(lang)
try:
wavs = []
for chunk in chunks:
wavs.append(tts.generate_chunk(model, model_kind, chunk, lang, voice_path))
final = wavs[0] if len(wavs) == 1 else torch.cat(wavs, dim=-1)
finally:
# GPU 2 is shared; hand cached blocks back so a neighbour process can't be
# starved by fragmentation we're holding on to.
if torch.cuda.is_available():
torch.cuda.empty_cache()
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as wav_tmp:
wav_path = wav_tmp.name