Skip to content

Commit 7b04cda

Browse files
authored
Safe async cancellations. (#880)
* Connection pool work * Connection pool work * Connection pool work * Connection pool work * Comments * Comments * Connection pool work * Reraise * Lookin sharp * nocover directive * Safe cancellations * Update CHANGELOG
1 parent 79fa6bf commit 7b04cda

9 files changed

+512
-379
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

77
## Unreleased
88

9+
- Fix support for async cancellations. (#880)
910
- Fix trace extension when used with socks proxy. (#849)
1011
- Fix SSL context for connections using the "wss" scheme (#869)
1112

httpcore/_async/connection.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from .._backends.auto import AutoBackend
88
from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend, AsyncNetworkStream
9-
from .._exceptions import ConnectError, ConnectionNotAvailable, ConnectTimeout
9+
from .._exceptions import ConnectError, ConnectTimeout
1010
from .._models import Origin, Request, Response
1111
from .._ssl import default_ssl_context
1212
from .._synchronization import AsyncLock
@@ -70,9 +70,9 @@ async def handle_async_request(self, request: Request) -> Response:
7070
f"Attempted to send request to {request.url.origin} on connection to {self._origin}"
7171
)
7272

73-
async with self._request_lock:
74-
if self._connection is None:
75-
try:
73+
try:
74+
async with self._request_lock:
75+
if self._connection is None:
7676
stream = await self._connect(request)
7777

7878
ssl_object = stream.get_extra_info("ssl_object")
@@ -94,11 +94,9 @@ async def handle_async_request(self, request: Request) -> Response:
9494
stream=stream,
9595
keepalive_expiry=self._keepalive_expiry,
9696
)
97-
except Exception as exc:
98-
self._connect_failed = True
99-
raise exc
100-
elif not self._connection.is_available():
101-
raise ConnectionNotAvailable()
97+
except BaseException as exc:
98+
self._connect_failed = True
99+
raise exc
102100

103101
return await self._connection.handle_async_request(request)
104102

0 commit comments

Comments
 (0)