Skip to content

Fix: Record a failure row for a request whose body cannot be buffered - #1293

Merged
esnible merged 1 commit into
rossoctl:mainfrom
esnible:fix/oversize-request-row
Oct 5, 2026
Merged

esnible merged 1 commit into
rossoctl:mainfrom
esnible:fix/oversize-request-row

Conversation

@esnible

@esnible esnible commented Oct 5, 2026

Copy link
Copy Markdown
Member

Summary

A forward-proxy request whose body couldn't be buffered got its 413 (or 400) and recorded nothing, because the early return in serveOutbound ran before the session was resolved.

Path Rows before this change
Plain HTTP none
TLS bridge (how Claude Code reaches api.anthropic.com) the tunnel's own open, and a close saying 200 with no error, in the default bucket

On the bridged path, a request the client saw fail looked healthy.

The return now records the request row and a paired failure row, in the session the client's headers name. The new bodyread.Failure classifies the failure:

Case Status error.kind
Over maxRequestBodySize 413 proxy_error, e.g. "request body of 34603008 bytes is over the 33554432-byte buffer limit"
Client hung up mid-upload 499 client_canceled (net/http cancels the request context on the read error)
Any other unreadable body 400 request_unreadable, a new kind named after the existing log line and the client's JSON error

On a bridged tunnel, the request row takes the tunnel's open with it, as any recorded request does, so no misleading close is recorded. Neither the pipeline nor a Finisher runs, so the request row has no model, tokens or cost. The 413 row counts as an error in /v1/usage. The request-row construction moves into recordOutboundRequestEvent, so this path records exactly the row the normal path does.

Two items from the #1290 review are included:

  • The 32 MiB value is pinned. TestForwardProxy_RequestBodyCapIs32MiB uses literals: exactly 32 MiB must reach both the plugin and the upstream whole, and one byte more must get a 413. The existing tests are computed from the constant, so they passed at any cap.
  • Nit: TestForwardProxy_BodyTooLarge now asserts that the plugin never saw the body.

CLAUDE.md's event-schema section now documents the new rows.

Refs #1250.

Not in this PR

  • The reverse proxy has the same rowless return for inbound requests over its 1 MB limit, going by its code.
  • A streamed SSE response cut off by an oversized event still records 200 with no error. That needs a decision on what status the row should carry, because /v1/usage counts only statusCode >= 400 as an error.
  • The remaining Fix: Raise the forward proxy's request body cap to 32 MiB #1290 review items are also not here: the memory comment, early rejection when Content-Length is already over the cap, the ext_proc docs wording, and the reverse-proxy comment nit.

Test plan

  • TestForwardProxy_OversizeRequest_RecordsProxyError: plain HTTP, 32 MiB + 1 gives a request row and a 413 proxy_error row whose message names the announced size and the limit.
  • TestForwardProxy_BridgedOversizeRequest_RecordsProxyError: over the TLS bridge, sess-1 holds the tunnel open, the request row and the 413 row, with no tunnel close and nothing in default.
  • TestForwardProxy_UnreadableRequestBody_RecordsFailure: a hangup mid-upload gives 499 client_canceled, and a malformed chunked body gives 400 request_unreadable.
  • With the new recording call removed, all four of those fail with "recorded 0 event(s)" or "holds nothing".
  • TestForwardProxy_RequestBodyCapIs32MiB fails with the reader set to 16 MiB, and with the constant set to 64 MiB.
  • TestFailure in bodyread covers each classification, including a chunked overrun with no announced size.
  • gofmt -l listener and go vet ./listener/... are clean, go test ./... in core passes, and the forwardproxy and bodyread packages also pass under -race.

Assisted-By: Claude (Anthropic AI) [email protected]

A forward-proxy request whose body could not be buffered got its 413 (or
400) and recorded nothing, because the early return ran before the session
was resolved. On plain HTTP that left no row at all. On a bridged tunnel,
the path Claude Code takes to api.anthropic.com, the only rows were the
tunnel's own open and a close saying 200, in the default bucket, so a
request the client saw fail looked healthy.

The return now records the request row and a paired failure row, in the
session the client's headers name. bodyread.Failure classifies it:

- over maxRequestBodySize: 413, proxy_error, with the announced size and
  the limit in the message
- the client hung up mid-upload: 499, client_canceled
- anything else unreadable: 400, a new kind, request_unreadable

On a bridged tunnel the request row takes the tunnel's open with it, as
any recorded request does, so no misleading close is recorded. Neither
the pipeline nor a Finisher runs, so the request row carries no parse.

From the rossoctl#1290 review:
- TestForwardProxy_RequestBodyCapIs32MiB pins the cap with literals:
  exactly 32 MiB reaches the plugin and the upstream whole, and one byte
  more gets a 413. It fails with the reader at 16 MiB and with the
  constant at 64 MiB, which the constant-relative tests did not.
- TestForwardProxy_BodyTooLarge now asserts the plugin never saw the body.

Refs rossoctl#1250

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Ed Snible <[email protected]>
@esnible
esnible requested a review from a team as a code owner October 5, 2026 17:21
@coderabbitai

coderabbitai Bot commented Oct 5, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 53 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 26137f11-2ff3-4a63-8591-2086b2d759ef
📥 Commits

