Repository navigation
fix(proxy): keep the downstream connection when an h2 upstream finishes before the request body - #1116
Open
gulldan wants to merge 2 commits into
Open
fix(proxy): keep the downstream connection when an h2 upstream finishes before the request body#1116gulldan wants to merge 2 commits into
gulldan wants to merge 2 commits into
Conversation
…es before the request body A server may send a complete response before it has read the whole request body and then reset the stream with RST_STREAM(NO_ERROR) (RFC 9113 section 8.1); nginx does this for every early response. The proxy treated the failed body write as an upstream error, marked the downstream as errored and closed its connection after a response that already advertised keep-alive, so an HTTP/1.1 client that had sent its next request saw a connection reset. The body write failure is now checked against the stream reset reason: a remote NO_ERROR reset finishes the request body instead, and the connection is reused. Measured against an nginx h2 upstream at 32k keep-alive POST requests per second: 4457 client connection errors in 20 s before, 0 after.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 44f8f4c89c
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…reset Marking the request body finished on RST_STREAM(NO_ERROR) left the HTTP/1.1 body parser expecting more bytes while the loop moved on to idle reads. When the client sent the rest of its body in several chunks while the early response was still being delivered, the next body byte was taken for data after the end of the body (or for a pipelined request) and the response was aborted with "Sent data after end of body". The loop now records that the upstream stopped the upload, keeps reading the downstream body and drops it, and no longer waits for that body once the response is delivered; reuse() drains whatever is left. A 7 MiB response with a one-byte-at-a-time body was cut at about 4.3 MB in 10 of 10 runs before and is delivered in full now; an h2 downstream that keeps sending its body gets RST_STREAM(NO_ERROR) after the response. 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.
Fixes #1115.
Related: #404 looks like the same upstream behaviour from a gRPC server (the OpenTelemetry Collector closed the stream before the request body was written, logged as
while writing h2 request body cause: user error: inactive stream).What
When an HTTP/2 upstream sends a complete response and then
RST_STREAM(NO_ERROR)because it does not need the rest of the request body (RFC 9113 §8.1 — nginx does this for every early response), the request body write inbidirection_down_to_upfails and the proxy marked the downstream as errored, closing a keep-alive HTTP/1.1 connection after the response was already delivered. Clients that had sent their next request got a connection reset.The failed body write is now checked against
SendStream::poll_reset. On a remoteNO_ERRORreset the loop records that the upstream stopped the upload instead of callingto_errored():reuse()drains whatever is left on the usual keep-alive path, and an h2 downstream stream is released, which sends the clientRST_STREAM(NO_ERROR).Other reset reasons and connection errors keep the previous behaviour.
Tests
In
pingora-proxy/tests/test_upstream.rs, each against anh2::serverupstream that answers before reading the body and drops the unreadRecvStream(which makesh2sendRST_STREAM(NO_ERROR)behind the response):test_h2_upstream_early_response_keeps_downstream_alive: the HTTP/1.1 downstream sends the body after the response, then a second request on the same connection. Fails onmainwithdownstream connection closed after the early response: "".test_h2_upstream_early_response_reads_body_sent_during_delivery: the HTTP/1.1 downstream sends a 3-byte body one byte at a time while a 7 MiB response is still being delivered, then a second request. With the body marked finished on the reset, the response was cut at about 4.3 MB withSent data after end of bodyin 10 of 10 runs.test_h2_upstream_early_response_resets_h2_downstream_body: an h2c downstream sends one body chunk after the response and expectsRST_STREAM(NO_ERROR)from the proxy within 5 s instead of a proxy waiting for the rest of the body.Measured on the first revision of this change (the path for a body sent in one piece is unchanged), with nginx as the h2 upstream at 32 000 keep-alive POST requests/s for 20 s: 4457 client connection errors before, 0 after; at 48 000/s: 32 299 before, 0 after.