Fix race condition in Deepgram STT disconnect causing error flood

Clear self._connection before sending close stream so run_stt stops
sending audio immediately during the WebSocket close handshake.
This commit is contained in:
Mark Backman
2026-03-06 21:01:31 -05:00
parent 750b87dc24
commit 536f1e178a

View File

@@ -594,13 +594,16 @@ class DeepgramSTTService(STTService):
return
logger.debug("Disconnecting from Deepgram")
# Ask Deepgram to close the stream gracefully before cancelling the task.
if self._connection:
await self._connection.send_close_stream()
# Clear self._connection first to prevent run_stt from sending audio
# during the close handshake, then close gracefully on the saved ref.
connection = self._connection
self._connection = None
if connection:
await connection.send_close_stream()
await self.cancel_task(self._connection_task)
self._connection_task = None
self._connection = None
async def _connection_handler(self):
"""Manages the full WebSocket lifecycle inside a single async with block.