Skip to content

Pusher: blocking sync calls in async handlers cause thread pool exhaustion at audio-byte frequency #6368

Closed as not planned

Description

@beastoin

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 connection
  • send_audio_bytes_developer_webhook() — fires every ~1s per WebSocket connection
  • trigger_realtime_integrations() — fires per transcript flush
  • realtime_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 /health still 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_conversation
  • create_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.Semaphore per 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_bytes use threading.Thread + .join()
  • backend/utils/webhooks.py — realtime_transcript_webhook, send_audio_bytes_developer_webhook use requests.post(timeout=15)
  • backend/utils/conversations/location.py — get_google_maps_location uses requests.get

Related


by AI for @beastoin

Activity

  1. added
    backend
    p2
    on Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendp2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions