pipecat icon indicating copy to clipboard operation
pipecat copied to clipboard

Fix race condition in DeepgramFluxSTTService reconnection

Open jamsea opened this issue 1 month ago • 1 comments

Issue

Resolves the error: cannot call recv while another coroutine is already running recv or recv_streaming that occurs after upgrading to 0.0.95 when DeepgramFluxSTTService attempts to reconnect.

Root Cause

The _receive_task and _watchdog_task were being created inside _connect_websocket(). During reconnection:

  1. The existing _receive_task_handler continues running from the base WebsocketService class
  2. It calls _receive_messages() which iterates over the websocket
  3. When reconnection happens, _connect_websocket() is called again
  4. Multiple coroutines attempt to receive from the websocket simultaneously → error

Solution

Moved task creation from _connect_websocket() to _connect():

  • Tasks are now created only once during initial connection
  • During reconnection, only the websocket connection itself is re-established
  • The existing _receive_task continues running and uses the new websocket connection
  • This matches the pattern used by other websocket services (ElevenLabs, Cartesia, Rime, etc.)

Changes

  • Moved _receive_task creation to _connect() (only created once)
  • Moved _watchdog_task creation to _connect() (only created once)
  • Added comments clarifying tasks are created only during initial connection
  • _connect_websocket() now only handles low-level websocket connection

Testing

  • Verified the pattern matches other websocket services in the codebase
  • Ensured proper separation of concerns between connection and task lifecycle

jamsea avatar Nov 26 '25 09:11 jamsea

Codecov Report

:x: Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/pipecat/services/deepgram/flux/stt.py 0.00% 4 Missing :warning:
Files with missing lines Coverage Δ
src/pipecat/services/deepgram/flux/stt.py 0.00% <0.00%> (ø)
:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Nov 26 '25 09:11 codecov[bot]

Filipi just spent time fixing issues in Flux here: https://github.com/pipecat-ai/pipecat/pull/3144

Closing.

markbackman avatar Dec 06 '25 04:12 markbackman