Repository navigation
fix(pusher): circuit breaker + graceful degradation (#6022) - #6030
Conversation
…trics (#6022) Co-Authored-By: Claude Opus 4.6 <[email protected]>
) Co-Authored-By: Claude Opus 4.6 <[email protected]>
…dation (#6022) Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
lgtm |
Greptile SummaryThis PR fixes three Pusher reliability bugs (#6022) by introducing a pod-level Key changes:
Issues found:
Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant WS as Client WebSocket
participant TH as transcribe.py (_stream_handler)
participant RL as _pusher_reconnect_loop
participant CB as PusherCircuitBreaker (pod-level)
participant PW as Pusher WebSocket
WS->>TH: connect
TH->>CB: can_attempt()?
CB-->>TH: True (CLOSED)
TH->>PW: connect_to_trigger_pusher()
PW-->>TH: ws (success)
TH->>CB: record_success()
Note over TH: pusher_connected=True, state=CONNECTED
PW--xTH: ConnectionClosed
TH->>TH: _mark_disconnected()
Note over TH: state=RECONNECT_BACKOFF
TH->>RL: create_task(_pusher_reconnect_loop)
Note over RL: PUSHER_SESSION_DEGRADED.inc()
loop RECONNECT_BACKOFF (up to 6 attempts)
RL->>CB: can_attempt()?
CB-->>RL: True/False
alt circuit breaker OPEN
CB-->>RL: PusherCircuitBreakerOpen
RL->>RL: state=DEGRADED (fast-path)
else normal retry
RL->>PW: connect()
alt success
PW-->>RL: ws connected
Note over RL: state=CONNECTED, break
else failure
RL->>RL: reconnect_attempts++
end
end
end
Note over RL: 6 failures → state=DEGRADED
RL->>TH: route pending convs to _create_conversation_fallback
loop DEGRADED (60s cooldown, then HALF_OPEN_PROBE)
RL->>RL: wait min(remaining, 5s)
Note over RL: after 60s → state=HALF_OPEN_PROBE
RL->>PW: single probe connect()
alt probe success
PW-->>RL: ws connected
Note over RL: state=CONNECTED, break
else probe failure
RL->>RL: state=DEGRADED, reset degraded_since
end
end
Note over RL: PUSHER_SESSION_DEGRADED.dec() (finally)
|
| logger.info(f"Pusher reconnect loop started {uid} {session_id}") | ||
| PUSHER_SESSION_DEGRADED.inc() |
There was a problem hiding this comment.
PUSHER_SESSION_DEGRADED metric incremented too early
PUSHER_SESSION_DEGRADED.inc() is called when the reconnect loop starts — which happens on any disconnect (even a transient one that recovers in seconds during RECONNECT_BACKOFF). The metric name pusher_sessions_degraded implies sessions that have exhausted retries and are in the actual DEGRADED state.
As written, a brief Pusher blip that self-heals in 1-2 retries will still appear in the pusher_sessions_degraded gauge, inflating alerts and dashboards.
Consider incrementing the gauge only when the state machine transitions to DEGRADED, and decrementing when it exits:
# In _pusher_reconnect_loop, remove the .inc() here
# Instead, inside the RECONNECT_BACKOFF block after transition:
reconnect_state = PusherReconnectState.DEGRADED
PUSHER_SESSION_DEGRADED.inc() # <-- move here
degraded_since = time.monotonic()And in the finally block, only decrement if we ever entered DEGRADED:
finally:
if reconnect_state in (PusherReconnectState.DEGRADED, PusherReconnectState.HALF_OPEN_PROBE):
PUSHER_SESSION_DEGRADED.dec()| # Route to fallback if in degraded mode | ||
| if reconnect_state == PusherReconnectState.DEGRADED: | ||
| await _fallback_process_conversation(cid) |
There was a problem hiding this comment.
Fallback routing misses
HALF_OPEN_PROBE state
When a pending conversation exhausts its Pusher retries, the code routes it to fallback only if reconnect_state == PusherReconnectState.DEGRADED. However, is_degraded() (used for all other conversation routing decisions) returns True for both DEGRADED and HALF_OPEN_PROBE. A session stuck in HALF_OPEN_PROBE with pusher_connected=False will drop conversations that exceed MAX_RETRIES_PER_REQUEST without routing them to fallback.
| # Route to fallback if in degraded mode | |
| if reconnect_state == PusherReconnectState.DEGRADED: | |
| await _fallback_process_conversation(cid) | |
| if reconnect_state in (PusherReconnectState.DEGRADED, PusherReconnectState.HALF_OPEN_PROBE): | |
| await _fallback_process_conversation(cid) |
This aligns with is_degraded() and ensures no conversation is silently abandoned.
The _pusher_reconnect_loop added in PR #6030 uses random.random() for backoff jitter but the random module was never imported, causing NameError on every reconnect attempt. This means the circuit breaker never reconnects — sessions die on first pusher disconnect. 60 errors across 15 pods since deploy. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Fixes #6022 — P0: Pusher retry exhaustion cascade.
Cherry-pick of PR #6025 (merged to
feat/opus-encoding-5418) rebased ontomainfor deployment.Three bugs fixed:
_connect()causing infinite retry —connect_to_trigger_pusher()now records success/failure on the breaker and fails fast if breaker trips during retriesFiles changed:
backend/utils/pusher.py—PusherCircuitBreakersingleton,PusherCircuitBreakerOpenexceptionbackend/utils/metrics.py— 3 Prometheus metrics (state gauge, rejection counter, degraded sessions gauge)backend/routers/transcribe.py—PusherReconnectStateenum,_mark_disconnected(),_pusher_reconnect_loop(),start_degraded(), degraded-aware conversation routingbackend/tests/unit/test_pusher_circuit_breaker.py— 32 unit testsbackend/test.sh— test registrationby AI for @beastoin