Repository navigation
Description
Summary
Pusher pods accumulate memory over time due to WebSocket connection state never being freed after disconnect. The PUSHER_ACTIVE_WS_CONNECTIONS gauge is incremented on connect but not decremented on disconnect for ~90% of connections, indicating the finally cleanup block is never reached.
Evidence (live pod inspection Apr 28 2026)
Gauge vs actual TCP connections
| Pod | Gauge (active WS) | Actual TCP on :8080 | Leaked | RSS |
|---|---|---|---|---|
| 7k2lk | 199 | 17 | 182 | 3155 Mi |
| 8blbj | 147 | 20 | 127 | 2489 Mi |
| sl5zz | 132 | 16 | 116 | 2234 Mi |
| qnxrh | 79 | 22 | 57 | 1685 Mi |
| p9j42 | 7 | 7 | 0 | 514 Mi |
Perfect correlation: pods with zero leaked connections have normal memory (~500Mi). Pods with leaked connections grow linearly (~15Mi per leaked connection).
Per-connection state that leaks
Each leaked connection retains:
private_cloud_queue—deque(maxlen=20)× ~960KB chunksspeaker_sample_queue,transcript_queue,audio_bytes_queue— bounded dequesaudiobuffer,trigger_audiobuffer,private_cloud_sync_buffer— bytearraysbg_tasks— set of asyncio Tasks (may still be running)- All closures referencing the websocket, uid, and connection state
Memory map confirms Python arena bloat
/proc/1/smaps on 7k2lk:
2992 MB in anonymous regions (97% of RSS)
Single 2256 MB anon region = Python's arena allocator
Additional signals
- CLOSE_WAIT sockets: 18-21 on high-memory pods vs 4 on low-memory pods. All to Google IPs (Firestore/GCS gRPC channels not being closed)
- Thread count: 46 threads on high-memory pods vs 32 on low-memory pods (leaked executor threads?)
- No OOMKilled in 48h — but pod 7k2lk at 3155Mi/4608Mi limit (68%), heading toward OOM
- private_cloud_queue_full warnings: concentrated on the same 5 high-memory pods
Root cause hypothesis
In routers/pusher.py, the cleanup flow is:
# Line 567-601
try:
PUSHER_ACTIVE_WS_CONNECTIONS.inc() # <-- incremented
receive_task = asyncio.create_task(receive_tasks())
# ... 4 more background tasks ...
await asyncio.gather(receive_task, ...) # <-- blocks here
except Exception as e:
...
finally:
websocket_active = False
# Cancel bg_tasks ...
PUSHER_ACTIVE_WS_CONNECTIONS.dec() # <-- never reached
# Close websocket ...asyncio.gather() waits for ALL 5 tasks. If any background task hangs after WS disconnect (e.g., queue processor stuck in while websocket_active or len(queue) > 0 with non-empty queue), gather() never returns → finally block never executes → gauge never decremented → memory never freed.
Likely stuck task: process_private_cloud_queue() — if upload fails repeatedly and retry logic re-adds to pending dict indefinitely, the loop condition len(pending) > 0 keeps it alive forever.
Scope
- File:
backend/routers/pusher.py - Function:
_websocket_util_trigger()— lines 124-610 - Also check:
utils/pusher.py(client-side connect),database/_client.py(Firestore gRPC client — CLOSE_WAIT leak),utils/executors.py(thread pool sizing)
What we need
- Root cause confirmation: Identify exactly which of the 5 background tasks hangs after disconnect, and why
- Long-term fix: Ensure
finallycleanup ALWAYS runs, even if background tasks hang — considerasyncio.wait_forwith timeout on thegather(), or restructuring cleanup to not depend on task completion - CLOSE_WAIT fix: Firestore gRPC channels accumulating CLOSE_WAIT sockets — investigate if the client needs explicit channel management or keepalive tuning
- Observability: Add per-connection memory tracking or queue depth metrics so we can detect leaks before they approach OOM
Reproduction
# Check any pusher pod:
kubectl exec -n prod-omi-backend <pod> -c pusher -- \
curl -s -H "Authorization: Bearer $METRICS_SECRET" http://localhost:8080/metrics | \
grep pusher_active_ws
# Compare with actual TCP:
kubectl exec -n prod-omi-backend <pod> -c pusher -- \
python3 -c "print(sum(1 for l in open('/proc/1/net/tcp') if l.split()[3]=='01' and int(l.split()[1].split(':')[1],16)==8080))"If gauge >> actual TCP, connections are leaked.
Activity
beastoin commentedon Jun 10, 2026
Closing as not planned per CTO directive (2026-06-10).
by AI for @beastoin