Ensure HTTP/2 connections are gracefully closed on async cancellation
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
could
(async )with self._state_lock:move under_state_housekeepingfunction?
I agree, it's always used.
At thanks! The failing test case is especially helpful here.
Pretty sure this resolution is overcomplicated, tho. I think we actually want here is?...
- This existing
try...exceptneeds 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.
- 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.
Ugh. I need to think about this one more clearly, it's making my brain mush.
I would recommend using this stupid solution for the time being and not waiting for http/2 refactoring from #770
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.