|
| 1 | +import asyncio |
1 | 2 | import enum
|
2 | 3 | import logging
|
3 | 4 | import time
|
@@ -401,6 +402,9 @@ async def _receive_remote_settings_change(self, event: h2.events.Event) -> None:
|
401 | 402 | await self._max_streams_semaphore.acquire()
|
402 | 403 | self._max_streams -= 1
|
403 | 404 |
|
| 405 | + async def _reset_steam(self, stream_id: int, error_code: int) -> None: |
| 406 | + self._h2_state.reset_stream(stream_id=stream_id, error_code=error_code) |
| 407 | + |
404 | 408 | async def _response_closed(self, stream_id: int) -> None:
|
405 | 409 | await self._max_streams_semaphore.release()
|
406 | 410 | del self._events[stream_id]
|
@@ -578,12 +582,18 @@ async def __aiter__(self) -> typing.AsyncIterator[bytes]:
|
578 | 582 | # we want to close the response (and possibly the connection)
|
579 | 583 | # before raising that exception.
|
580 | 584 | with AsyncShieldCancellation():
|
581 |
| - await self.aclose() |
| 585 | + # close the stream with cancel |
| 586 | + await self.aclose(cancel_stream=isinstance(exc, asyncio.exceptions.CancelledError)) |
582 | 587 | raise exc
|
583 | 588 |
|
584 |
| - async def aclose(self) -> None: |
| 589 | + async def aclose(self, cancel_stream: bool = False) -> None: |
585 | 590 | if not self._closed:
|
586 | 591 | self._closed = True
|
587 | 592 | kwargs = {"stream_id": self._stream_id}
|
588 | 593 | async with Trace("response_closed", logger, self._request, kwargs):
|
| 594 | + if cancel_stream: |
| 595 | + await self._connection._reset_steam( |
| 596 | + stream_id=self._stream_id, |
| 597 | + error_code=h2.settings.ErrorCodes.CANCEL, |
| 598 | + ) |
589 | 599 | await self._connection._response_closed(stream_id=self._stream_id)
|
0 commit comments