Repository navigation
Description
Problem
The pusher WebSocket service calls blocking synchronous code (requests.post, threading.Thread + .join()) directly inside async def functions, blocking the uvicorn event loop. This affects:
trigger_realtime_audio_bytes()— fires every ~1s per WebSocket connectionsend_audio_bytes_developer_webhook()— fires every ~1s per WebSocket connectiontrigger_realtime_integrations()— fires per transcript flushrealtime_transcript_webhook()— fires per transcript flush
Current impact
When these blocking calls stall the event loop, all concurrent WebSocket connections on that pod are affected — new frames can't be processed, health checks can't be accepted, and conversation processing stalls.
Why asyncio.to_thread is insufficient (PR #6335)
PR #6335 wraps these calls with asyncio.to_thread(), which moves blocking work off the event loop into the default thread pool (~40 threads). However, at audio-byte frequency:
- 3 concurrent WebSocket connections with slow webhook targets = 3 calls/sec × 15s timeout = 45 threads held → default pool exhausted
- Critical work sharing the same pool (
process_conversation,create_audio_files) gets queued behind webhook calls - Result: conversation creation stalls, but
/healthstill returns 200 — silent degradation that K8s can't detect
Proposed architecture: Bulkhead isolation (3 lanes)
1. Critical lane — dedicated ThreadPoolExecutor (2-4 workers)
For unavoidable sync blocking work that must always complete:
process_conversationcreate_audio_files_from_chunks- Conversation DB updates
2. Best-effort lane — httpx.AsyncClient (no threads)
For fire-and-forget webhooks at audio-byte frequency:
- Audio byte developer webhooks
- Realtime transcript webhooks
- App integration webhooks
Key behaviors:
- Latest-wins dropping: keep 1 in-flight + 1 pending per target. Old audio bytes have no value.
- Bounded concurrency:
asyncio.Semaphoreper target and globally. - Aggressive timeouts:
connect=1s, read=2s(not 15s). - Circuit breaker per webhook URL: open after 3 consecutive failures, 60s cooldown.
3. Storage lane — dedicated ThreadPoolExecutor (2 workers)
For file I/O that shouldn't compete with critical work:
- Private cloud chunk uploads
- Audio file creation
Migration path
| Phase | Change | Risk |
|---|---|---|
| 1 (stopgap) | Merge PR #6335 asyncio.to_thread wrapping |
Low — stops event loop blocking immediately |
| 2 | Split executors: critical_executor + storage_executor for sync work in pusher.py |
Low — isolated change in pusher |
| 3 | Replace webhook requests.post with shared httpx.AsyncClient + semaphore bounds |
Medium — changes HTTP client for webhooks |
| 4 | Add latest-wins dropping + circuit breaker for audio-byte webhooks | Medium — changes delivery semantics |
| 5 (long-term) | Move all fire-and-forget webhooks to Cloud Tasks | Higher — architectural change |
Files involved
backend/routers/pusher.py— WebSocket handler, calls all four blocking functions (lines 321-322, 349, 351)backend/utils/app_integrations.py—trigger_realtime_integrations/audio_bytesusethreading.Thread+.join()backend/utils/webhooks.py—realtime_transcript_webhook,send_audio_bytes_developer_webhookuserequests.post(timeout=15)backend/utils/conversations/location.py—get_google_maps_locationusesrequests.get
Related
- PR Fix unhealthy health check in GCP ALB for pusher endpoints #6335 — stopgap fix (asyncio.to_thread wrapping)
by AI for @beastoin
Activity