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).

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

1 participant