Upstream added WithBodyReadTimeout (gateway.request_body_read_timeout_seconds)
and client-gone handling for body reads. Drop this branch's own server-level
idle-timeout wrapper and server.request_body_idle_timeout setting in favor of
it, and keep the classification and diagnostics on top:
- readRequestBodyChunksWithBudget reports the read error before ctx.Err():
on HTTP/1 a read deadline also cancels the request context, which turned
every timeout into context.Canceled. Read errors carry bytes read and
elapsed time in RequestBodyReadError.
- handleRequestBodyReadError and RejectRequestBodyReadError skip the
client-gone (empty 499) path for read timeouts, so they are reported as
408 instead of a client cancellation (HTTP/1) or a generic 400 (HTTP/2).
- Every gateway body read site and both body-reading middlewares log
payload-free diagnostics, including the client-gone path.
Async tasks inherited the ingress reservation through context.WithoutCancel,
but RequestBodyLimit releases and closes it when Submit returns. The task's
request body was a plain reader, so the gateway's read reserved against the
closed reservation and every task failed with a 503 budget error.
- Add httputil.DetachRequestBodyMemory: a reservation on the same budget that
the handler's release does not close. It takes over the retained body's
existing charge from the request and reserves only the remainder, so the
body is never counted twice; the caller releases it when the work ends.
- Submit reserves the retained body before creating the task, returning 503
with Retry-After on exhaustion instead of accepting a task that cannot run;
the task releases it when execution finishes.
- Back the task request with PrereadBody so the body is not buffered again.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Gateway handlers such as Anthropic /v1/messages read the whole request body
before they know the model or account. The HTTP server sets no ReadTimeout
(large bodies may legitimately take long), so a stalled upload held the
handler until the client gave up, and every read failure then surfaced as a
generic 400 "Failed to read request body" with no diagnostics.
- Add server.request_body_idle_timeout (default 60s, 0 disables). A body
read that receives no bytes for that long fails with 408. Each Read re-arms
the deadline, so total upload time is not limited and handler pauses are
not counted. HTTP/2 deadlines are armed only during a Read; HTTP/1 keeps
one armed while the body is unfinished so net/http's own drain of an
unread body cannot hang an early error response.
- Classify body read failures in the shared reader: read timeout (408),
client disconnect (499), truncated body, decode errors and size limit.
Other failures keep the stable 400 message. Map 408 to DEADLINE_EXCEEDED
in Google-format errors.
- Log payload-free diagnostics for every gateway and middleware body read
failure: kind, protocol, declared length, encoding, bytes received and
read duration.