|
17 | 17 | RemoteProtocolError, |
18 | 18 | ) |
19 | 19 | from .._models import Origin, Request, Response |
20 | | -from .._synchronization import AsyncLock, AsyncSemaphore, AsyncShieldCancellation |
| 20 | +from .._synchronization import AsyncLock, AsyncSemaphore, async_cancel_shield |
21 | 21 | from .._trace import Trace |
22 | 22 | from .interfaces import AsyncConnectionInterface |
23 | 23 |
|
@@ -108,8 +108,7 @@ async def handle_async_request(self, request: Request) -> Response: |
108 | 108 | async with Trace("send_connection_init", logger, request, kwargs): |
109 | 109 | await self._send_connection_init(**kwargs) |
110 | 110 | except BaseException as exc: |
111 | | - with AsyncShieldCancellation(): |
112 | | - await self.aclose() |
| 111 | + await async_cancel_shield(self.aclose) |
113 | 112 | raise exc |
114 | 113 |
|
115 | 114 | self._sent_connection_init = True |
@@ -160,11 +159,12 @@ async def handle_async_request(self, request: Request) -> Response: |
160 | 159 | "stream_id": stream_id, |
161 | 160 | }, |
162 | 161 | ) |
163 | | - except BaseException as exc: # noqa: PIE786 |
164 | | - with AsyncShieldCancellation(): |
165 | | - kwargs = {"stream_id": stream_id} |
166 | | - async with Trace("response_closed", logger, request, kwargs): |
167 | | - await self._response_closed(stream_id=stream_id) |
| 162 | + except BaseException as exc: |
| 163 | + kwargs = {"stream_id": stream_id} |
| 164 | + async with Trace("response_closed", logger, request, kwargs): |
| 165 | + await async_cancel_shield( |
| 166 | + lambda: self._response_closed(stream_id=stream_id) |
| 167 | + ) |
168 | 168 |
|
169 | 169 | if isinstance(exc, h2.exceptions.ProtocolError): |
170 | 170 | # One case where h2 can raise a protocol error is when a |
@@ -577,8 +577,7 @@ async def __aiter__(self) -> typing.AsyncIterator[bytes]: |
577 | 577 | # If we get an exception while streaming the response, |
578 | 578 | # we want to close the response (and possibly the connection) |
579 | 579 | # before raising that exception. |
580 | | - with AsyncShieldCancellation(): |
581 | | - await self.aclose() |
| 580 | + await async_cancel_shield(self.aclose) |
582 | 581 | raise exc |
583 | 582 |
|
584 | 583 | async def aclose(self) -> None: |
|
0 commit comments