Skip to content

http2: send the complete response before resetting an open request - #49126

Open
ggreenway wants to merge 1 commit into
envoyproxy:mainfrom
ggreenway:http2-hold-reset-until-response-sent
Open

ggreenway wants to merge 1 commit into
envoyproxy:mainfrom
ggreenway:http2-hold-reset-until-response-sent

Conversation

@ggreenway

@ggreenway ggreenway commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Commit Message: http2: send the complete response before resetting an open request

When a response completes before the downstream request, the connection manager resets the stream. The HTTP/2 codec deferred that reset only on a best-effort basis: if flow control was still holding back part of the response, it reset the stream anyway with INTERNAL_ERROR and discarded the rest of the response. This is common for gRPC client-streaming and bidirectional-streaming RPCs.

The connection manager now uses a new reset reason, LocalResetNoError, for the reset that ends an open request after a complete response. The HTTP/2 codec holds that reset until the response's END_STREAM has been sent, then sends RST_STREAM(NO_ERROR). The stream flush timeout limits the wait.

Additional Description:
This is an alternative to #48857, which fixes the same issue by keeping the connection manager's stream alive until the codec finishes. This change keeps the fix in the codec instead. That is how HTTP/3 already handles this case (EnvoyQuicServerStream::resetStream() calls StopReading() once the response is complete), and how #44749 handled the upstream half of this scenario.

Details:

  • doEndStream() uses LocalResetNoError only when the response was already complete on entry (that is, when called from endStream()) and the codec has not reported an error. Explicit aborts, protocol errors, CONNECT errors and tunnel drain-deadline resets keep their existing reasons. The HTTP/1 and HTTP/3 codecs treat the new reason like LocalReset.
  • The HTTP/2 codec keeps a held stream in pending_deferred_reset_streams_ until END_STREAM is sent, the client ends or resets the stream, the flush timer fires, or the overload manager resets it. A held stream always has a flush timer when the flush timeout is non-zero. With a flush timeout of zero, the hold has no time limit, as it already does for a stream whose request is complete.
  • While a reset is held, the codec drops request data and returns its flow-control window, even if the stream was read-disabled, so the held stream does not use up connection window that other streams share. It also keeps the buffer memory account registered, so the overload manager can still reclaim the buffered response.
  • HTTP/2 wantsToWrite() now also reports held streams, so checkForDeferredClose() does not close a draining connection early. This matches the documented contract of wantsToWrite() and what HTTP/3 already reports. The new ServerConnectionCallbacks::onWantsToWriteCleared() tells the connection manager when the codec gives up on the last held stream outside of dispatch (flush timeout or overload reset). Everything else that ends a held stream happens during dispatch, after which the connection manager already re-checks.

Known limitations:

  • The access log is written when the connection manager finishes the stream, before the codec has sent the rest of the response. I describe an alternative that addresses this in a comment below, and would appreciate feedback on it.
  • HTTP/3 has an analogous drain-close gap: a drained HTTP/3 connection whose last response was held back by flow control is not closed after the data has been sent. The new MultiplexedIntegrationTest.DrainCloseAfterFlowControlledResponse* tests exclude HTTP/3 with a TODO. I plan to address it separately.

This change was developed with the help of an AI coding assistant. I have reviewed and understand all of it.

Risk Level: Medium
Testing: New HTTP/2 codec unit tests (nghttp2 and oghttp2), connection manager unit tests, and raw-frame integration tests that send a response larger than the client's flow-control window with the request still open, including draining and flush-timeout cases.
Docs Changes: N/A
Release Notes: Added changelogs/current/bug_fixes/http2__hold-reset-until-response-sent.rst.
Platform Specific Features: N/A
Runtime guard: envoy.reloadable_features.http2_hold_reset_until_response_sent
Fixes #48192

When a response completes before the downstream request, the connection
manager resets the stream. The HTTP/2 codec deferred that reset only on a
best-effort basis: if flow control was still holding back part of the
response, it reset the stream anyway with INTERNAL_ERROR and discarded the
rest of the response. This is common for gRPC client-streaming and
bidirectional-streaming RPCs.

The connection manager now uses a new reset reason, LocalResetNoError, for
the reset that ends an open request after a complete response. The HTTP/2
codec holds that reset until the response's END_STREAM has been sent, then
sends RST_STREAM(NO_ERROR). The stream flush timeout limits the wait.

While a reset is held, the codec drops request data and returns its
flow-control window, keeps the stream's buffer memory account so that the
overload manager can still reset it, and reports the pending response
through wantsToWrite() so that a draining connection is not closed early.
The new ServerConnectionCallbacks::onWantsToWriteCleared() lets the
connection manager close a draining connection once the codec gives up on
a held response outside of dispatch.

