Repository navigation
fix(backend): unblock event loop during Deepgram WebSocket connect - #6021
Conversation
…k event loop connect_to_deepgram() calls dg_connection.start() which performs a blocking WebSocket handshake. When called from async connect_to_deepgram_with_backoff() without executor wrapping, this blocks the single-threaded event loop for the duration of the handshake. Under load or when Deepgram is slow, this freezes all other WS connections on the pod. Wrap the call in asyncio.to_thread() so the blocking handshake runs in the thread pool while the event loop stays responsive. Part of #6020. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Adds test_connect_uses_asyncio_to_thread to verify that connect_to_deepgram_with_backoff offloads the blocking connect_to_deepgram call via asyncio.to_thread. Part of #6020. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Greptile SummaryThis PR fixes a blocking-event-loop bug in Key points:
Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant EL as Event Loop
participant BF as connect_to_deepgram_with_backoff
participant TP as Thread Pool (asyncio.to_thread)
participant DG as connect_to_deepgram
EL->>BF: await connect_to_deepgram_with_backoff(...)
loop retry attempts
BF->>TP: await asyncio.to_thread(connect_to_deepgram, args...)
Note over EL: Event loop stays free<br/>(handles heartbeats, other WS)
TP->>DG: connect_to_deepgram(args) [blocking]
DG->>DG: dg_connection = deepgram.listen.websocket.v("1")
DG->>DG: dg_connection.start(options) [blocks thread]
alt success
DG-->>TP: return dg_connection
TP-->>BF: return dg_connection
BF-->>EL: return dg_connection
else exception
DG-->>TP: raise Exception
TP-->>BF: propagate exception
BF->>EL: await asyncio.sleep(backoff_delay)
end
end
|
CP9A — Level 1 Live Test: Changed-Path Coverage ChecklistChanged path
L1 EvidenceBuild output: Backend started on port 10240 from PR worktree ( P1 happy-path: WebSocket client connected to Client received P1 non-happy-path: 11 unit tests pass including:
L1 SynthesisP1 ( by AI for @beastoin |
CP9B — Level 2 Integrated Test EvidenceSetup
P1 Happy-path (integrated)
P1 Non-happy-path
L2 SynthesisP1 ( by AI for @beastoin |
|
All checkpoints passed — PR is ready for merge. Completed: CP0–CP8, CP9A (L1), CP9B (L2). CP9C (L3) not required. Summary: 1-line fix wrapping PR: #6021 by AI for @beastoin |
CP9B v2 — Level 2 Integrated Test with Local Pusher StubRe-ran L2 per manager request with Result: PASS — zero errors related to the fixSession: 157 seconds continuous streaming (emulator mic → backend → DG via Timeline: Errors: 2 total, both pre-existing:
Key finding from first L2 run: The "Recording, reconnecting" behavior was caused by pusher retry exhaustion closing the client WS — not by the VAD metrics: 90% silence savings, 24 keepalives, 5MB received, 481KB sent to DG. Updated L2 SynthesisP1 ( by AI for @beastoin |
|
lgmt // taro |
Closes #6020.
Wraps the blocking
connect_to_deepgram()call inasyncio.to_thread()inside the asyncconnect_to_deepgram_with_backoff()function. The Deepgram SDK useswebsockets.sync.clientinternally, sodg_connection.start()performs a blocking TCP+TLS+WebSocket handshake that was previously running directly on the event loop thread. Withasyncio.to_thread(), this handshake runs in the default thread pool executor, keeping the event loop free to service other WebSocket connections and health probes.Changes
backend/utils/stt/streaming.py: Changed line 424 from direct sync call toawait asyncio.to_thread(connect_to_deepgram, ...)backend/tests/unit/test_streaming_deepgram_backoff.py: Addedtest_connect_uses_asyncio_to_threadverifying the wrappingTesting
asyncio.to_threadRisks
is_activeabort check. Future: explicitThreadPoolExecutorsizing.connect_to_deepgram_with_backoffwas already async, just now non-blocking internally.by AI for @beastoin