Close proxy connection when tunnel TLS handshake fails#1010
Open
mbeijen wants to merge 1 commit into
Open
Conversation
When an HTTP CONNECT proxy tunnel is established but the subsequent TLS handshake with the remote server fails, the underlying TCP connection to the proxy was left in ACTIVE state and never removed from the pool. The pool would then hit `max_connections` and stall. Wrap `start_tls()` in a try/except so any exception during TLS setup triggers an `aclose()` on the CONNECT connection, returning it to a closed state the pool can discard. Ported from encode/httpcore#1049 (baizhu), via codeberg.org/httpxyz/httpcorexyz@b192486. Co-Authored-By: baizhu <806927537@qq.com>
mbeijen
added a commit
to mbeijen/httpx2
that referenced
this pull request
Jun 2, 2026
Merging this PR will not alter performance
Comparing Footnotes
|
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
When an HTTP CONNECT proxy tunnel is established but the subsequent TLS handshake with the remote server fails, the underlying TCP connection to the proxy is left in ACTIVE state and never removed from the pool. The pool eventually hits
max_connectionsand stalls forever.This PR wraps the
start_tls()call inAsyncTunnelHTTPConnection.handle_async_request()(and its sync twin) in atry/except, so any exception during TLS setup triggers anaclose()on the CONNECT connection — returning it to a closed state the pool can discard.Ports encode/httpcore#1049 (baizhu), via codeberg.org/httpxyz/httpcorexyz@b192486.
Notes
_async/http_proxy.pyandtests/httpcore2/_async/test_http_proxy.pywere edited by hand; the_sync/counterparts were regenerated byscripts/unasync.py.test_proxy_tunneling_tls_error, which uses aBrokenTLSStreamthat raisesOSErrorfromstart_tls(). The test asserts that the request fails and thatproxy.connectionsis empty afterwards (i.e. the leaked connection is gone). 100% coverage preserved.Note: this change was prepared with AI assistance (Claude Code).