# Production Pitfall: Two CryptoProviders in rustls and a Runtime Panic

> Why a Rust server can start up fine and then crash on its very first TLS connection, and how one line in main() picks the crypto backend for rustls.

- **URL:** https://eagle2000.space/en/rust/rustls-crypto-provider-conflict/
- **Date:** 2026-12-05

- **Tags:** rust, dev, tls, cargo, debugging

---{{< lead >}}
"Production Pitfalls" - short breakdowns of non-obvious bugs I ran into myself, so you don't have to.
{{< /lead >}}

Today's pitfall is **`rustls`** and its `CryptoProvider`. Neither the compiler, nor `clippy`, nor local tests will catch this one.

---

## The server started fine, then crashed on the first email

I added one-time login-code emails to my HTTP backend built with the `Axum` framework. For sending mail, I used the `mail-send` crate (SMTP + STARTTLS + DKIM). Locally `cargo clippy` reported zero warnings. I built the Docker image, deployed it, and the healthcheck returned `200 OK`.

{{< spoiler title="Quick recap: what SMTP, STARTTLS, and DKIM are" >}}
- SMTP is the protocol for sending email between servers.
- STARTTLS is an extension that upgrades a plain SMTP connection to an encrypted TLS session (which is the step where `rustls` kicked in).
- DKIM is a cryptographic signature in the email headers that lets mail providers verify the message actually came from your domain and wasn't altered in transit.
{{< /spoiler >}}

But the very first request to send a code returned:

```text
HTTP/1.1 502 Bad Gateway
Server: nginx/1.24.0 (Ubuntu)
```

And the server logs showed the reason for the crash:

```text
thread 'tokio-rt-worker' panicked at rustls-0.23.46/src/crypto/mod.rs:249:14:

Could not automatically determine the process-level CryptoProvider from Rustls crate features.
Call CryptoProvider::install_default() before this point to select a provider manually,
or make sure exactly one of the 'aws-lc-rs' and 'ring' features is enabled.
```

---

## What a CryptoProvider is

{{< alert "comment" >}}
`rustls` is a modern TLS library written in Rust (a memory-safe alternative to OpenSSL). In the Rust ecosystem, HTTP clients, TLS-enabled databases, gRPC, and mail clients commonly use it under the hood.
{{< /alert >}}

`rustls` used to take all of its cryptography from the `ring` crate.

Starting with 0.23, the crypto backend lives behind the `CryptoProvider` interface and is selected via features:

- `ring` - the classic compact crypto library (Rust + assembly);
- `aws-lc-rs` - AWS cryptography based on a BoringSSL fork (supports FIPS). This is what `rustls` enables by default today.

{{< alert "comment" >}}
CryptoProvider is a struct holding implementations for encryption, signature verification, and random number generation. `rustls` handles the TLS protocol itself and delegates all the math to the selected provider.
{{< /alert >}}

When only one of these features ends up in the binary, `rustls` picks it as the default provider and the developer never notices.

---

## Where the second provider came from

The root cause is Cargo feature unification. When several dependencies pull in the same crate with different features, Cargo builds one copy of that crate with all of those features enabled at once.

In my project it went like this:

- `aws-sdk-s3` (MinIO / S3 access) enabled the `aws-lc-rs` feature in `rustls`;
- the new `mail-send` enabled the `ring` feature.

So a single `rustls` ended up with two crypto backends at once. Guessing one of them at random is unsafe: if a service requires `aws-lc-rs` for compliance, silently switching to `ring` would break that requirement. So when TLS starts, `rustls` crashes with an error and asks you to choose a provider explicitly.

To see who enables which feature:

```bash
cargo tree -e features -i rustls@0.23.46
```

{{< alert "lightbulb" >}}
`cargo tree` prints your project's dependency list. The `-i` (invert) flag flips the output around: instead of showing what `rustls` depends on, it shows which packages from your `Cargo.toml` pulled it into the build. With `-e features`, you can see which dependency turned on `ring` and which turned on `aws-lc-rs`.
{{< /alert >}}

---

## Why it didn't crash at startup

`rustls` does not check the provider when the program starts. It only looks for one when the code tries to open a secure connection for the first time and builds a config via `ClientConfig::builder()`.

At startup my server connected to Postgres and MinIO inside the Docker network without TLS, so `rustls` was never touched. It was needed for the first time only when `mail-send` opened a secure connection to the recipient's mail server. At that moment the `tokio` task panicked, the connection dropped, and `nginx` returned `502`.

{{< alert "fire" >}}
A service can run without issues for months and then crash on its first HTTPS request to an external API. A healthcheck that only verifies "the process is alive and responding" won't catch it.
{{< /alert >}}

---

## The one-line fix

Install a process-wide provider before any TLS connection is created, i.e. as the first line of `main()`:

```rust
#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Pick the rustls crypto backend for the whole process
    let _ = rustls::crypto::ring::default_provider().install_default();

    // ...then logger, DB pool, S3, HTTP server
    Ok(())
}
```

To reach `rustls::crypto::ring`, add `rustls` as a direct dependency:

```toml
[dependencies]
rustls = { version = "0.23", default-features = false, features = ["ring"] }
```

A few details:

1. Why `let _ =` is there. `install_default()` returns `Result<(), Arc<CryptoProvider>>`: `Ok(())` on the first call, or `Err` if a provider is already set (for example, by another library). Leaving the return value unhandled triggers an unused-`Result` compiler warning, while `.unwrap()` or `?` would crash the process on a second call. Since an already-installed provider is harmless here, `let _ =` explicitly discards the result.
2. Tests. `#[tokio::test]` has its own entry point, `main()` is not called there. If your tests use TLS, add the `install_default()` call to a test helper too. Calling it repeatedly is safe, it just returns `Err`:

   ```rust
   fn init_crypto() {
       let _ = rustls::crypto::ring::default_provider().install_default();
   }

   #[tokio::test]
   async fn sends_login_code() {
       init_crypto();
       // ...a test that talks to SMTP or HTTPS
   }
   ```

3. Alternative. You can configure every dependency in `Cargo.toml` to use the same backend — for example, by setting `default-features = false` and enabling only `aws-lc-rs`. But in a large dependency tree that's fragile: the next `cargo add` can silently pull the second provider back in through transitive dependencies. An explicit `install_default()` call is more reliable.

If you want to stay on the `rustls` default backend, the same line works with `aws-lc-rs`: `rustls::crypto::aws_lc_rs::default_provider().install_default()`. The point is to choose explicitly.

---

## What to take away from this

1. Cargo takes all features from your dependencies and merges them into a single build: any new crate can change the behavior of one you already use.
2. `rustls` 0.23+ with two providers fails only at runtime, on the very first TLS connection.
3. Set `CryptoProvider` explicitly in `main()` if your project has many networking dependencies (AWS SDK, `reqwest`, `sqlx`, `tonic`, SMTP clients). Adding one line can save you from unexpected error codes on the server.

🔗 **Docs:** [rustls::crypto::CryptoProvider](https://docs.rs/rustls/latest/rustls/crypto/struct.CryptoProvider.html)  
📦 **Crates.io:** [crates.io/crates/rustls](https://crates.io/crates/rustls)

