gateway: a large passing reply is not read for why it failed, so a 24MB JSON reply reaches its client again (#2551)

b14784c03 put `failure(hw.code(), hw.errBody())` in the if's init of the
check that asks a too-small fallback last. An init runs before the
condition, so it ran on every try, a passing one too. A JSON reply is
held whole, so errBody is the whole reply. failure() over a 24MB body
took 1m45s on this box without -race. TestPluginStreamLargeReplies'
JSON_fast and JSON_throttled cases then waited past their client's
timeout, red on ubuntu and macOS CI at b14784c03 (run 39057486035) and
b36819d34 (run 39059788085). SSE passes through unheld, which is why
only the JSON cases failed.

The body is now read last in the condition, after hw.failed() and the
other cheap checks, as every other failure() call in serve already
does. The other callers were checked: each one is behind hw.failed() or
a status switch (passing() reads the body only for a 429).

Run (worktree at origin/main b36819d34 + this, sandbox HOME, pinned
caches, load 230 to 400):
- TestPluginStreamLargeReplies at origin/main: JSON_fast FAIL 77.9s and
  JSON_throttled FAIL 77.8s (client timeout), SSE both pass. This is the
  test that fails without the change.
- with the change: all 4 pass (JSON 45.0s and 42.0s, SSE 10.4s and 11.1s).
- failure(200, 24MB JSON) timed alone: 1m45.6s (scratch test, not
  committed).
- go test -tags nogui ./internal/gateway -count=1 -timeout 45m: ok (1941s).
- gofmt -l clean; go vet for darwin, GOOS=linux -tags nogui and
  GOOS=windows: ok.
Not run: the whole ./... suite, since the change is one condition in
internal/gateway/gateway.go. CI runs it.
This commit is contained in:
yetone
2026-10-18 04:40:49 +08:00
parent b36819d349
commit 7ac20247a9
+5 -2
View File
@@ -3231,8 +3231,11 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
continue
}
}
if f := failure(hw.code(), hw.errBody()); last && len(small) > 0 && pin == "" && other == nil && hw.failed() && !hw.passing && !hw.turnedAway && !hw.refused && !outgrew &&
(f == failQuota || f == failCredit || try.Fail == failQuota || try.Fail == failCredit) {
// the body is read for why last, once the try is known to have
// failed: a reply held whole that passed is the whole reply, and
// failure over a 24MB one held its client past its timeout
if last && len(small) > 0 && pin == "" && other == nil && hw.failed() && !hw.passing && !hw.turnedAway && !hw.refused && !outgrew &&
(try.Fail == failQuota || try.Fail == failCredit || slices.Contains([]string{failQuota, failCredit}, failure(hw.code(), hw.errBody()))) {
// every one with room is out of its allowance: those roomy
// left out as too small for the request are asked after all,
// those not resting, for the agent to be told to compact