Skip to content

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

Closed
MarkLux wants to merge 7 commits into
encode:masterfrom
MarkLux:fix/h2_connection_reset
Closed

fix(h2_connection_reset): add stream reset when client cancel the req…#944
MarkLux wants to merge 7 commits into
encode:masterfrom
MarkLux:fix/h2_connection_reset

Conversation

@MarkLux

Copy link
Copy Markdown

Summary

add the missing connection reset of h2 connection when client cancel the request.

fix#943

in discussion with #941

@MarkLux
MarkLuxforce-pushed the fix/h2_connection_reset branch from 7455935 to 9924db4CompareAugust 20, 2024 03:28
Comment threadhttpcore/_async/http2.py Outdated

@lovelydinosaurlovelydinosaur left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, seems like a good start. Make sure to run scripts/unasync.py to also apply the changes to the httpcore/_sync/http2.py module.

https://github.com/encode/httpcore/actions/runs/10464774808/job/28978912844#step:5:26

(Related... maybe we could change the build logs when unasync --check fails, so that there's clearer feedback to the developer.)

@MarkLux
MarkLuxforce-pushed the fix/h2_connection_reset branch 4 times, most recently from 46031e9 to 5997901CompareSeptember 4, 2024 06:41
Comment threadhttpcore/_async/http2.py Outdated
@MarkLux
MarkLuxforce-pushed the fix/h2_connection_reset branch 3 times, most recently from af2e7e4 to c061e4fCompareSeptember 4, 2024 08:15
@MarkLux
MarkLuxforce-pushed the fix/h2_connection_reset branch from bd4f039 to 18422ffCompareSeptember 4, 2024 09:45
@MarkLux

Copy link
Copy Markdown
Author

sorry to bother but we are really in a hurry to fix this one, could you have a review again? @tomchristie

@MarkLux

Copy link
Copy Markdown
Author

langchain-ai#5

approved by langchain community now, shall we have any further progress on this one? @tomchristie

Comment on lines +578 to +579
# need send cancel frame when the exception is not from remote peer.
if not isinstance(exc, RemoteProtocolError):

Copy link
Copy Markdown
Contributor

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:
yieldchunkexceptExceptionasexc:
self._connection._reset_steam(
stream_id=self._stream_id,
error_code=h2.settings.ErrorCodes.CANCEL,
)
raiseexc

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

BaseException covers asyncio.CancelledError as well, which should also reset the stream.

@lovelydinosaur

Copy link
Copy Markdown
Contributor

approved by langchain community now, shall we have any further progress on this one?

Thanks @MarkLux - apologies for the delay it's a bit of a complex one to review.

Thoughts on my review comment?

@stale

staleBot commented Apr 27, 2025

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stalestaleBot added the wontfix This will not be worked on label Apr 27, 2025
@stalestaleBot closed this Jun 27, 2025
@mabrowning

Copy link
Copy Markdown

This PR or something like it is still needed. Even with the latest version of httpcore, if the client closes the response before the server is done, the client does not send any RST_STREAM frames; the server will continue to generate and stream data and the client will ignore it.

Comment on lines +400 to +401
def _reset_steam(self, stream_id: int, error_code: int) -> None:
self._h2_state.reset_stream(stream_id=stream_id, error_code=error_code)

@mabrowningmabrowningJan 9, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

After updating the local h2_state, we should write the updated bytes on the wire, otherwise its reliant on some (possibly much) later write (like initiating a new request or a keep-alive frame) sending the reset.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wontfixThis will not be worked on

Development

Successfully merging this pull request may close these issues.

Missing client stream reset in h2 connection

3 participants

@MarkLux@lovelydinosaur@mabrowning