-
Notifications
You must be signed in to change notification settings - Fork 143
fix: handle http2 goaway race conditions #1054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bayoumi17m
wants to merge
15
commits into
encode:master
Choose a base branch
from
bayoumi17m:fix/http2-race-condition
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+2,494
−28
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
…away race conditions
…ttp2 goaway race conditions
Author
|
All checks are passing and I'm happy to make any changes if needed! I've been using this example to reproduce and confirm the resolution: |
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.
Fix HTTP/2 GOAWAY Race Conditions
Problem
When using HTTPX with HTTP/2 under high concurrency, users encounter errors where connections are closed for streams beyond the
last_stream_id. This forces applications to implement retry logic for what should be transparent library behavior.The root cause is a set of race conditions between h2's state machine and httpcore's event processing:
RemoteProtocolError("Server disconnected")with no retry opportunityConnectionNotAvailabledidn't convey why the connection was unavailable, preventing informed retry decisionsA user proposed changing
>to>=in the stream ID comparison (discussion #1041), but this would violate RFC 7540 Section 6.8:A stream matching
last_stream_idmay have already been processed, making automatic retry unsafe for non-idempotent operations.Solution
This PR introduces proper GOAWAY handling that maintains RFC compliance while enabling safe automatic retries:
1. New
ConnectionGoingAwayExceptionA dedicated exception (subclass of
ConnectionNotAvailable) that carries GOAWAY context:last_stream_id/request_stream_idfor RFC 7540 stream comparisonerror_codeto distinguish graceful shutdown (NO_ERROR) from errorsheaders_sent/body_sentto track request phase at time of GOAWAYis_safe_to_retry,is_graceful_shutdown,may_have_side_effectsThis exception is exported publicly, allowing applications to implement idempotency-aware retry logic when needed.
2. New
DRAININGConnection StateConnections receiving a graceful GOAWAY (NO_ERROR) transition to
DRAININGrather thanCLOSED:is_available() = False)This fits naturally into the existing
HTTPConnectionStateenum pattern and makes state transitions easier to reason about.3. Request Phase Tracking
Each stream tracks
headers_sentandbody_sentto determine potential side effects:This information flows through
ConnectionGoingAway.may_have_side_effectsfor retry decisions.4. Connection Pool Retry Logic
The connection pool now handles
ConnectionGoingAwaywith RFC-compliant retry semantics:stream_id > last_stream_idRemoteProtocolErrorwith context for application decisionBehavior Summary
ConnectionNotAvailable(retry worked but was implicit)RemoteProtocolErrororConnectionNotAvailableRemoteProtocolErrorwith cause chain toConnectionGoingAwayConnectionGoingAwaywith conservative assumptionsRemoteProtocolError("Server disconnected")ConnectionGoingAwaywith retry contextDesign Decisions
Why a new connection state vs. a boolean flag?
The
DRAININGstate fits the existingHTTPConnectionStatepattern and makes state transitions explicit. A boolean would work but adds cognitive overhead when reasoning about valid state combinations.Why track headers and body separately?
Both are relevant for side-effect determination. Applications can use this granular information - for example, a read-only GET that sent headers but no body might be safer to retry than a POST with body sent.
Why raise
RemoteProtocolErrorinstead ofConnectionGoingAwayfor potentially-processed requests?Balances API simplicity with power. Most users catch
RemoteProtocolError; advanced users can inspect__cause__to access the fullConnectionGoingAwaycontext. SinceConnectionGoingAwayextendsConnectionNotAvailable, it's also catchable directly for those who need it.Why conservative assumptions when h2 is CLOSED without GOAWAY event?
When the connection is closed but we haven't processed a GOAWAY event, we can't know the true
last_stream_id. Assuming the current stream may have been processed is the safe default - better to surface an error than silently retry a potentially-processed request.Related Issues
Test Plan
ConnectionGoingAwayexception propertiesChecklist