Repository navigation
fix(gateway): bound reading an upstream error body in time and size - #186
Open
gerarecipe wants to merge 1 commit into
Open
gerarecipe wants to merge 1 commit into
gerarecipe wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
doInferenceAttemptreturns 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_secondscannot help once the response is committed, and with both optional timeouts unsetdoInferenceAttemptis a plainclient.Do.httpx.DrainAndCloseadds 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-Encodingadjusted). 403/429/5xx classification,Retry-Afterhandling and retries see the same data as before; only an endless or stalled body is cut.client.Doreturn for "no optional timeouts" and for/systemoneis gone:/systemonestill skips the optional deadlines, but its error bodies are bounded too.bufferRetryResponsekeeps the same 1 MiB cap instead of truncating a retained error body at 64 KiB; the read is now memory-only.httpx.DrainAndClosecloses 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._test.gofiles, 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;/systemonebounded; retries still respectmax_attempts.TestErrorBodyBoundedfails on currentmainand passes here. Glad to add them to the tree if you prefer.🤖 Generated with Claude Code