Move HTTP/2 stream events cleanup inside state lock in _response_closed#1062
Open
bysiber wants to merge 1 commit intoencode:masterfrom
Open
Move HTTP/2 stream events cleanup inside state lock in _response_closed#1062bysiber wants to merge 1 commit intoencode:masterfrom
bysiber wants to merge 1 commit intoencode:masterfrom
Conversation
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.
In
_response_closed, thedel self._events[stream_id]happens outside the_state_lock, but thenot self._eventscheck that decides whether to transition to IDLE happens inside the lock. This creates a race window with multiplexed HTTP/2 requests:_response_closedruns: releases semaphore, deletes events entry, yields before acquiring the lockhandle_async_request: acquires_state_lock, setsstate=ACTIVE, releases lockself._events[stream_id] = []yet_state_lock, seesnot self._eventsis True (B hasn't added its entry), setsstate=IDLEand_expire_atNow the connection is marked IDLE with an expiry timer while Request B is actively using it. The connection pool may see the connection as expired and close it mid-request, or evict it as an excess idle connection.
Moving the
del self._events[stream_id]inside the_state_lockensures the events dict modification and the IDLE-transition check are atomic.Applied to both async and sync implementations.