Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safe async cancellations. #880

Merged
merged 14 commits into from
Feb 12, 2024
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
364 changes: 187 additions & 177 deletions httpcore/_async/connection_pool.py

Large diffs are not rendered by default.

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

from .._backends.sync import SyncBackend
from .._backends.base import SOCKET_OPTION, NetworkBackend, NetworkStream
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 Lock
Expand Down Expand Up @@ -70,9 +70,9 @@ def handle_request(self, request: Request) -> Response:
f"Attempted to send request to {request.url.origin} on connection to {self._origin}"
)

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

ssl_object = stream.get_extra_info("ssl_object")
Expand All @@ -94,11 +94,9 @@ def handle_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 self._connection.handle_request(request)

Expand Down
Loading
Loading