Skip to content

Commit

Permalink
Safe async cancellations. (#880)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tomchristie authored Feb 12, 2024
1 parent 79fa6bf commit 7b04cda
Show file tree
Hide file tree
Showing 9 changed files with 512 additions and 379 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased

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

Expand Down
16 changes: 7 additions & 9 deletions httpcore/_async/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

async with self._request_lock:
if self._connection is None:
try:
try:
async with self._request_lock:
if self._connection is None:
stream = await self._connect(request)

ssl_object = stream.get_extra_info("ssl_object")
Expand All @@ -94,11 +94,9 @@ async def handle_async_request(self, request: Request) -> Response:
stream=stream,
keepalive_expiry=self._keepalive_expiry,
)
except Exception as exc:
self._connect_failed = True
raise exc
elif not self._connection.is_available():
raise ConnectionNotAvailable()
except BaseException as exc:
self._connect_failed = True
raise exc

return await self._connection.handle_async_request(request)

Expand Down
Loading

0 comments on commit 7b04cda

Please sign in to comment.