pipecat
pipecat copied to clipboard
Fix race condition in DeepgramFluxSTTService reconnection
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:
- The existing
_receive_task_handlercontinues running from the baseWebsocketServiceclass - It calls
_receive_messages()which iterates over the websocket - When reconnection happens,
_connect_websocket()is called again - 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_taskcontinues running and uses the new websocket connection - This matches the pattern used by other websocket services (ElevenLabs, Cartesia, Rime, etc.)
Changes
- Moved
_receive_taskcreation to_connect()(only created once) - Moved
_watchdog_taskcreation 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
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.
Filipi just spent time fixing issues in Flux here: https://github.com/pipecat-ai/pipecat/pull/3144
Closing.