fix: reuse the upstream connection after a response_filter error - #952
Open
stareezy-1 wants to merge 1 commit into
Open
fix: reuse the upstream connection after a response_filter error#952stareezy-1 wants to merge 1 commit into
stareezy-1 wants to merge 1 commit into
Conversation
When response_filter returns an error (e.g. a 3xx redirect followed by the outer retry loop), proxy_1to1's try_join! cancelled the upstream half the moment the downstream half errored, so the remaining response body was never consumed and the connection was dropped instead of returned to the pool — every retry and later request opened a new upstream connection. Replace try_join! with a select-based join: when the downstream half aborts in a response filter (signalled via a new PipeState::DownstreamFilterAborted), the upstream half is polled to completion and drains the remaining response body (bounded by a 1 MiB cap plus the peer read timeout), after which the connection is released for reuse. All other error paths keep the previous fail-fast behavior. Closes cloudflare#866.
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.
Summary
Fixes #866
Problem
When user code follows a 3xx redirect by returning
ErrfromProxyHttp::response_filter(withretry = true) and relies on the outerprocess_requestretry loop, the upstream keep-alive connection is never returned to the pool — every retry and later request opens a new upstream connection.Root cause:
proxy_1to1usedtokio::try_join!, which cancels the upstream half the moment the downstream half errors. The remaining upstream response body (e.g. the 3xx body) was never consumed, so the connection was dropped instead of released.Fix
PipeState::DownstreamFilterAborted: the downstream half signals it whenh1_response_filterfails.proxy_1to1now uses a select-based join instead oftry_join!:Ok(true)(reusable) only when the drain fully consumed the response;Ok(false)otherwise.Tests
New self-contained
tests/test_redirect_conn_reuse.rs(in-process proxy + two scripted TCP origins, no nginx):GET /a→ 302 with a delayed split body (upstream half is still reading when the downstream half aborts)response_filtererrors with retry → outer retry serves/bfrom origin BGET /cto origin ALib tests pass (22),
cargo fmt --checkand clippy clean for the touched files.