From c49eda98e79aaf8441c7508dfa3efe5ac1c74547 Mon Sep 17 00:00:00 2001 From: Daksh Dua Date: Fri, 20 Feb 2026 15:37:14 -0300 Subject: [PATCH] Fix race condition where context times out after sending second transcript --- src/pipecat/services/tts_service.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/pipecat/services/tts_service.py b/src/pipecat/services/tts_service.py index 1948d3da2..1e5bdf73f 100644 --- a/src/pipecat/services/tts_service.py +++ b/src/pipecat/services/tts_service.py @@ -1042,6 +1042,8 @@ class AudioContextTTSService(WebsocketTTSService): audio from context ID "A" will be played first. """ + _CONTEXT_KEEPALIVE = object() + def __init__( self, *, @@ -1152,9 +1154,15 @@ class AudioContextTTSService(WebsocketTTSService): A context ID string for the TTS request. """ if self._reuse_context_id_within_turn and self._context_id: + self._refresh_active_audio_context() return self._context_id return super().create_context_id() + def _refresh_active_audio_context(self): + """Signal that the audio context is still in use, resetting the timeout.""" + if self.has_active_audio_context(): + self._contexts[self._context_id].put_nowait(AudioContextTTSService._CONTEXT_KEEPALIVE) + async def start(self, frame: StartFrame): """Start the audio context TTS service. @@ -1242,6 +1250,10 @@ class AudioContextTTSService(WebsocketTTSService): while running: try: frame = await asyncio.wait_for(queue.get(), timeout=AUDIO_CONTEXT_TIMEOUT) + if frame is AudioContextTTSService._CONTEXT_KEEPALIVE: + # Context is still in use, reset the timeout. + continue + if frame: await self.push_frame(frame) running = frame is not None