Other resets, including explicit aborts and resets after a codec-detected
error, keep their existing reasons and behavior.

Runtime guard: envoy.reloadable_features.http2_hold_reset_until_response_sent

Fixes envoyproxy#48192

Signed-off-by: Greg Greenway <[email protected]>
@repokitteh-read-only

Copy link
Copy Markdown

CC @envoyproxy/runtime-guard-changes: FYI only for changes made to (source/common/runtime/runtime_features.cc).

🐱

Caused by: #49126 was opened by ggreenway.

see: more, trace.

@ggreenway

Copy link
Copy Markdown
Member Author

I'd like feedback on one design question before this goes further.

This PR holds the reset in the HTTP/2 codec, and the connection manager finishes its stream as soon as the response has been handed to the codec, the same way HTTP/3 works today. The downside is observability: the access log and trace are written before the codec has finished sending the response, a later flush-timeout truncation only shows up in http2.tx_flush_timeout, and the connection is treated as idle while it is still sending. Handling draining connections also needed the wantsToWrite() change and the new ServerConnectionCallbacks::onWantsToWriteCleared().

An alternative is a hybrid: keep everything in the codec as it is in this PR, but have the connection manager keep its stream as a zombie when it resets with LocalResetNoError, instead of destroying it. The zombie is reaped when the codec reports the outcome through the existing onCodecEncodeComplete() and onCodecLowLevelReset() callbacks. That is the mechanism the connection manager already uses when the request is complete and flow control is holding back the response.

What the hybrid would gain:

  • Access logs, traces and timing reflect when the response was actually delivered or abandoned.
  • Draining needs no new API. The zombie keeps the connection open, and reaping it re-runs the close check, so onWantsToWriteCleared() and the wantsToWrite() change could be removed.
  • The connection is not counted as idle while it is still sending.

What it would cost:

  • The connection manager's own reset uses up the stream's reset callbacks, so a later client reset no longer reaches onResetStream(). onStreamClose() would need to fire onCodecLowLevelReset() for held streams, or the zombie leaks until the connection closes.
  • doDeferredStreamDestroy() updates the premature-reset counters each time it runs, before its zombie check, so a zombie is counted twice. That already happens for request-complete zombies, but the hybrid would make zombies much more common, which would dilute the rapid-reset protection unless that is fixed. The fix is small.
  • Every HTTP/2 response that completes before its request, including local replies, would be finished when the codec sends END_STREAM rather than in doEndStream(). Access-log byte counts for such local replies would change, as they did in http2: prevent gRPC streaming response truncation on premature local resets #48857.
  • Filters and connection manager state stay alive while the response is held, as they already do for request-complete zombies.

Compared with #48857, the hybrid keeps the stream as a zombie rather than as an active stream. The connection manager would not need to intercept request data, schedule the reset, or special-case reset reasons, because the codec drops request data and sends the reset itself.

Does the hybrid seem worth it for the observability and drain handling, or is the codec-only approach in this PR preferable? I'm happy to rework it either way.

@bingqifan

Copy link
Copy Markdown
Contributor

Thanks for explaining the trade-offs between the two approaches. After looking at both, I lean toward the hybrid approach. #48857 also uses a zombie stream to retain HCM state. I like that your proposal puts the HTTP/2 codec in charge of deferring the reset and handling any subsequent request DATA. That lets HCM wait until the codec finishes encoding the response or abandons the flush before it records the request end time and writes the final access log entry.

I also noticed while looking at the HTTP/3 implementation that QUIC may still have response data waiting to be sent when HCM finalizes %DURATION%. If you think it’s worth adjusting when that happens, I’d be happy to open a separate issue to confirm the expected behavior and follow up with a fix.

For edge-case testing, I think we can reuse a few tests from #48857. For example, if the upstream sends a reset after the upstream stream has been detached, the codec should still finish sending the complete response it already has queued. I’d also check that HCM completes cleanup in each case where the client resets the stream early, the downstream connection closes, or the flush times out, and that the same reset is only counted once.

@bingqifan

Copy link
Copy Markdown
Contributor

I have some availability over the next few days and would be happy to help finish #49126.
Since I’ve already spent a lot of time investigating this issue in #48857, it would be good to see the fix through together.
We can continue directly on your branch, using whatever workflow works best for you. I’m also happy to take on the remaining implementation and tests.
It would be great to finish this as a joint contribution. Would that work for you?

@ggreenway
ggreenway marked this pull request as ready for review December 1, 2026 01:05

This branch has not been deployed

No deployments
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.

HTTP/2 RST_STREAM(NO_ERROR) after a complete gRPC response drops pending downstream encode

5 participants