Skip to content

http1: reset a premature upstream half close on a reused connection - #48316

Closed
wdauchy wants to merge 1 commit into
envoyproxy:mainfrom
wdauchy:wdauchy/http1-premature-half-close-reused-connection
Closed

wdauchy wants to merge 1 commit into
envoyproxy:mainfrom
wdauchy:wdauchy/http1-premature-half-close-reused-connection

Conversation

@wdauchy

@wdauchy wdauchy commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Commit Message: http1: reset a premature upstream half close on a reused connection

With allow_multiplexed_upstream_half_close enabled, the HTTP/1 client codec resets the stream when the upstream response completes before the request (!encode_complete_ in onMessageCompleteBase()). But encode_complete_ belongs to the connection and is never cleared, so on a reused connection the reset is skipped. The pool frees the connection's StreamWrapper while the router still holds it as its request encoder, and the next request body or downstream reset crashes the worker.

Clear encode_complete_ in newStream() so it reflects the current request.

Additional Description:
We hit this crash in production after enabling allow_multiplexed_upstream_half_close so that TCP tunnels can half close (see #47378). The flag is global, so it also changed the behavior of HTTP/1 upstreams. A follow-up PR will stop the router from treating this reset as an upstream failure (outlier detection, LR flag) once the response was fully forwarded.

Risk Level: Low. Only reachable with allow_multiplexed_upstream_half_close, which is off by default.
Testing: New codec unit test and integration tests that reuse an HTTP/1 upstream connection. Without the fix the unit test misses the reset and the integration tests segfault; with it, codec_impl_test and the half close cases of protocol_integration_test pass.
Docs Changes: N/A
Release Notes: Added.
Platform Specific Features: N/A

AI assistance: prepared with AI assistance (Claude Code and Cursor agent). We reviewed and understand all of it.

With envoy.reloadable_features.allow_multiplexed_upstream_half_close
enabled, the router no longer resets the upstream request when the upstream
response completes before the downstream request. For HTTP/1 upstreams the
client codec resets the stream instead, in
ClientConnectionImpl::onMessageCompleteBase(), when !encode_complete_.

encode_complete_ is a member of the client connection. onEncodeComplete()
sets it and nothing clears it, so it stays true for every request after the
first one on a connection. On a reused upstream connection the codec
therefore skips the reset. The response completion has already deleted the
CodecClient::ActiveRequest, removed its callbacks, and closed the connection
from the pool ("response before request complete"), so no reset reaches the
router. The router keeps HttpUpstream::request_encoder_, which points at the
pool's StreamWrapper, freed with the connection. The next request body from
the downstream, or a downstream reset or close, uses it and crashes the
worker.

Clear encode_complete_ in newStream(), so that it describes the request of
the current stream. A premature response on a reused connection is then
reset exactly as on a fresh connection. Nothing changes when the runtime
feature is off, because encode_complete_ is only read when it is on.

The codec unit test and the integration tests reuse an upstream connection
and fail without this change: the unit test does not see the reset, and the
integration tests crash with the stacks seen in production.

Co-authored-by: Henry Wang <[email protected]>
Signed-off-by: William Dauchy <[email protected]>
@repokitteh-read-only

Copy link
Copy Markdown

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #48316 was opened by wdauchy.

see: more, trace.

@wdauchy
wdauchy marked this pull request as ready for review October 14, 2026 13:05
@yanavlasov

Copy link
Copy Markdown
Contributor

@wdauchy I will be removing the allow_multiplexed_upstream_half_close flag altogether. I do not think this is the right change. While it is technically allowed by the HTTP/2 spec, there are no practical applications where it is needed. So it is just an unneeded complexity at this point. I do not think it makes sense to fix this behavior as it will go away.

@wdauchy

wdauchy commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

@wdauchy I will be removing the allow_multiplexed_upstream_half_close flag altogether. I do not think this is the right change. While it is technically allowed by the HTTP/2 spec, there are no practical applications where it is needed. So it is just an unneeded complexity at this point. I do not think it makes sense to fix this behavior as it will go away.

@yanavlasov thanks for the review. makes sense for this PR. Happy to close it if the flag goes away

One concern: we enabled allow_multiplexed_upstream_half_close for TCP tunneling over HTTP/2 CONNECT (#47378), not for generic HTTP/2 streams. For tunnels, half close is a real need: RFC 9113 §8.5 maps END_STREAM to TCP FIN. When an origin sends a download and then FINs while the client keeps its half open, Envoy without the flag resets the stream and the client gets a truncated body. #47378 has a reproduction: about 240 KB of 16 MiB arrives without the flag, and all 16 MiB arrive with it.

Would you be open to keeping half close only for CONNECT terminated into TCP (TcpUpstream)? HTTP/1 upstreams would then never reach the codec path that crashed here, so this fix wouldn't be needed. If you have a PR or issue for the removal, could you link it? I can also work on it if you'd like

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]>
@wdauchy

wdauchy commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

closing in favor of #48648

@wdauchy wdauchy closed this Oct 31, 2026
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.

2 participants