From 89a5a85c568b32f7bc679ddfe6eb11725e27d849 Mon Sep 17 00:00:00 2001 From: marauder-actual Date: Fri, 29 May 2026 17:41:49 +0200 Subject: [PATCH] fix(ws): wrap TTS in timeout so it doesn't block WebSocket after text streams --- app/main.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 0f4b8d4..9cb8430 100644 --- a/app/main.py +++ b/app/main.py @@ -13,6 +13,7 @@ Single-file FastAPI app: from __future__ import annotations +import asyncio import json import logging import os @@ -811,7 +812,17 @@ async def chat_ws(ws: WebSocket) -> None: if 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: log.info("%s disconnected", user["email"])