Release HTTP/2 semaphore permit on NoAvailableStreamIDError#1012
Merged
Kludex merged 1 commit intoJun 2, 2026
Merged
Conversation
`handle_async_request` / `handle_request` acquires a permit from `_max_streams_semaphore` before calling `_h2_state.get_next_available_stream_id()`. If the stream-ID space is exhausted and `NoAvailableStreamIDError` is raised, the exception handler sets `_used_all_stream_ids` and decrements `_request_count` but never releases the permit — leaking it permanently. In practice `_used_all_stream_ids = True` prevents further requests on the connection, so the visible impact is limited, but the resource cleanup is still wrong. Add the missing `release()` call in both async and sync paths. Ports encode/httpcore#1061 (bysiber), via codeberg.org/httpxyz/httpcorexyz@e88f30b (first of two fixes bundled in that commit; the second lands in a separate PR). Co-Authored-By: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com>
Merging this PR will not alter performance
Comparing Footnotes
|
mbeijen
added a commit
to mbeijen/httpx2
that referenced
this pull request
Jun 2, 2026
…om fork's e88f30b)
Kludex
approved these changes
Jun 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AsyncHTTP2Connection.handle_async_requestacquires a permit from_max_streams_semaphorebefore callingself._h2_state.get_next_available_stream_id(). If the stream-ID space is exhausted andNoAvailableStreamIDErroris raised, the exception handler sets_used_all_stream_ids = Trueand decrements_request_countbut never releases the permit — leaking it permanently.In practice
_used_all_stream_ids = Trueprevents further requests on the same connection so the visible impact is limited, but the resource cleanup is still wrong.This PR adds the missing
release()call in both the async and sync paths.Ports encode/httpcore#1061 (bysiber, currently open upstream), via codeberg.org/httpxyz/httpcorexyz@e88f30b. The fork bundled this fix with encode/httpcore#1062; I'm splitting them into two httpx2 PRs to mirror the upstream split.
Notes
_async/http2.pywas edited by hand;_sync/http2.pywas regenerated byscripts/unasync.py.NoAvailableStreamIDErrorbranch is already marked# pragma: no coverbecause exhausting the H/2 client stream-ID space (≈ 2³⁰ requests) is impractical to exercise in a unit test. The upstream PR doesn't ship a test either. 100% coverage preserved.Note: this change was prepared with AI assistance (Claude Code).