Skip to content

Cache default SSLContext in create_ssl_context() - #3863

Open
Burakceliks wants to merge 1 commit into
encode:masterfrom
Burakceliks:perf/cache-default-ssl-context
Open

Burakceliks wants to merge 1 commit into
encode:masterfrom
Burakceliks:perf/cache-default-ssl-context

Conversation

@Burakceliks

Copy link
Copy Markdown

Fixes #3734

Summary

Caches the default ssl.SSLContext created by create_ssl_context() via functools.lru_cache when verify=True and cert=None, avoiding repeated OpenSSL CA bundle allocations across clients/requests.

  • Keyed on (cafile, capath, keylog_file) so SSL_CERT_FILE, SSL_CERT_DIR, and SSLKEYLOGFILE environment overrides continue to work as expected.
  • Bypasses the cache when cert is provided so ctx.load_cert_chain(...) never mutates a shared context.
  • Added test_default_ssl_context_is_cached in tests/test_config.py (29/29 passed, ruff check passed).

@ege-arhan ege-arhan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caching the default SSLContext makes it a shared mutable object, and httpcore mutates it per connection: ssl_context.set_alpn_protocols(alpn_protocols) in httpcore/_sync/connection.py:147 (and the proxy variants), where the list depends on the client's http2 flag. With the cache on httpx/_config.py:24, every Client with verify=True, cert=None now gets the same object, so whichever client connected last decides the ALPN list for all of them.

Repro against this branch, SSL_CERT_FILE pointing at a test CA:

cached = httpx.create_ssl_context()
cached.set_alpn_protocols(["h2"])        # Client(http2=True)
cached.set_alpn_protocols(["http/1.1"])  # a plain Client() connects, httpcore rewrites ALPN
# http2=True client's next handshake now offers only http/1.1

Against an h2-only server the handshake comes back with selected_alpn_protocol() is None, so the http2 client silently downgrades or fails to negotiate; on main the same client negotiates h2. I hit this with two clients in one process, but it is also a race for concurrent clients since the mutation happens on each connection.

The cache is still a reasonable idea, but the value cannot be the object handed to httpcore. Either cache the inputs (the loaded CA bundle) and build a fresh context per client, or have the transport copy: ssl.SSLContext has no copy(), so ssl.create_default_context(cafile=...) per client with a cached certifi path is the simple version. If you keep sharing, ALPN has to stop being set on the shared object, which means a change in httpcore, not here.

Also note test_default_ssl_context_is_cached asserts the identity that causes this, so it would need to flip to asserting the per-client isolation.

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.

The create_ssl_context() call can consume a lot of memory

2 participants