From 29d604f608a3395a4cf1428ce18d0407274f893e Mon Sep 17 00:00:00 2001 From: Mark Backman Date: Tue, 21 Apr 2026 14:48:55 -0400 Subject: [PATCH 1/2] Fix UnboundLocalError in Deepgram STT connection handler If the WebSocket handshake is cancelled or fails before `keepalive_task` is assigned (e.g. an STTUpdateSettingsFrame triggers a reconnect during initial connect), the `finally` block tried to cancel an unbound local. Initialize `keepalive_task = None` before the try and guard the cancel. --- src/pipecat/services/deepgram/stt.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pipecat/services/deepgram/stt.py b/src/pipecat/services/deepgram/stt.py index 66b1d70da..c9ebd49c8 100644 --- a/src/pipecat/services/deepgram/stt.py +++ b/src/pipecat/services/deepgram/stt.py @@ -621,6 +621,7 @@ class DeepgramSTTService(STTService): """ while True: connect_kwargs = self._build_connect_kwargs() + keepalive_task = None try: async with self._client.listen.v1.connect(**connect_kwargs) as connection: self._connection = connection @@ -639,7 +640,8 @@ class DeepgramSTTService(STTService): finally: self._connection_ready.clear() self._connection = None - await self.cancel_task(keepalive_task) + if keepalive_task: + await self.cancel_task(keepalive_task) async def _keepalive_handler(self): """Periodically send KeepAlive frames to prevent server-side timeout. From 648094da261569564619624a1179e6ddc59a8115 Mon Sep 17 00:00:00 2001 From: Mark Backman Date: Tue, 21 Apr 2026 14:51:30 -0400 Subject: [PATCH 2/2] Add changelog for #4347 --- changelog/4347.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/4347.fixed.md diff --git a/changelog/4347.fixed.md b/changelog/4347.fixed.md new file mode 100644 index 000000000..e5c8206e5 --- /dev/null +++ b/changelog/4347.fixed.md @@ -0,0 +1 @@ +- Fixed a crash in `DeepgramSTTService` when an `STTUpdateSettingsFrame` arrived before the WebSocket handshake completed (for example, when pushing an update upstream on `StartFrame`). The settings-triggered reconnect cancelled the in-flight connection task before its keepalive task was created, causing an `UnboundLocalError: cannot access local variable 'keepalive_task'` in the handler's `finally` block.