httpcore icon indicating copy to clipboard operation
httpcore copied to clipboard

Release HTTP/2 semaphore permit on NoAvailableStreamIDError

Open bysiber opened this issue 9 months ago • 0 comments

In the HTTP/2 implementation, handle_async_request / handle_request acquires a _max_streams_semaphore permit before getting a stream ID. If get_next_available_stream_id() raises NoAvailableStreamIDError, the exception handler correctly sets _used_all_stream_ids and decrements the request count, but it doesn't release the semaphore permit that was just acquired:

await self._max_streams_semaphore.acquire()      # permit consumed

try:
    stream_id = self._h2_state.get_next_available_stream_id()
    self._events[stream_id] = []
except h2.exceptions.NoAvailableStreamIDError:
    self._used_all_stream_ids = True
    self._request_count -= 1
    raise ConnectionNotAvailable()                # permit not released

This permanently leaks one semaphore permit. While the practical impact is limited since _used_all_stream_ids = True prevents further requests on this connection, it's still incorrect resource cleanup.

This adds the missing release() call in both the async and sync implementations.

bysiber avatar Feb 20 '26 03:02 bysiber