Skip to content

Commit 18422ff

Browse files
committed
fix(h2_connection_reset): add stream reset when client cancel the request
1 parent 7d87c9d commit 18422ff

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

httpcore/_async/http2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ async def _receive_remote_settings_change(self, event: h2.events.Event) -> None:
401401
await self._max_streams_semaphore.acquire()
402402
self._max_streams -= 1
403403

404+
async def _reset_steam(self, stream_id: int, error_code: int) -> None:
405+
self._h2_state.reset_stream(stream_id=stream_id, error_code=error_code)
406+
404407
async def _response_closed(self, stream_id: int) -> None:
405408
await self._max_streams_semaphore.release()
406409
del self._events[stream_id]
@@ -578,6 +581,12 @@ async def __aiter__(self) -> typing.AsyncIterator[bytes]:
578581
# we want to close the response (and possibly the connection)
579582
# before raising that exception.
580583
with AsyncShieldCancellation():
584+
# need send cancel frame when the exception is not from remote peer.
585+
if not isinstance(exc, RemoteProtocolError):
586+
await self._connection._reset_steam(
587+
stream_id=self._stream_id,
588+
error_code=h2.settings.ErrorCodes.CANCEL,
589+
)
581590
await self.aclose()
582591
raise exc
583592

httpcore/_sync/http2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ def _receive_remote_settings_change(self, event: h2.events.Event) -> None:
401401
self._max_streams_semaphore.acquire()
402402
self._max_streams -= 1
403403

404+
def _reset_steam(self, stream_id: int, error_code: int) -> None:
405+
self._h2_state.reset_stream(stream_id=stream_id, error_code=error_code)
406+
404407
def _response_closed(self, stream_id: int) -> None:
405408
self._max_streams_semaphore.release()
406409
del self._events[stream_id]
@@ -578,6 +581,12 @@ def __iter__(self) -> typing.Iterator[bytes]:
578581
# we want to close the response (and possibly the connection)
579582
# before raising that exception.
580583
with ShieldCancellation():
584+
# need send cancel frame when the exception is not from remote peer.
585+
if not isinstance(exc, RemoteProtocolError):
586+
self._connection._reset_steam(
587+
stream_id=self._stream_id,
588+
error_code=h2.settings.ErrorCodes.CANCEL,
589+
)
581590
self.close()
582591
raise exc
583592

0 commit comments

Comments
 (0)