Skip to content

Close proxy connection when tunnel TLS handshake fails#1010

Open
mbeijen wants to merge 1 commit into
pydantic:mainfrom
mbeijen:fix/proxy-tunnel-tls-close
Open

Close proxy connection when tunnel TLS handshake fails#1010
mbeijen wants to merge 1 commit into
pydantic:mainfrom
mbeijen:fix/proxy-tunnel-tls-close

Conversation

@mbeijen
Copy link
Copy Markdown
Contributor

@mbeijen mbeijen commented Jun 2, 2026

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_connections and stalls forever.

This PR wraps the start_tls() call in AsyncTunnelHTTPConnection.handle_async_request() (and its sync twin) 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.

Ports encode/httpcore#1049 (baizhu), via codeberg.org/httpxyz/httpcorexyz@b192486.

Notes

  • Only _async/http_proxy.py and tests/httpcore2/_async/test_http_proxy.py were edited by hand; the _sync/ counterparts were regenerated by scripts/unasync.py.
  • Added test_proxy_tunneling_tls_error, which uses a BrokenTLSStream that raises OSError from start_tls(). The test asserts that the request fails and that proxy.connections is empty afterwards (i.e. the leaked connection is gone). 100% coverage preserved.

Note: this change was prepared with AI assistance (Claude Code).

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
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jun 2, 2026

Merging this PR will not alter performance

✅ 15 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing mbeijen:fix/proxy-tunnel-tls-close (a5a21a5) with main (04f3804)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

Re-trigger cubic

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.

1 participant