Coalesce overlapping native TLS root certificate loads (#54056)

## Why

Concurrent TLS client builds can independently load the same platform trust store. Share overlapping loads while preserving fresh trust reads for later builds.

## What changed

- Share one in-flight native root load without reusing completed loads.
- Bypass sharing when `SSL_CERT_FILE` or `SSL_CERT_DIR` is set, and continue reading custom CA bundles independently for every build.

## Testing

Add tests for overlapping loads, fresh trust after completion, and recovery from a panicking loader. Extend TLS handshake coverage to verify that replacing a custom CA file affects new configurations while existing configurations retain their trust, and that empty or removed files produce errors.

GitOrigin-RevId: 1465ef3faf82f53651d9dbfe314341b17346580b
This commit is contained in:
jif
2026-10-14 17:56:24 +00:00
committed by copyberry
parent 2b21129c6b
commit 80ad49e3b7
5 changed files with 153 additions and 16 deletions
+3 -11
View File
@@ -6,6 +6,8 @@
//! `reqwest::ClientBuilder` or rustls client config, layer in custom CA support, and either get
//! back a configured transport or a user-facing error that explains how to fix a misconfigured CA
//! bundle.
//! Concurrent native-root reads may share one in-flight load; later builds reload trust, and
//! custom CA files are read independently for every build.
//!
//! The module intentionally has a narrow responsibility:
//!
@@ -49,7 +51,6 @@ use std::sync::Arc;
use codex_utils_rustls_provider::ensure_rustls_crypto_provider;
use rustls::ClientConfig;
use rustls::RootCertStore;
use rustls_pki_types::CertificateDer;
use rustls_pki_types::pem::PemObject;
use rustls_pki_types::pem::SectionKind;
@@ -247,16 +248,7 @@ fn build_rustls_client_config(
// Start from the platform roots so websocket callers keep the same baseline trust behavior
// they would get from tungstenite's default rustls connector, then layer in the Codex custom
// CA bundle on top when configured.
let mut root_store = RootCertStore::empty();
let rustls_native_certs::CertificateResult { certs, errors, .. } =
rustls_native_certs::load_native_certs();
if !errors.is_empty() {
warn!(
native_root_error_count = errors.len(),
"encountered errors while loading native root certificates"
);
}
let _ = root_store.add_parsable_certificates(certs);
let mut root_store = (*crate::native_roots::load()).clone();
if let Some(bundle) = bundle {
let certificates = bundle.load_certificates()?;