Repository navigation
Conversation
fl0Lec
force-pushed
the
fl/tcp-tunnel-upstream-half-close
branch
from
September 11, 2026 13:19
5858d3a to
06df48e
Compare
Contributor
Author
|
/retest |
allow_multiplexed_upstream_half_close lets an HTTP/2 or HTTP/3 upstream half close before the downstream. When it was introduced TcpUpstream was given a force close so that TCP proxy teardown stayed the same, and that force close reads the same runtime guard. The two behaviours are therefore inseparable: enabling independent half close for a TCP tunnel also enables the force close that cancels it out. For a tunnel carrying a one way download downstream_complete_ is never true, because the client keeps its half of the tunnel open for the life of the stream and so never sends end_stream. The upstream's half close therefore always becomes a full stream reset, and that reset discards whatever response data the downstream codec has not serialised yet. The client sees a complete set of response headers, a truncated body, and a stream reset. The force close is now selected by envoy.reloadable_features.tcp_tunnel_allow_upstream_half_close, on by default, so enabling independent half close is sufficient on its own: the half close is propagated downstream as an end of stream, the upstream connection is left open so the downstream to upstream direction keeps working, and it is closed when the stream is torn down. Setting the new guard to false restores the force close. allow_multiplexed_upstream_half_close remains the first term of force_reset_on_upstream_half_close_ because it is a lifetime precondition rather than configuration: with it disabled the decodeData() call closes the stream and destroys the TcpUpstream object, so it is what keeps the dereference of upstream_request_ after that call unreachable. Adding a second term can only narrow the set of configurations that reach the dereference, never widen it. The onUpstreamData comment is rewritten around those two configurations. It also drops the note claiming Envoy cannot distinguish a half closed TCP peer from a fully closed one: TcpUpstream enables half close semantics on this connection, and ConnectionImpl::onReadReady only turns a read EOF into a connection close when half close is disabled, so a FIN arrives as end_stream with the connection still open while a full close raises onEvent(). What a FIN does not indicate is whether the peer is still reading, which only surfaces when a later write fails. Signed-off-by: Florent Lecoultre <[email protected]>
fl0Lec
force-pushed
the
fl/tcp-tunnel-upstream-half-close
branch
from
September 14, 2026 09:37
06df48e to
2bb6f32
Compare
fl0Lec
marked this pull request as ready for review
September 14, 2026 13:08
Contributor
Author
|
cc:@yanavlasov as you originally made #34461 |
Conflict in runtime_features.cc: main removed the tcp_proxy_odcds_over_ads_fix guard in f307a7d while this branch added tcp_tunnel_allow_upstream_half_close immediately after it. Took main's removal and kept only the new guard, which stays in alphabetical position between tap_honor_tap_enabled and test_feature_true. Signed-off-by: Florent Lecoultre <[email protected]>
Member
|
Reassigning to @yanavlasov because he touched this code last. Also, should the previous runtime flag be removed now (was added 2 years ago) for clarity? |
Contributor
Author
|
I am fine with removing the previous runtime flag. But it should probably be done in another PR. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Member
|
ping for review @yanavlasov |
Contributor
|
PTAL when you have a chance @yanavlasov |
Member
|
gentle ping @yanavlasov for a review, thanks! |
Contributor
from a discussion with Yan, I think we should instead target #48648 |
yanavlasov
pushed a commit
that referenced
this pull request
Oct 30, 2026
… for TCP upstreams (#48648) Remove the `envoy.reloadable_features.allow_multiplexed_upstream_half_close` runtime guard. HTTP upstreams no longer half close before the downstream, so the compensating resets in the HTTP/1 codec and in `TcpUpstream` are removed. Half close is kept for TCP upstreams with an HTTP/2 or HTTP/3 downstream, where END_STREAM maps to a TCP FIN (RFC 9113 §8.5). HTTP/1 downstreams can only end a CONNECT response by closing the connection, so they keep the reset. The router enables it per stream when the conn pool supports it (`GenericConnPool::supportsIndependentHalfClose()`), through the new `DownstreamStreamFilterCallbacks::enableHalfClose()`. Additional Description: Follows @yanavlasov's proposal in #48316 to remove the flag. TCP tunnels still need half close: without it, an origin that FINs after a download resets the stream and the client gets a truncated body (#47378). This supersedes the TCP part of #47378 (cc @fl0Lec) and makes #48316 unnecessary. The decision uses the conn pool, not the route: the `http` upstream factory ignores the requested protocol, so a CONNECT route can still reach an HTTP/1 upstream (the #48316 crash path). Risk Level: Medium. TCP tunnels now end with an end of stream instead of a reset when the upstream half closes first. Revert with `envoy.reloadable_features.tcp_tunnel_allow_upstream_half_close=false`. Testing: Router unit tests, and `ConnectTerminationIntegrationTest.UpstreamHalfCloseKeepsTunnelOpen` / `UpstreamCloseWithHalfCloseDisabled`. Tests that only covered HTTP upstream half close are removed. Docs Changes: HTTP lifecycle in `http_connection_management.rst` and `life_of_a_request.rst`. Release Notes: Added. Platform Specific Features: N/A Runtime guard: `envoy.reloadable_features.tcp_tunnel_allow_upstream_half_close` AI assistance: prepared with AI assistance (Cursor agent). We reviewed and understand all of it. Signed-off-by: William Dauchy <[email protected]>
Contributor
|
/wait |
This branch has not been deployed
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.
Commit Message: tcp: allow a TCP tunnel upstream to half close the stream
Additional Description:
envoy.reloadable_features.allow_multiplexed_upstream_half_closelets an HTTP/2 or HTTP/3 upstream half close before the downstream. When it landed,TcpUpstreamwas given a force close so that TCP proxy behaviour stayed the same. That force close reads the same runtime guard:So the two behaviours cannot be separated: enabling independent half close for a TCP tunnel also enables the force close that cancels it out.
For a tunnel carrying a one way download,
downstream_complete_is never true — the client keeps its half of the tunnel open for the life of the stream, so it never sendsend_stream. The upstream's half close therefore always becomes a full stream reset, and that reset discards whatever response data the downstream codec has not serialised yet. The client sees complete response headers, a truncated body, and a stream reset.This gates the force close behind a separate guard,
envoy.reloadable_features.tcp_tunnel_allow_upstream_half_close, on by default. Enabling independent half close is then sufficient on its own: the upstream half close propagates downstream as an end of stream, and the stream closes once the client half closes its own side. Turning the new guard off restores the force close.allow_multiplexed_upstream_half_closedeliberately remains in the condition, and is now commented, because it is a lifetime precondition rather than configuration: it is what guarantees thedecodeData()call does not destroy theTcpUpstreamobject, which is what makes the access toupstream_request_immediately afterwards safe. Replacing it with the new guard alone would make a use after free reachable purely by configuration.I also verified that the tunnel stays usable in the remaining direction after the upstream half close: with an origin that half closes and then keeps reading, data the client sends on its still open half still arrives. That is why the upstream socket is intentionally not closed on FIN — closing it would release the file descriptor at the cost of silently dropping client to origin bytes.
Reproducing
client -> envoy -> origin, no TLS and no other proxies. The origin sends 16 MiB then half closes; the client CONNECTs, counts bytes, and reports any stream reset.envoy.yamlorigin.py— sends 16 MiB, then half closesclient.py— HTTP/2 CONNECT, counts bytes and any resetdocker-compose.yamlallow_multiplexed_upstream_half_closeenvoyproxy/envoy:v1.39-latestINTERNAL_ERRORenvoyproxy/envoy:v1.39-latestCONNECT_ERRORRows 1 and 2 are the same data loss reached by two different paths. With the guard off, the connection manager resets the stream in
doEndStream(), because a CONNECT tunnel never observes the last downstream byte. With the guard on, the connection manager correctly defers tocheckAndCloseStreamIfFullyClosed()and does not reset, butTcpUpstreamforce closes instead and the payload is lost anyway. Enabling the guard today therefore only changes which component resets the stream, not the outcome — which is why the two behaviours have to be separable.Row 1 is a separate defect in the connection manager and is out of scope here; this PR turns row 2 into row 3.
Reproduced identically on
envoyproxy/envoy-dev:latest(262,141 / 16,777,216,CONNECT_ERROR), so this is not specific to the stable branch.One disclosure on row 3: it is a build of this change on a 1.39 based tree. The code in
upstream_request.ccis byte identical on main and the patch applies cleanly, but I have not yet been able to run a full upstream main build locally.Risk Level: Medium — changes stream teardown for TCP tunnels, but only when
allow_multiplexed_upstream_half_closeis enabled, which is off by default. A deployment already running that guard with TCP tunnelling loses the force close unless it sets the new guard tofalse.Testing: New integration test
ConnectTerminationIntegrationTest.UpstreamHalfCloseWithoutForceReset.UpstreamCloseWithHalfCloseEnablednow requests the force close explicitly, since itswaitForAnyTermination()would otherwise accept the new clean end of stream and quietly stop covering the reset it is named for.Docs Changes: N/A
Release Notes: Added under
changelogs/current/behavior_changes/, since a deployment already runningallow_multiplexed_upstream_half_closewith TCP tunnelling no longer gets the force close unless it sets the new guard tofalse.Platform Specific Features: N/A
Runtime guard:
envoy.reloadable_features.tcp_tunnel_allow_upstream_half_close