Repository navigation
openssl: create SSL object once per thread in SSL_get_cipher_by_value - #48956
Merged
Merged
Conversation
OpenSSL can only look up a cipher through an SSL object. The compat implementation created and freed a new SSL_CTX and SSL on every call, and Envoy calls it once per offered cipher suite during TLS 1.2 certificate selection. Create the SSL object once on first use and reuse it for all later lookups. Fixes envoyproxy#48858 Signed-off-by: NMJlam <[email protected]>
NMJlam
force-pushed
the
openssl-cipher-by-value-static
branch
from
November 3, 2026 09:06
74429be to
9eb241f
Compare
NMJlam
commented
Nov 3, 2026
NMJlam
marked this pull request as ready for review
November 3, 2026 09:30
Contributor
Author
|
cc: @ggreenway @tedjpoole |
tedjpoole
reviewed
Nov 4, 2026
NMJlam
force-pushed
the
openssl-cipher-by-value-static
branch
from
November 4, 2026 22:35
9eb241f to
8ad3a92
Compare
OpenSSL does not guarantee that SSL_CIPHER_find() is thread safe, so cache one SSL object per thread instead of sharing one across threads. The object is now freed at thread exit. Signed-off-by: NMJlam <[email protected]>
NMJlam
force-pushed
the
openssl-cipher-by-value-static
branch
from
November 4, 2026 23:37
8ad3a92 to
11ded6f
Compare
Contributor
|
/assign @ggreenway |
Contributor
Author
|
@greenway bumping this, it's all green and has code owner approval. Could you take a look when you get a chance? Thanks! |
Member
|
I'm badly backed up on PR reviews. I'll get to it eventually |
jwendell
approved these changes
Nov 10, 2026
phlax
pushed a commit
to phlax/envoy
that referenced
this pull request
Nov 11, 2026
…envoyproxy#48956) Commit Message: openssl: create SSL object once per thread in SSL_get_cipher_by_value Additional Description: OpenSSL can only look up a cipher through an SSL object. The compat implementation of SSL_get_cipher_by_value created and freed a new SSL_CTX and SSL on every call. Envoy calls it once per offered cipher suite during TLS 1.2 certificate selection, meaning a single handshake could create many SSL_CTX objects. This change creates one SSL object per thread on first use, with a `thread_local bssl::UniquePtr`, and reuses it for later lookups on that thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread safe, so threads do not share the object. It is freed when its thread exits. Fixes envoyproxy#48858 Risk Level: Low (only affects --config=openssl builds) Testing: ```bash bazel test --config=openssl //compat/openssl/test/... bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test ``` Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B), --config=openssl): - before: 188.77 us/call - after: 0.34 us/call (~550x faster) *The thread_local version benchmarks about the same.* Docs Changes: N/A Release Notes: N/A AI Disclosure: Claude Code was used to help explain the codebase, simplify the implementation, write the throwaway benchmark, and draft this description. I have reviewed, tested, and understand all of the code in this PR. --------- Signed-off-by: NMJlam <[email protected]>
phlax
pushed a commit
to phlax/envoy
that referenced
this pull request
Nov 11, 2026
…envoyproxy#48956) Commit Message: openssl: create SSL object once per thread in SSL_get_cipher_by_value Additional Description: OpenSSL can only look up a cipher through an SSL object. The compat implementation of SSL_get_cipher_by_value created and freed a new SSL_CTX and SSL on every call. Envoy calls it once per offered cipher suite during TLS 1.2 certificate selection, meaning a single handshake could create many SSL_CTX objects. This change creates one SSL object per thread on first use, with a `thread_local bssl::UniquePtr`, and reuses it for later lookups on that thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread safe, so threads do not share the object. It is freed when its thread exits. Fixes envoyproxy#48858 Risk Level: Low (only affects --config=openssl builds) Testing: ```bash bazel test --config=openssl //compat/openssl/test/... bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test ``` Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B), --config=openssl): - before: 188.77 us/call - after: 0.34 us/call (~550x faster) *The thread_local version benchmarks about the same.* Docs Changes: N/A Release Notes: N/A AI Disclosure: Claude Code was used to help explain the codebase, simplify the implementation, write the throwaway benchmark, and draft this description. I have reviewed, tested, and understand all of the code in this PR. --------- Signed-off-by: NMJlam <[email protected]> Signed-off-by: Ryan Northey <[email protected]>
phlax
pushed a commit
to phlax/envoy
that referenced
this pull request
Nov 11, 2026
…envoyproxy#48956) Commit Message: openssl: create SSL object once per thread in SSL_get_cipher_by_value Additional Description: OpenSSL can only look up a cipher through an SSL object. The compat implementation of SSL_get_cipher_by_value created and freed a new SSL_CTX and SSL on every call. Envoy calls it once per offered cipher suite during TLS 1.2 certificate selection, meaning a single handshake could create many SSL_CTX objects. This change creates one SSL object per thread on first use, with a `thread_local bssl::UniquePtr`, and reuses it for later lookups on that thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread safe, so threads do not share the object. It is freed when its thread exits. Fixes envoyproxy#48858 Risk Level: Low (only affects --config=openssl builds) Testing: ```bash bazel test --config=openssl //compat/openssl/test/... bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test ``` Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B), --config=openssl): - before: 188.77 us/call - after: 0.34 us/call (~550x faster) *The thread_local version benchmarks about the same.* Docs Changes: N/A Release Notes: N/A AI Disclosure: Claude Code was used to help explain the codebase, simplify the implementation, write the throwaway benchmark, and draft this description. I have reviewed, tested, and understand all of the code in this PR. --------- Signed-off-by: NMJlam <[email protected]> Signed-off-by: Ryan Northey <[email protected]>
jwendell
pushed a commit
that referenced
this pull request
Nov 11, 2026
…#48956) Commit Message: openssl: create SSL object once per thread in SSL_get_cipher_by_value Additional Description: OpenSSL can only look up a cipher through an SSL object. The compat implementation of SSL_get_cipher_by_value created and freed a new SSL_CTX and SSL on every call. Envoy calls it once per offered cipher suite during TLS 1.2 certificate selection, meaning a single handshake could create many SSL_CTX objects. This change creates one SSL object per thread on first use, with a `thread_local bssl::UniquePtr`, and reuses it for later lookups on that thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread safe, so threads do not share the object. It is freed when its thread exits. Fixes #48858 Risk Level: Low (only affects --config=openssl builds) Testing: ```bash bazel test --config=openssl //compat/openssl/test/... bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test ``` Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B), --config=openssl): - before: 188.77 us/call - after: 0.34 us/call (~550x faster) *The thread_local version benchmarks about the same.* Docs Changes: N/A Release Notes: N/A AI Disclosure: Claude Code was used to help explain the codebase, simplify the implementation, write the throwaway benchmark, and draft this description. I have reviewed, tested, and understand all of the code in this PR. --------- Signed-off-by: NMJlam <[email protected]> Signed-off-by: Ryan Northey <[email protected]>
phlax
pushed a commit
to phlax/envoy
that referenced
this pull request
Nov 16, 2026
Enabled format checks for `compat/openssl/...` and fixed format errors --------- Signed-off-by: Ted Poole <[email protected]> Backport note: also formats `SSL_get_cipher_by_value.cc`, which on `main` was reformatted by envoyproxy#48956 rather than here. (cherry picked from commit d276628) Signed-off-by: Ryan Northey <[email protected]>
phlax
pushed a commit
to phlax/envoy
that referenced
this pull request
Nov 16, 2026
Enabled format checks for `compat/openssl/...` and fixed format errors --------- Signed-off-by: Ted Poole <[email protected]> Backport note: also formats `SSL_get_cipher_by_value.cc`, which on `main` was reformatted by envoyproxy#48956 rather than here. (cherry picked from commit d276628) Signed-off-by: Ryan Northey <[email protected]>
phlax
pushed a commit
that referenced
this pull request
Nov 17, 2026
Enabled format checks for `compat/openssl/...` and fixed format errors --------- Signed-off-by: Ted Poole <[email protected]> Backport note: also formats `SSL_get_cipher_by_value.cc`, which on `main` was reformatted by #48956 rather than here. (cherry picked from commit d276628) Signed-off-by: Ryan Northey <[email protected]>
phlax
pushed a commit
to phlax/envoy
that referenced
this pull request
Nov 17, 2026
…envoyproxy#48956) Commit Message: openssl: create SSL object once per thread in SSL_get_cipher_by_value Additional Description: OpenSSL can only look up a cipher through an SSL object. The compat implementation of SSL_get_cipher_by_value created and freed a new SSL_CTX and SSL on every call. Envoy calls it once per offered cipher suite during TLS 1.2 certificate selection, meaning a single handshake could create many SSL_CTX objects. This change creates one SSL object per thread on first use, with a `thread_local bssl::UniquePtr`, and reuses it for later lookups on that thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread safe, so threads do not share the object. It is freed when its thread exits. Fixes envoyproxy#48858 Risk Level: Low (only affects --config=openssl builds) Testing: ```bash bazel test --config=openssl //compat/openssl/test/... bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test ``` Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B), --config=openssl): - before: 188.77 us/call - after: 0.34 us/call (~550x faster) *The thread_local version benchmarks about the same.* Docs Changes: N/A Release Notes: N/A AI Disclosure: Claude Code was used to help explain the codebase, simplify the implementation, write the throwaway benchmark, and draft this description. I have reviewed, tested, and understand all of the code in this PR. --------- Signed-off-by: NMJlam <[email protected]> (cherry picked from commit 3d18e34) Signed-off-by: Ryan Northey <[email protected]>
phlax
pushed a commit
to phlax/envoy
that referenced
this pull request
Nov 17, 2026
…envoyproxy#48956) Commit Message: openssl: create SSL object once per thread in SSL_get_cipher_by_value Additional Description: OpenSSL can only look up a cipher through an SSL object. The compat implementation of SSL_get_cipher_by_value created and freed a new SSL_CTX and SSL on every call. Envoy calls it once per offered cipher suite during TLS 1.2 certificate selection, meaning a single handshake could create many SSL_CTX objects. This change creates one SSL object per thread on first use, with a `thread_local bssl::UniquePtr`, and reuses it for later lookups on that thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread safe, so threads do not share the object. It is freed when its thread exits. Fixes envoyproxy#48858 Risk Level: Low (only affects --config=openssl builds) Testing: ```bash bazel test --config=openssl //compat/openssl/test/... bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test ``` Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B), --config=openssl): - before: 188.77 us/call - after: 0.34 us/call (~550x faster) *The thread_local version benchmarks about the same.* Docs Changes: N/A Release Notes: N/A AI Disclosure: Claude Code was used to help explain the codebase, simplify the implementation, write the throwaway benchmark, and draft this description. I have reviewed, tested, and understand all of the code in this PR. --------- Signed-off-by: NMJlam <[email protected]> (cherry picked from commit 3d18e34) Signed-off-by: Ryan Northey <[email protected]>
phlax
pushed a commit
to phlax/envoy
that referenced
this pull request
Nov 17, 2026
…envoyproxy#48956) Commit Message: openssl: create SSL object once per thread in SSL_get_cipher_by_value Additional Description: OpenSSL can only look up a cipher through an SSL object. The compat implementation of SSL_get_cipher_by_value created and freed a new SSL_CTX and SSL on every call. Envoy calls it once per offered cipher suite during TLS 1.2 certificate selection, meaning a single handshake could create many SSL_CTX objects. This change creates one SSL object per thread on first use, with a `thread_local bssl::UniquePtr`, and reuses it for later lookups on that thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread safe, so threads do not share the object. It is freed when its thread exits. Fixes envoyproxy#48858 Risk Level: Low (only affects --config=openssl builds) Testing: ```bash bazel test --config=openssl //compat/openssl/test/... bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test ``` Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B), --config=openssl): - before: 188.77 us/call - after: 0.34 us/call (~550x faster) *The thread_local version benchmarks about the same.* Docs Changes: N/A Release Notes: N/A AI Disclosure: Claude Code was used to help explain the codebase, simplify the implementation, write the throwaway benchmark, and draft this description. I have reviewed, tested, and understand all of the code in this PR. --------- Signed-off-by: NMJlam <[email protected]> (cherry picked from commit 3d18e34) Signed-off-by: Ryan Northey <[email protected]>
phlax
pushed a commit
to phlax/envoy
that referenced
this pull request
Nov 17, 2026
…envoyproxy#48956) Commit Message: openssl: create SSL object once per thread in SSL_get_cipher_by_value Additional Description: OpenSSL can only look up a cipher through an SSL object. The compat implementation of SSL_get_cipher_by_value created and freed a new SSL_CTX and SSL on every call. Envoy calls it once per offered cipher suite during TLS 1.2 certificate selection, meaning a single handshake could create many SSL_CTX objects. This change creates one SSL object per thread on first use, with a `thread_local bssl::UniquePtr`, and reuses it for later lookups on that thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread safe, so threads do not share the object. It is freed when its thread exits. Fixes envoyproxy#48858 Risk Level: Low (only affects --config=openssl builds) Testing: ```bash bazel test --config=openssl //compat/openssl/test/... bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test ``` Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B), --config=openssl): - before: 188.77 us/call - after: 0.34 us/call (~550x faster) *The thread_local version benchmarks about the same.* Docs Changes: N/A Release Notes: N/A AI Disclosure: Claude Code was used to help explain the codebase, simplify the implementation, write the throwaway benchmark, and draft this description. I have reviewed, tested, and understand all of the code in this PR. --------- Signed-off-by: NMJlam <[email protected]> (cherry picked from commit 3d18e34) Signed-off-by: Ryan Northey <[email protected]>
phlax
pushed a commit
to phlax/envoy
that referenced
this pull request
Nov 17, 2026
…envoyproxy#48956) Commit Message: openssl: create SSL object once per thread in SSL_get_cipher_by_value Additional Description: OpenSSL can only look up a cipher through an SSL object. The compat implementation of SSL_get_cipher_by_value created and freed a new SSL_CTX and SSL on every call. Envoy calls it once per offered cipher suite during TLS 1.2 certificate selection, meaning a single handshake could create many SSL_CTX objects. This change creates one SSL object per thread on first use, with a `thread_local bssl::UniquePtr`, and reuses it for later lookups on that thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread safe, so threads do not share the object. It is freed when its thread exits. Fixes envoyproxy#48858 Risk Level: Low (only affects --config=openssl builds) Testing: ```bash bazel test --config=openssl //compat/openssl/test/... bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test ``` Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B), --config=openssl): - before: 188.77 us/call - after: 0.34 us/call (~550x faster) *The thread_local version benchmarks about the same.* Docs Changes: N/A Release Notes: N/A AI Disclosure: Claude Code was used to help explain the codebase, simplify the implementation, write the throwaway benchmark, and draft this description. I have reviewed, tested, and understand all of the code in this PR. --------- Signed-off-by: NMJlam <[email protected]> (cherry picked from commit 3d18e34) Signed-off-by: Ryan Northey <[email protected]>
phlax
pushed a commit
that referenced
this pull request
Nov 17, 2026
…#48956) Commit Message: openssl: create SSL object once per thread in SSL_get_cipher_by_value Additional Description: OpenSSL can only look up a cipher through an SSL object. The compat implementation of SSL_get_cipher_by_value created and freed a new SSL_CTX and SSL on every call. Envoy calls it once per offered cipher suite during TLS 1.2 certificate selection, meaning a single handshake could create many SSL_CTX objects. This change creates one SSL object per thread on first use, with a `thread_local bssl::UniquePtr`, and reuses it for later lookups on that thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread safe, so threads do not share the object. It is freed when its thread exits. Fixes #48858 Risk Level: Low (only affects --config=openssl builds) Testing: ```bash bazel test --config=openssl //compat/openssl/test/... bazel test --config=openssl //test/common/tls:ssl_socket_test //test/common/tls:context_impl_test ``` Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B), --config=openssl): - before: 188.77 us/call - after: 0.34 us/call (~550x faster) *The thread_local version benchmarks about the same.* Docs Changes: N/A Release Notes: N/A AI Disclosure: Claude Code was used to help explain the codebase, simplify the implementation, write the throwaway benchmark, and draft this description. I have reviewed, tested, and understand all of the code in this PR. --------- Signed-off-by: NMJlam <[email protected]> (cherry picked from commit 3d18e34) Signed-off-by: Ryan Northey <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Commit Message: openssl: create SSL object once per thread in SSL_get_cipher_by_value
Additional Description:
OpenSSL can only look up a cipher through an SSL object. The compat implementation of SSL_get_cipher_by_value created and freed a new SSL_CTX and SSL on every call. Envoy calls it once per offered cipher suite during TLS 1.2 certificate selection, meaning a single handshake could create many SSL_CTX objects.
This change creates one SSL object per thread on first use, with a
thread_local bssl::UniquePtr, and reuses it for later lookups on that thread. OpenSSL does not guarantee that SSL_CIPHER_find() is thread safe, so threads do not share the object. It is freed when its thread exits.Fixes #48858
Risk Level: Low (only affects --config=openssl builds)
Testing:
Micro-benchmark (10,000 calls to SSL_get_cipher_by_value(0xC02B), --config=openssl):
The thread_local version benchmarks about the same.
Docs Changes: N/A
Release Notes: N/A
AI Disclosure: Claude Code was used to help explain the codebase, simplify the implementation, write the throwaway benchmark, and draft this description. I have reviewed, tested, and understand all of the code in this PR.