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

fix(h2_connection_reset): add stream reset when client cancel the req… #944

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
9 changes: 9 additions & 0 deletions httpcore/_async/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ async def _receive_remote_settings_change(self, event: h2.events.Event) -> None:
await self._max_streams_semaphore.acquire()
self._max_streams -= 1

async def _reset_steam(self, stream_id: int, error_code: int) -> None:
self._h2_state.reset_stream(stream_id=stream_id, error_code=error_code)

async def _response_closed(self, stream_id: int) -> None:
await self._max_streams_semaphore.release()
del self._events[stream_id]
Expand Down Expand Up @@ -572,6 +575,12 @@ async def __aiter__(self) -> typing.AsyncIterator[bytes]:
# we want to close the response (and possibly the connection)
# before raising that exception.
with AsyncShieldCancellation():
# need send cancel frame when the exception is not from remote peer.
if not isinstance(exc, RemoteProtocolError):
await self._connection._reset_steam(
stream_id=self._stream_id,
error_code=h2.settings.ErrorCodes.CANCEL,
)
await self.aclose()
raise exc

Expand Down
9 changes: 9 additions & 0 deletions httpcore/_sync/http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ def _receive_remote_settings_change(self, event: h2.events.Event) -> None:
self._max_streams_semaphore.acquire()
self._max_streams -= 1

def _reset_steam(self, stream_id: int, error_code: int) -> None:
self._h2_state.reset_stream(stream_id=stream_id, error_code=error_code)

def _response_closed(self, stream_id: int) -> None:
self._max_streams_semaphore.release()
del self._events[stream_id]
Expand Down Expand Up @@ -572,6 +575,12 @@ def __iter__(self) -> typing.Iterator[bytes]:
# we want to close the response (and possibly the connection)
# before raising that exception.
with ShieldCancellation():
# need send cancel frame when the exception is not from remote peer.
if not isinstance(exc, RemoteProtocolError):
Comment on lines +578 to +579
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need send cancel frame when the exception is not from remote peer.

It's not obvious to me that not isinstance(exc, RemoteProtocolError) is correct/sufficient here.

Should this instead be something like just wrapping the yield?...

try:
    yield chunk
except Exception as exc:
    self._connection._reset_steam(
        stream_id=self._stream_id,
        error_code=h2.settings.ErrorCodes.CANCEL,
    )
    raise exc

self._connection._reset_steam(
stream_id=self._stream_id,
error_code=h2.settings.ErrorCodes.CANCEL,
)
self.close()
raise exc

Expand Down
Loading