Qwen3-TTS warm-server hang after several generations on Apple Silicon — MPSGraph shader specialization deadlock (root-caused + fix proposed)
Summary
On Apple Silicon (MPS), the Qwen3-TTS generation worker hangs permanently after several consecutive generations in a long-running server process. The hung thread sits in MPSGraphExecutable specializeWithDevice: (MLIR Metal-shader JIT specialization), not in a mutex or in token generation. Once wedged, every later /generate queues behind it forever — status stays generating, no further model logs, and POST /models/unload + /models/load does not recover it (only a full process restart does).
Environment
- voicebox 0.5.0, backend
pytorch, Qwen3-TTS 1.7B - Apple Silicon (M4 Max), macOS 26.x, MPS
- Reproduces on a long-running server after ~3–5 consecutive short German generations, or after the first long/chunked text following a warmup.
Root cause (verified)
The hang is not a memory deadlock. Sampling the wedged thread (sample) shows it parked in:
MPSGraphExecutable specializeWithDevice: (~82% CPU)
Apple MPS JIT-compiles Metal kernels per input shape. Qwen3-TTS is autoregressive with a growing KV-cache and variable prompt lengths, so the kernel cache grows until a specialization for a new/evicted shape takes minutes. The inference runs in asyncio.to_thread; the Future never resolves, so the serial _generation_worker (await task) blocks permanently and every subsequent job stacks behind it. A model reload does not help because the wedged thread still holds the old model on MPS.
Local aggravator (in voicebox): backend/backends/pytorch_backend.py never called torch.mps.synchronize()/empty_cache() between generations (only on unload_model). Worse, calling empty_cache() per generation on Apple Silicon additionally evicts the MPSGraph kernel cache, so every subsequent same-shape generation has to re-specialize every kernel from scratch — i.e. it manufactures exactly the multi-minute compile that wedges the worker.
Reproduction
# Long-running server, then several short DE gens, then one long/chunked gen
for i in $(seq 1 5); do
curl -s -X POST localhost:8770/generate -H 'Content-Type: application/json' \
-d '{"profile_id":"<cloned-profile>","text":"Kurzer deutscher Test.","language":"de","engine":"qwen"}'
done
# 6th gen — long chunked text: hangs >5 min, worker wedges, /tasks/active stacks
Repro script (long chunked DE): scripts/repro_warm_hang_long.sh in the proposed fix branch.
Related (not exact dupes)
- #825 voice cloning hangs indefinitely on macOS 27 beta
- #596 generation gets stuck after model load (no output, infinite loading)
- #752 server stuck in
loading_modelindefinitely - #699
Stream(gpu,1) not in current threadon Apple Silicon (different — stream-init crash, already addressed)
Proposed fix (4 files, verified)
backend/backends/base.py— addsync_device_cache(device): onlytorch.mps.synchronize()(drain async queue, keep the MPSGraph kernel cache hot). Addrelease_device_cache(device)(sync + empty_cache) reserved for unload/shutdown.backend/backends/pytorch_backend.py—generate()andcreate_voice_prompt()callsync_device_cache(self.device)in afinallyper inference.empty_cachenow only onunload_model/shutdown.backend/services/task_queue.py— per-job timeout (VOICEBOX_GENERATION_TIMEOUT_SECONDS, default 900s) cancels + fails a wedged/slow gen instead of wedging the worker;_reset_tts_model()unloads + flushes MPS so the next gen starts clean;shutdown_queue()cancels worker + in-flight jobs on SIGTERM.backend/app.py—_run_shutdown()callsshutdown_queue()+release_device_cache()before model unload so a graceful SIGTERM frees the port + MPS even with a wedged inference (no more need forkill -9).
Verification
- 9 consecutive gens clean (7 short DE + 1 long chunked 90s/48s audio + 1 short after) — no hang.
- Graceful
SIGTERMfrees port 8770 within ~1s even from a wedged state; restart + 1 gen completes (≈46s incl. model load). POST /models/unload+/loadno longer needed for recovery (and no longer sufficient on its own anyway).
Residual (Apple, not voicebox)
Very long/chunked texts can still stochastically hit a slow MPSGraph specialization after ~4–5 long gens in a row (Apple MPS compiler pathology). The timeout now catches that as failed + resets the worker instead of an endless hang. Short-clip voiceover (the common case) is solid.
Branch
fix/mps-warm-hang (off main). Happy to open a PR if useful — the commit is isolated to the 4 code files + repro scripts.