Skip to content

Configure the default Linux SSL context once instead of per handshake - #1253

Merged
Kludex merged 9 commits into
mainfrom
configure-linux-ssl-context-once
Oct 19, 2026
Merged

Kludex merged 9 commits into
mainfrom
configure-linux-ssl-context-once

Conversation

@Kludex

@Kludex Kludex commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Closes #1252.

On Linux, truststore.SSLContext calls set_default_verify_paths() on its shared inner context before every handshake. OpenSSL < 3.4 appends a duplicate CApath lookup on each call, so issuer lookups grow quadratically and CPU climbs over the lifetime of a reused client (sethmlarson/truststore#212).

On Linux, the default context is now a stdlib ssl.SSLContext configured once, with the same logic truststore uses there (its Linux backend delegates verification to OpenSSL anyway). Windows and macOS still use truststore. httpx2.create_ssl_context() now uses httpcore2.default_ssl_context() instead of duplicating it, passing trust_env through a private _trust_env keyword. With trust_env=False, SSL_CERT_FILE/SSL_CERT_DIR are ignored. The private keyword goes away together with the Linux branch.

Measured on Debian bookworm (OpenSSL 3.0.22), against a server with an intermediate chain:

before after
CPU per request, 4 batches of 100 23 → 27 → 32 → 36 ms 1.4 ms, flat
/usr/lib/ssl/certs opens for 5 / 20 / 50 requests 20 / 230 / 1325 6 / 21 / 51

Verification is still enforced: an untrusted chain fails with CERTIFICATE_VERIFY_FAILED, and system CAs still validate public hosts.

This can be reverted once sethmlarson/truststore#213 is released.

AI Disclaimer

This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.

View guided diff

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-08T12:32:44.254418Z 478e923 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@codspeed

codspeed Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 17 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing configure-linux-ssl-context-once (5606fb4) with main (53c11c4)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 478e923fe9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/httpcore2/httpcore2/_ssl.py

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread src/httpcore2/httpcore2/_ssl.py Outdated
Comment thread src/httpx2/httpx2/_config.py Outdated
Comment thread src/httpcore2/httpcore2/_ssl.py Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread src/httpcore2/httpcore2/_ssl.py Outdated
Comment thread src/httpcore2/httpcore2/_ssl.py Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 3 files (changes from recent commits).

Heads up: you’re close to your included review allowance. Set a flex budget so reviews don’t pause.

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread src/httpcore2/httpcore2/_ssl.py Outdated
@Kludex
Kludex merged commit 7715bf0 into main Oct 19, 2026
19 checks passed
@Kludex
Kludex deleted the configure-linux-ssl-context-once branch October 19, 2026 20:09
@Kludex Kludex mentioned this pull request Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CPU keeps climbing in reused async clients during TLS verification on OpenSSL 3.0

1 participant