Repository navigation
Conversation
A synchronous Client with http2=True shared between threads could fail with RuntimeError: deque mutated during iteration from the HPACK encoder, or companion errors such as KeyError and LocalProtocolError from the h2 state machine. httpcore multiplexes concurrent requests onto a single HTTP/2 connection, but that connection's state is not thread-safe, and unlike HTTP/1.1 connections it never refuses a concurrent request. The sync transport now wraps pooled connections so each serves one request at a time, from handle_request until the response stream closes, signalling httpcore.ConnectionNotAvailable while busy - the same contract HTTP/1.1 connections already use - so the pool assigns concurrent requests to separate connections, bounded by the configured limits. The async transport is unchanged. Fixes encode#3633.
This branch has not been deployed
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.
Fixes #3633.
Problem
Sharing one synchronous
httpx.Client(http2=True)between threads is unsafe. httpcore multiplexes concurrent requests onto a single HTTP/2 connection, but that connection's state (the h2 state machine and its HPACK encoder table) is not thread-safe, and unlike HTTP/1.1 connections an HTTP/2 connection never refuses a concurrent request. Two threads encoding headers at the same time corrupt the shared HPACK dynamic table, surfacing as:(from hpack's
HeaderTable.search), or as companion failures such asKeyErrorandLocalProtocolErrorfrom the h2 state machine. Thanks to @Shanmukh-l33t for the root-cause analysis on the issue.Fix
In the synchronous transport, when HTTP/2 is enabled, pooled connections are wrapped so that each connection serves at most one request at a time — from
handle_requestuntil the response stream is exhausted or closed. A request that lands on a busy connection raiseshttpcore.ConnectionNotAvailable, the same contract HTTP/1.1 connections already use, so the pool assigns it to another connection (bounded by the configuredLimits) or queues it. The wrapper is applied throughcreate_connection, so it covers the plain pool and the HTTP/SOCKS proxy pools alike. The async transport is unchanged: its requests interleave on a single event loop and are unaffected.Trade-off: concurrent requests from a shared sync client no longer multiplex over one HTTP/2 connection; they use separate connections instead, exactly like HTTP/1.1 today. Sequential requests still reuse a single connection (covered by a test below).
Tests
New
tests/client/test_thread_safety.py, using a minimal prior-knowledge HTTP/2 server built on theh2package:Full test suite with the fix: 1416 passed. The only 4 failures (
test_proxies.pyproxy-close ×3,test_main.py::test_download) fail identically on unpatched master, so they are pre-existing and unrelated.