Skip to content

Commit 30789fc

Browse files
committed
Fix sync version: Add try-except for TLS handshake failure
Apply the same fix to the synchronous version to keep async/sync in sync.
1 parent 204ec92 commit 30789fc

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

httpcore/_sync/http_proxy.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,15 @@ def handle_request(self, request: Request) -> Response:
307307
alpn_protocols = ["http/1.1", "h2"] if self._http2 else ["http/1.1"]
308308
ssl_context.set_alpn_protocols(alpn_protocols)
309309

310-
kwargs = {
311-
"ssl_context": ssl_context,
312-
"server_hostname": self._remote_origin.host.decode("ascii"),
313-
"timeout": timeout,
314-
}
315-
with Trace("start_tls", logger, request, kwargs) as trace:
316-
stream = stream.start_tls(**kwargs)
317-
trace.return_value = stream
310+
try:
311+
with Trace("start_tls", logger, request, kwargs) as trace:
312+
stream = stream.start_tls(**kwargs)
313+
trace.return_value = stream
314+
except Exception:
315+
# Close the underlying connection when TLS handshake fails to avoid
316+
# zombie connections occupying the connection pool
317+
self._connection.close()
318+
raise
318319

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

0 commit comments

Comments
 (0)