fix(ws): wrap TTS in timeout so it doesn't block WebSocket after text streams

This commit is contained in:
marauder-actual
2026-05-29 17:41:49 +02:00
parent df2791a4de
commit 89a5a85c56
+12 -1
View File
@@ -13,6 +13,7 @@ Single-file FastAPI app:
from __future__ import annotations from __future__ import annotations
import asyncio
import json import json
import logging import logging
import os import os
@@ -811,7 +812,17 @@ async def chat_ws(ws: WebSocket) -> None:
if response_text: if response_text:
history.append({"role": "assistant", "content": response_text}) history.append({"role": "assistant", "content": response_text})
await _send_audio_with_voice(ws, response_text, voice) # TTS runs after text is fully streamed — don't let it block
# the WebSocket. If it takes too long, user still has the text.
try:
await asyncio.wait_for(
_send_audio_with_voice(ws, response_text, voice),
timeout=20.0,
)
except asyncio.TimeoutError:
log.warning("TTS timed out for voice=%s, skipping audio", voice)
except Exception:
log.exception("TTS failed, continuing without audio")
except WebSocketDisconnect: except WebSocketDisconnect:
log.info("%s disconnected", user["email"]) log.info("%s disconnected", user["email"])