Skip to content

Commit 204ec92

Browse files
committed
Fix zombie connection when TLS handshake fails in HTTP proxy
When using an HTTP proxy to connect to HTTPS servers, if the TLS handshake fails after CONNECT succeeds, the connection remains in ACTIVE state and never gets cleaned up, occupying the connection pool forever. This fix ensures the connection is properly closed when TLS fails, preventing Pool timeout issues.
1 parent 10a6582 commit 204ec92

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

httpcore/_async/http_proxy.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,15 @@ async def handle_async_request(self, request: Request) -> Response:
312312
"server_hostname": self._remote_origin.host.decode("ascii"),
313313
"timeout": timeout,
314314
}
315-
async with Trace("start_tls", logger, request, kwargs) as trace:
316-
stream = await stream.start_tls(**kwargs)
317-
trace.return_value = stream
315+
try:
316+
async with Trace("start_tls", logger, request, kwargs) as trace:
317+
stream = await stream.start_tls(**kwargs)
318+
trace.return_value = stream
319+
except Exception:
320+
# Close the underlying connection when TLS handshake fails to avoid
321+
# zombie connections occupying the connection pool
322+
await self._connection.aclose()
323+
raise
318324

319325
# Determine if we should be using HTTP/1.1 or HTTP/2
320326
ssl_object = stream.get_extra_info("ssl_object")

0 commit comments

Comments
 (0)