Require `truststore>=0.10.5` so concurrent first requests through the default `httpx2` client no longer crash

Merged
MP
10d ago
+74-6

The default httpx2 client verifies TLS with a truststore.SSLContext. Because that isn't a stdlib ssl.SSLContext, anyio runs its wrap_bio in a worker thread, and truststore before 0.10.5 reloaded the CA certificates into the shared context there without a lock (wrap_socket had one, wrap_bio didn't). Concurrent first requests on a cold pool could then configure one context from several threads at once, which is the SIGABRT / realloc(): invalid old size crash in the issue. truststore 0.10.5 takes the context lock in wrap_bio too.

Needs Aditya's review (core code, outside pydantic_ai_harness).

Changes:

  • pydantic-ai-slim now requires truststore>=0.10.5 (not on Emscripten, matching httpx2's own marker), with a comment giving the reason, and uv.lock and the gh-aw runner's .github/scripts/pydantic-ai-runner.lock move to 0.10.5.
  • New regression test test_httpx2_client_concurrent_first_requests_configure_tls_one_at_a_time fires 4 concurrent first requests through create_async_httpx2_client() at a local listener, and records how many TLS context configurations overlap. It fails on truststore 0.10.4 (assert 4 == 1) and passes on 0.10.5. It runs in about 0.25s and also passes on Trio.

Choices:

  • I raised the dependency floor instead of adding our own lock or a stdlib ssl.create_default_context() default. The upstream release already fixes the race for every httpx2 client, including ones users build themselves. It needs no new code in _http.py and keeps truststore's OS trust store behavior (corporate CAs on macOS/Windows), which docs/install.md promises.
  • The test patches truststore's private _configure_context to observe the overlap, because the race happens in TLS setup below HTTPX and a VCR recording can't show it. A real crash would kill the pytest process.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

Summary by cubic

Fixes concurrent first requests through the default httpx2 client crashing with a SIGABRT on cold connection pools. The crash came from truststore before 0.10.5, which configured the shared TLS context without a lock inside wrap_bio (only wrap_socket was locked). This PR raises the dependency floor to truststore>=0.10.5 (excluding Emscripten, matching httpx2's own marker) so the upstream fix applies to every httpx2 client without new code or behavior changes.

Changes

  • pydantic-ai-slim now requires truststore>=0.10.5, with a comment explaining why, and uv.lock and the gh-aw runner lock move to 0.10.5.
  • New regression test test_httpx2_client_concurrent_first_requests_configure_tls_one_at_a_time fires 4 concurrent first requests against a local listener and asserts the shared TLS context is never configured by more than one thread at a time. It fails on truststore 0.10.4 and passes on 0.10.5.
  • Chose the dependency floor over adding our own lock or a stdlib ssl.create_default_context() default: the upstream release fixes the race for every httpx2 client, keeps truststore's OS trust store behavior, and needs no new code in _http.py.
  • The test patches truststore's private _configure_context to observe the overlap, since the race happens below HTTPX and a real crash would kill the pytest process.