Skip to content

fix(router): release the half-open circuit token on a panic from attempt - #8

Merged
tigrawap merged 3 commits into
mainfrom
fix/circuit-breaker-halfopen-leak
Oct 12, 2026
Merged

tigrawap merged 3 commits into
mainfrom
fix/circuit-breaker-halfopen-leak

Conversation

@oferki

@oferki oferki commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Problem statement

Running with the wekai router returned 502 upstream_unavailable for every single request — including a trivial hand-crafted curl — instantly (sub-3ms, meaning no real network call to the backend was ever attempted), while the vLLM workers behind them were confirmed completely healthy and idle (vllm:num_requests_running=0, direct curl succeeded). Restarting the router pod (which resets all in-memory state) immediately fixed it. This will recur under the same trigger condition.

Root cause

  1. Breaker.Allow() in router/internal/circuit/circuit.go (states Closed/Open/HalfOpen, HalfOpenMax: 1 per DefaultConfig()) admits at most ONE probe request while recovering from Open. The ONLY thing that releases that probe token is the matching Breaker.Record(outcome, token) call (defer b.halfOpenTokens.Add(-1) inside Record).
  2. The call site pairing them is Proxy.Serve() in router/internal/proxy/proxy.go (~lines 249-263): permitted, token := b.CB.Allow(), then unconditionally later in the same stack frame, out := p.attempt(...) followed by b.CB.Record(circuit.Classify(out.status, out.err), token).
  3. attempt() calls rp.ServeHTTP(...) (httputil.ReverseProxy). When a client disconnects mid-stream, ReverseProxy raises http.ErrAbortHandler as a real Go panic — this is documented behavior of net/http.
  4. recoverMiddleware in router/internal/gateway/middleware.go (lines 39-71) wraps the ENTIRE handler chain (outermost, by design). For http.ErrAbortHandler specifically, it re-panics (line 61) after bumping a disconnect metric, rather than swallowing it — this is intentional so net/http's own recovery closes the connection without a bogus stack trace. The middleware's own comment documents the exact trigger: "a load driver hitting its timeout cancelled 16 in-flight streams at once, and every one of them landed here as a panic" — precisely what an aborted AIPerf benchmark run does with many concurrent streaming requests.
  5. The problem: that panic unwinds the ENTIRE stack, including Proxy.Serve(), which has no defer/recover around the Allow() → attempt() → Record() sequence. So Record() at proxy.go:263 is skipped entirely. If the one admitted half-open probe happens to be a request whose client disconnects mid-stream — very likely during a mass-abort event — that backend's single HalfOpenMax=1 token leaks permanently, and every future probe is denied forever with no path to recovery. This matches the observed symptom exactly.
  6. Corroborating evidence already in the codebase: proxy.go's own header comment (lines 23-27) independently documents a related, previously-noticed symptom of the same divergence: "a backend answering 53 consecutive 500s in one window ... did not open its breaker on a live fleet ... The divergence is somewhere between attempt() returning and Record receiving, which is this file." This package's header also says "THIS PACKAGE HAS NO TESTS."

Fix

runAttempt wraps the Allow-paired call so Record always runs, whether attempt returns or panics, and re-panics afterward so recoverMiddleware's existing ErrAbortHandler handling (metrics, logging) is unchanged. A client abort is classified Ignored, like a 429; any other panic is classified Failure.

Tests done

  1. Ran with agentx benchmarks on the dcai cluster.
  2. Validated that even surpassing the pods' capacity the engines are still taking requests in after they're free

Note

Medium Risk
Touches proxy serve-path panic handling and circuit breaker classification on client aborts; behavior change is narrowly scoped but affects recovery from mass client disconnects under load.

Overview
Fixes a permanent half-open circuit breaker deadlock when httputil.ReverseProxy panics with http.ErrAbortHandler after a client disconnects mid-stream, which could skip the paired Record and leave the single HalfOpenMax probe token leaked so backends never recover (instant 502 upstream_unavailable until restart).

Proxy.Serve now routes each admitted attempt through runAttempt, which always calls cb.Record on the normal path or inside a recover before re-panicking. Client abort panics are recorded as circuit.Ignored (like a 429); other panics as Failure, so outer middleware behavior stays the same while breaker bookkeeping stays exception-safe.

Adds proxy_test.go regression tests for abort vs other panics and for the unchanged successful probe path.

Reviewed by Cursor Bugbot for commit 3b872b3. Bugbot is set up for automated code reviews on this repo. Configure here.

oferki and others added 3 commits September 22, 2026 21:03
Serve calls b.CB.Allow() then p.attempt() then b.CB.Record(...) with no
defer between them. httputil.ReverseProxy raises http.ErrAbortHandler as
a real panic when a client disconnects mid-stream, which unwinds past
Record and leaks the HalfOpenMax token forever, permanently denying every
future probe on that backend. A mass client-abort event (e.g. a load
driver hitting its timeout) can hit every router-fronted backend at once.

runAttempt wraps the Allow-paired call so Record always runs, whether
attempt returns or panics, and re-panics afterward so recoverMiddleware's
existing ErrAbortHandler handling (metrics, logging) is unchanged. A
client abort is classified Ignored, like a 429; any other panic is
classified Failure.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Serve calls b.CB.Allow() then p.attempt() then b.CB.Record(...) with no
defer between them. httputil.ReverseProxy raises http.ErrAbortHandler as
a real panic when a client disconnects mid-stream, which unwinds past
Record and leaks the HalfOpenMax token forever, permanently denying every
future probe on that backend. A mass client-abort event (e.g. a load
driver hitting its timeout) can hit every router-fronted backend at once.

runAttempt wraps the Allow-paired call so Record always runs, whether
attempt returns or panics, and re-panics afterward so recoverMiddleware's
existing ErrAbortHandler handling (metrics, logging) is unchanged. A
client abort is classified Ignored, like a 429; any other panic is
classified Failure.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@tigrawap
tigrawap merged commit b145731 into main Oct 12, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants