Skip to content

Fix thread-safety of shared synchronous HTTP/2 clients - #3895

Open
keicodex wants to merge 1 commit into
encode:masterfrom
keicodex:contrib/3633-sync-http2-thread-safety
Open

keicodex wants to merge 1 commit into
encode:masterfrom
keicodex:contrib/3633-sync-http2-thread-safety

Conversation

@keicodex

@keicodex keicodex commented Dec 5, 2026

Copy link
Copy Markdown

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:

RuntimeError: deque mutated during iteration

(from hpack's HeaderTable.search), or as companion failures such as KeyError and LocalProtocolError from 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_request until the response stream is exhausted or closed. A request that lands on a busy connection raises httpcore.ConnectionNotAvailable, the same contract HTTP/1.1 connections already use, so the pool assigns it to another connection (bounded by the configured Limits) or queues it. The wrapper is applied through create_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 the h2 package:

  • 12 threads × 50 requests through one shared client, with unique per-request headers churning the HPACK table and a tightened GIL switch interval: on unpatched code this fails within the first few concurrent requests with the errors above; with the fix all 600 requests succeed.
  • A second request issued while a streaming response is held open is served by a separate connection.
  • Sequential requests still reuse a single connection.

Full test suite with the fix: 1416 passed. The only 4 failures (test_proxies.py proxy-close ×3, test_main.py::test_download) fail identically on unpatched master, so they are pre-existing and unrelated.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

BUG: RuntimeError: deque mutated during iteration

1 participant