Reviewing files that changed from the base of the PR and between ddbbf8d and 0ad6e68.

📒 Files selected for processing (7)
  • CLAUDE.md
  • core/listener/forwardproxy/bridgedopen_test.go
  • core/listener/forwardproxy/requestbodyfailure_test.go
  • core/listener/forwardproxy/server.go
  • core/listener/forwardproxy/server_test.go
  • core/listener/internal/bodyread/bodyread.go
  • core/listener/internal/bodyread/bodyread_test.go
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@mrsabath mrsabath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A genuine observability gap, fixed narrowly and with good evidence. I verified the mechanism rather than relying on the description.

The session-pairing works, and for the reason the comment gives. recordUnbufferedRequest sets pctx.OutboundSessionID = sid before calling recordOutboundResponseEvent, and that function reads pctx.OutboundSessionID first (server.go:1134), falling back to ActiveSession() then DefaultSessionID only when nothing was pinned. So both rows land in the same bucket, which is what makes the bridged test's "nothing in default" assertion hold.

The refactor is a pure move. I diffed the deleted inline SessionEvent literal against the extracted recordOutboundRequestEvent body: all 13 fields identical, same order, nothing added or omitted. That is what substantiates "this path records exactly the row the normal path does" — the normal path's behaviour is provably unchanged, so the extraction carries no regression risk.

The 499 classification follows an established pattern rather than inventing one. pipeline.StatusClientClosedRequest is a real constant (499) at core/pipeline/action.go:64, documented as "No client ever receives it — it has gone." More to the point, core/pipeline/snapshot.go:235's ExchangeFailure already returns exactly 499 plus Kind: "client_canceled" on ctx.Err() != nil. The new branch mirrors it, so the codebase now classifies an abandoned body-read and an abandoned exchange identically.

The /v1/usage claim rests on a real mechanism. TestRecord_UpstreamFailureCountsAsError (core/cost/usage/upstreamfailure_test.go) documents that a lone request event contributes 0 requests and 0 errors — counting happens entirely on the response half. That is precisely why recording a paired failure row, rather than only the request row, is what makes the failure visible. The fix is shaped correctly for the counting model it targets.

The MaxBytesError-before-context ordering is right, and tested. An overrun on an already-canceled context still reports 413 proxy_error, because the limit is what stopped the read — TestFailure's "overrun, client gone" case pins exactly that. Easy to get backwards; it isn't.

Test rigor is above average. The description reports negative controls: with the recording call removed all four tests fail with "recorded 0 event(s)", and TestForwardProxy_RequestBodyCapIs32MiB fails at both 16 MiB and 64 MiB. That last test addresses a blind spot it names honestly — the pre-existing tests compute their fixture from maxRequestBodySize, so they passed at any cap. Using literals is the point.

One nit inline, about discoverability rather than correctness.

Two smaller things worth acknowledging. The stale comment in bridgedopen_test.go is correctly re-pointed: it cited a body-read failure as the example of an unrecorded request, which this PR makes untrue, and it now names a plugin denial instead — easy to leave behind. And the "Not in this PR" section flags three real follow-ups unprompted: the reverse proxy has the same rowless return for its 1 MB limit, a truncated SSE response still records 200 with no error, and four deferred #1290 items. Scoping those out is the right call; the SSE one needs a status-semantics decision rather than a code change.

No secrets, no new external dependencies — only internal packages, and bodyread's new core/pipeline import introduces no cycle. CLAUDE.md's event-schema section is updated to match the new rows.

Areas reviewed: Go (forward proxy, bodyread), docs, tests, commit conventions, security
Commits: 1, signed off, imperative subject at 69 chars
CI: all 27 checks green (Spellcheck skipped)

One disclosure on method: I did not run the suite — no clone of this repo on my side — so the test-execution evidence is green CI plus your reported negative controls. I verified the integration logic, the status constants, the field-for-field refactor equivalence, and the usage-counting mechanism directly against source at 0ad6e68.

Nothing blocking. LGTM.

}
return http.StatusRequestEntityTooLarge, &pipeline.EventError{Kind: "proxy_error", Message: msg}
}
if r.Context().Err() != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, on discoverability rather than correctness.

This branch makes Failure and Rejection deliberately disagree about status for the same error: Rejection returns 400 (the response the client is sent), while Failure returns 499 (the status recorded). That divergence is right — net/http cancels the request context on the read error and discards the response, so nobody receives the 400, and recording 499 reflects what happened rather than what was attempted. The doc comment says so, and TestFailure's "client gone" case pins it.

The nit is that the two functions are adjacent in one file, take the same err, and return statuses that differ by design, with neither signature nor call site saying which is which. A reader comparing them — or someone later "fixing the inconsistency" — has to reconstruct the reasoning from the prose in Failure's doc.

A half-line on Rejection pointing at Failure (something like "the status the client is sent; the status recorded is Failure's, which differs for a client that has gone") would make the asymmetry self-evident at both ends. Purely optional.

@esnible
esnible merged commit 8c221c2 into rossoctl:main Oct 5, 2026
28 checks passed
@esnible
esnible deleted the fix/oversize-request-row branch October 5, 2026 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants