Skip to content

fix(proxy): keep the downstream connection when an h2 upstream finishes before the request body - #1116

Open
gulldan wants to merge 2 commits into
cloudflare:mainfrom
gulldan:fix/h2-early-response-reset
Open

gulldan wants to merge 2 commits into
cloudflare:mainfrom
gulldan:fix/h2-early-response-reset

Conversation

@gulldan

@gulldan gulldan commented Nov 8, 2026 •

Copy link
Copy Markdown

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 in bidirection_down_to_up fails 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 remote NO_ERROR reset the loop records that the upstream stopped the upload instead of calling to_errored():

  • the rest of the downstream body is still read and dropped, so the body parser reaches the real end of the body before any idle read (a byte of a multi-chunk HTTP/1.1 body is not taken for data after the end of the body or for a pipelined request), and a client that writes its whole body before reading the response is not stalled;
  • the loop does not wait for that body once the response is delivered; reuse() drains whatever is left on the usual keep-alive path, and an h2 downstream stream is released, which sends the client RST_STREAM(NO_ERROR).

Other reset reasons and connection errors keep the previous behaviour.

Tests

In pingora-proxy/tests/test_upstream.rs, each against an h2::server upstream that answers before reading the body and drops the unread RecvStream (which makes h2 send RST_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 on main with downstream 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 with Sent data after end of body in 10 of 10 runs.
  • test_h2_upstream_early_response_resets_h2_downstream_body: an h2c downstream sends one body chunk after the response and expects RST_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.

…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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-08T16:18:27.592068Z 44f8f4c PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread pingora-proxy/src/proxy_h2.rs Resolved
…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>
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.

[bug] H2 upstream RST_STREAM(NO_ERROR) after an early response closes the downstream connection

1 participant