Skip to content

fix(gateway): bound reading an upstream error body in time and size - #186

Open
gerarecipe wants to merge 1 commit into
jasonxu114514:mainfrom
gerarecipe:bounded-error-body
Open

gerarecipe wants to merge 1 commit into
jasonxu114514:mainfrom
gerarecipe:bounded-error-body

Conversation

@gerarecipe

Copy link
Copy Markdown

Problem

An upstream or exit that sends error headers (429, 403, 5xx, …) and then stalls on the body holds the whole retry/fallback loop until the request budget (retry.timeout_seconds) is gone. No other key, exit or tier is tried meanwhile.

Root cause

doInferenceAttempt returns a non-2xx response as soon as its headers arrive. Its body is read afterwards by classification (peekResponseBody), retry buffering (bufferRetryResponse) and the error copied to the client (copyErrorResponse) — none of them with a deadline. attempt_timeout_seconds cannot help once the response is committed, and with both optional timeouts unset doInferenceAttempt is a plain client.Do. httpx.DrainAndClose adds another untimed read (up to 64 KiB) before closing.

Change

  • doInferenceAttempt: for a non-2xx response, read at most 1 MiB of the body under an absolute 2 s limit (not reset by a peer trickling bytes; the attempt timer stays armed), close without draining, and return the buffered bytes with status and headers intact (Content-Length/Transfer-Encoding adjusted). 403/429/5xx classification, Retry-After handling and retries see the same data as before; only an endless or stalled body is cut.
  • The early client.Do return for "no optional timeouts" and for /systemone is gone: /systemone still skips the optional deadlines, but its error bodies are bounded too.
  • bufferRetryResponse keeps the same 1 MiB cap instead of truncating a retained error body at 64 KiB; the read is now memory-only.
  • httpx.DrainAndClose closes without a speculative read. Closing an unread body only gives up connection reuse; both transports already reuse a connection after a clean EOF.

2xx responses and streams are untouched.

Verification

  • gofmt -l ., go vet ./..., Linux and Windows builds: clean.
  • The repository has no _test.go files, so none is committed. Throwaway tests against loopback fake upstreams (not committed) covered: a stalled error body returns within the limit and the next key/tier is tried; a finite error body is preserved byte-for-byte; the 1 MiB cap; parent cancellation; /systemone bounded; retries still respect max_attempts. TestErrorBodyBounded fails on current main and passes here. Glad to add them to the tree if you prefer.

🤖 Generated with Claude Code

doInferenceAttempt returned a non-2xx response at its headers. Its body was
then read by classification (peekResponseBody), retry buffering
(bufferRetryResponse) and the error copied to the client
(copyErrorResponse), none with a deadline of their own. An upstream or
exit that sends error headers and then stalls held the retry and fallback
loop until the whole request budget was gone.

Read at most 1 MiB of a non-2xx body inside doInferenceAttempt under an
absolute 2 s limit (the attempt timer stays armed), close without draining,
and hand the buffered bytes on with status and headers intact, so
403/429/5xx classification and retries are unchanged. /systemone keeps
skipping the optional deadlines but gets the same bound. Retry buffering
keeps the same cap instead of truncating at 64 KiB, and DrainAndClose no
longer does an untimed speculative read before closing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant