httpcore icon indicating copy to clipboard operation
httpcore copied to clipboard

Ensure HTTP/2 connections are gracefully closed on async cancellation

Open karpetrosyan opened this issue 2 years ago • 4 comments

Closes #756

Checklist

  • [x] I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • [ ] I've updated the documentation accordingly.

TODO

  • [x] Add failing tests
  • [x] Gracefully handle cancellation for unstarted http/2 requests
  • [x] Update changelog

karpetrosyan avatar Jul 14 '23 08:07 karpetrosyan

could (async )with self._state_lock: move under _state_housekeeping function?

I agree, it's always used.

zanieb avatar Jul 15 '23 14:07 zanieb

At thanks! The failing test case is especially helpful here.

Pretty sure this resolution is overcomplicated, tho. I think we actually want here is?...

  1. This existing try...except needs to be expanded...

https://github.com/encode/httpcore/blob/21e47a7481fbad3719417899113e1435a340b752/httpcore/_async/http2.py#L106-L113

...so that it instead covers all of this block...

https://github.com/encode/httpcore/blob/21e47a7481fbad3719417899113e1435a340b752/httpcore/_async/http2.py#L106-L127

ie. the except case to move down to line 127 there.

  1. This call to acquire()...

https://github.com/encode/httpcore/blob/21e47a7481fbad3719417899113e1435a340b752/httpcore/_async/http2.py#L129

Should also be surrounded with...

try:
    await self._max_streams_semaphore.acquire()
 except BaseException as exc: 
     with AsyncShieldCancellation(): 
         await self.aclose() 
     raise exc

We could actually avoid (1) if we instead addressed the refactoring suggested in https://github.com/encode/httpcore/discussions/738 or refactored self._send_connection_init(**kwargs) so that our initial handshake waits for the settings response before we instantiate our max_streams_semaphore. But let's not be getting into that just now.

lovelydinosaur avatar Aug 01 '23 14:08 lovelydinosaur

Ugh. I need to think about this one more clearly, it's making my brain mush.

lovelydinosaur avatar Aug 01 '23 14:08 lovelydinosaur

I would recommend using this stupid solution for the time being and not waiting for http/2 refactoring from #770

karpetrosyan avatar Aug 08 '23 10:08 karpetrosyan

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.

stale[bot] avatar Apr 26 '25 04:04 stale[bot]