Conversation
mgLTF
force-pushed
the
fix/reuse-tls-context
branch
from
September 24, 2026 19:38
7e4aee0 to
504d16b
Compare
mgLTF
marked this pull request as ready for review
September 24, 2026 20:18
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.
Summary of change
Core requests and third-party token/user-info/signing-key requests currently construct an HTTPX client for each request. Each default client rebuilds TLS configuration and reloads trusted certificates, including when the destination is a plain-HTTP Core.
Reuse a lazily created, verified SSL context across these requests. HTTPX's public
create_ssl_context(verify=True, trust_env=True)retains the installed version's TLS defaults. The single-entry cache responds to changes inSSL_CERT_FILE,SSL_CERT_DIR, andSSLKEYLOGFILE, and is cleared by the test reset helper. Clients retain their existing per-request lifetimes and cleanup.Replacing certificate contents at the same path requires a worker restart/cache reset. In particular, a removed trusted CA can remain trusted by an existing worker until then; operators who rotate trust bundles in place need to restart workers. Concurrent cold calls may construct separate contexts; subsequent calls reuse the cached result. This does not introduce connection pooling, a public configuration API, or a new dependency.
The included
scripts/benchmark_tls.pycompares existing behavior, this patch, and a client reused within one event loop, using real local HTTP/HTTPS connections. Warm request medians (60 measured requests after one cold request, concurrency 1):Both use HTTPX 0.28.1 and certifi plus a temporary local CA. Context creation falls from 61 to 1; connections remain at 61 for this patch, versus 1 for full client reuse. These are local microbenchmarks, not measurements of a live Google sign-in. Full client reuse offers further savings but needs async-runtime ownership, shutdown, and request-state isolation across the SDK's sync/async integrations.
Related issues
crypto/x509loads file-based system roots once per process, so replacing a CA bundle at the same path can also require a restart there. That behavior predates Go PR fix: JWT lifetime setting issue #438 and does not apply identically to every platform or custom client. This Python patch targets the separately measured TLS setup cost and also covers third-party requests.Test Plan
make dev-install docker compose up --wait # dedicated local test instances only python -m pytest -q tests/test_ssl_utils.py python -m pytest -q tests --junit-xml=test-results/junit.xml python scripts/benchmark_tls.py make linttests/sessions/test_jwks.py::test_that_jwks_are_refresh_if_kid_is_unknown; it also fails on untouched upstream4bfa86337d1f0d49ebca6f8453dd6c373aaec04dagainst the same Docker Core.tests/webauthn/soft_authenticator.py:57, reproduced on untouched upstream. No added type errors..changes/v0.31.2.md,.changes/v0.31.3.md,tests/oauth2provider/test_frontend_redirect.py, andtests/webauthn/test_sign_in_double_verify.py. Their automatic edits are excluded from this PR.Documentation changes
Added a reproducible benchmark and documented cache lifecycle in the internal helper. No public API documentation change is required.
Checklist for important updates
Fixedchangie fragment usingchangie new.dev, following the current dev-sync release workflow.run-tests.Remaining TODOs for this PR
action_requiredwith no jobs. An upstream maintainer needs to approve the fork workflow runs and applyrun-tests; the fork author cannot add that label (AddLabelsToLabelablepermission denied). After the jobs run, evaluate the existing test/lint failures independently of this change.