Skip to content

Don't assume CertificateFactory is thread-safe - #16350

Merged
chrisvest merged 4 commits into
netty:4.2from
chrisvest:4.2-lazy-cert
Feb 25, 2026
Merged

chrisvest merged 4 commits into
netty:4.2from
chrisvest:4.2-lazy-cert

Conversation

@chrisvest

@chrisvest chrisvest commented Feb 24, 2026 •

Copy link
Copy Markdown
Member

Motivation:
We cannot assume that CertificateFactory implementations are thread-safe, since they contain complicated parsing logic which may be implemented as stateful operations depending on the particular provider used at runtime.

Modification:
Remove the singleton instance and just create a new CertificateFactory every time.
Creating these factories should not be the most expensive thing, regardless of provider used.

Made the wrapped field volatile to ensure visibility across threads, effectively obtaining safe publication for the X509Certificate object. This is important because X509Certificate, just like the CertificateFactory, may have any number of implementations at runtime and there are no guarantees about their thread-safety or immutability.

Made the test dig into the SANs and compare the data directly, instead of relying on the Collection.equals implementation, which could be anything, just like with the other objects above.

Result:
LazyX509Certificate is now thread-safe.

Should fix weird flaky test failures, such as:

[ERROR] Failures:
[ERROR]   LazyX509CertificateTest.testLazyX509Certificate:83 expected: java.util.Collections$UnmodifiableCollection@757bd65c<[[2, www.foo.com]]> but was: java.util.Collections$UnmodifiableCollection@2c32b649<[[2, www.foo.com]]>

Motivation:
We cannot assume that `CertificateFactory` implementations are thread-safe, since they contain complicated parsing logic which may be implemented as stateful operations depending on the particular provider used at runtime.

Modification:
Remove the singleton instance and replace it with a queue of factories.
When using a factory, it is temporarily removed from the queue while being used, and then added back when we're done.
`LazyX509Certificate` is not nearly busy enough to warrant the full Recycler machinery.

Made the `wrapped` field volatile to ensure visibility across threads, effectively obtaining safe publication for the `X509Certificate` object.
This is important because `X509Certificate`, just like the `CertificateFactory`, may have any number of implementations at runtime and there are no guarantees about their thread-safety or immutability.

Made the test dig into the SANs and compare the data directly, instead of relying on the `Collection.equals` implementation, which could be anything, just like with the other objects above.

Result:
`LazyX509Certificate` is now thread-safe.

Should fix weird flaky test failures, such as:

```
[ERROR] Failures:
[ERROR]   LazyX509CertificateTest.testLazyX509Certificate:83 expected: java.util.Collections$UnmodifiableCollection@757bd65c<[[2, www.foo.com]]> but was: java.util.Collections$UnmodifiableCollection@2c32b649<[[2, www.foo.com]]>
```
@chrisvest chrisvest added this to the 4.2.11.Final milestone Feb 24, 2026
@chrisvest chrisvest added needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Feb 24, 2026
@normanmaurer

Copy link
Copy Markdown
Member

Should me maybe just create a new Factory every time and just remove the queue ?

@chrisvest

Copy link
Copy Markdown
Member Author

Yeah, sure, we can just make a new one each time. It shouldn't get called all that often.

@normanmaurer

Copy link
Copy Markdown
Member

Yeah, sure, we can just make a new one each time. It shouldn't get called all that often.

I think this is basically called once per SSL / TLS handshake, so not sure if I would call it frequently or not.

@slandelle

Copy link
Copy Markdown
Contributor

I think this is basically called once per SSL / TLS handshake, so not sure if I would call it frequently or not.

Which is typically thousands of time per second in Gatling :(

@slandelle

Copy link
Copy Markdown
Contributor

Is this only an issue with custom/exotic CertificateFactory implementations? Could the behavior be controlled with a System property?

@chrisvest

Copy link
Copy Markdown
Member Author

Right, these are used to wrap the peer certs, not just the local cert. Let's use the Recycler, then.

@chrisvest

Copy link
Copy Markdown
Member Author

@normanmaurer @slandelle Please take a look.

@slandelle slandelle 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.

Great, thanks!

@chrisvest

Copy link
Copy Markdown
Member Author

Build failures are flaky tests. Merging.

@chrisvest
chrisvest merged commit 8e147e5 into netty:4.2 Feb 25, 2026
15 of 19 checks passed
@chrisvest
chrisvest deleted the 4.2-lazy-cert branch February 25, 2026 20:33
netty-project-bot pushed a commit that referenced this pull request Feb 25, 2026
Motivation:
We cannot assume that `CertificateFactory` implementations are
thread-safe, since they contain complicated parsing logic which may be
implemented as stateful operations depending on the particular provider
used at runtime.

Modification:
Remove the singleton instance and just create a new `CertificateFactory`
every time.
Creating these factories should not be the most expensive thing,
regardless of provider used.

Made the `wrapped` field volatile to ensure visibility across threads,
effectively obtaining safe publication for the `X509Certificate` object.
This is important because `X509Certificate`, just like the
`CertificateFactory`, may have any number of implementations at runtime
and there are no guarantees about their thread-safety or immutability.

Made the test dig into the SANs and compare the data directly, instead
of relying on the `Collection.equals` implementation, which could be
anything, just like with the other objects above.

Result:
`LazyX509Certificate` is now thread-safe.

Should fix weird flaky test failures, such as:

```
[ERROR] Failures:
[ERROR]   LazyX509CertificateTest.testLazyX509Certificate:83 expected: java.util.Collections$UnmodifiableCollection@757bd65c<[[2, www.foo.com]]> but was: java.util.Collections$UnmodifiableCollection@2c32b649<[[2, www.foo.com]]>
```

(cherry picked from commit 8e147e5)
@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 4.1: #16364

@github-actions github-actions Bot removed the needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. label Feb 25, 2026
netty-project-bot pushed a commit that referenced this pull request Feb 25, 2026
Motivation:
We cannot assume that `CertificateFactory` implementations are
thread-safe, since they contain complicated parsing logic which may be
implemented as stateful operations depending on the particular provider
used at runtime.

Modification:
Remove the singleton instance and just create a new `CertificateFactory`
every time.
Creating these factories should not be the most expensive thing,
regardless of provider used.

Made the `wrapped` field volatile to ensure visibility across threads,
effectively obtaining safe publication for the `X509Certificate` object.
This is important because `X509Certificate`, just like the
`CertificateFactory`, may have any number of implementations at runtime
and there are no guarantees about their thread-safety or immutability.

Made the test dig into the SANs and compare the data directly, instead
of relying on the `Collection.equals` implementation, which could be
anything, just like with the other objects above.

Result:
`LazyX509Certificate` is now thread-safe.

Should fix weird flaky test failures, such as:

```
[ERROR] Failures:
[ERROR]   LazyX509CertificateTest.testLazyX509Certificate:83 expected: java.util.Collections$UnmodifiableCollection@757bd65c<[[2, www.foo.com]]> but was: java.util.Collections$UnmodifiableCollection@2c32b649<[[2, www.foo.com]]>
```

(cherry picked from commit 8e147e5)
@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #16366

@github-actions github-actions Bot removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Feb 25, 2026
normanmaurer pushed a commit that referenced this pull request Feb 26, 2026
Auto-port of #16350 to 4.1
Cherry-picked commit: 8e147e5

---
Motivation:
We cannot assume that `CertificateFactory` implementations are
thread-safe, since they contain complicated parsing logic which may be
implemented as stateful operations depending on the particular provider
used at runtime.

Modification:
Remove the singleton instance and just create a new `CertificateFactory`
every time.
Creating these factories should not be the most expensive thing,
regardless of provider used.

Made the `wrapped` field volatile to ensure visibility across threads,
effectively obtaining safe publication for the `X509Certificate` object.
This is important because `X509Certificate`, just like the
`CertificateFactory`, may have any number of implementations at runtime
and there are no guarantees about their thread-safety or immutability.

Made the test dig into the SANs and compare the data directly, instead
of relying on the `Collection.equals` implementation, which could be
anything, just like with the other objects above.

Result:
`LazyX509Certificate` is now thread-safe.

Should fix weird flaky test failures, such as:

```
[ERROR] Failures:
[ERROR]   LazyX509CertificateTest.testLazyX509Certificate:83 expected: java.util.Collections$UnmodifiableCollection@757bd65c<[[2, www.foo.com]]> but was: java.util.Collections$UnmodifiableCollection@2c32b649<[[2, www.foo.com]]>
```

---------

Co-authored-by: Chris Vest <[email protected]>
normanmaurer pushed a commit that referenced this pull request Feb 26, 2026
Auto-port of #16350 to 5.0
Cherry-picked commit: 8e147e5

---
Motivation:
We cannot assume that `CertificateFactory` implementations are
thread-safe, since they contain complicated parsing logic which may be
implemented as stateful operations depending on the particular provider
used at runtime.

Modification:
Remove the singleton instance and just create a new `CertificateFactory`
every time.
Creating these factories should not be the most expensive thing,
regardless of provider used.

Made the `wrapped` field volatile to ensure visibility across threads,
effectively obtaining safe publication for the `X509Certificate` object.
This is important because `X509Certificate`, just like the
`CertificateFactory`, may have any number of implementations at runtime
and there are no guarantees about their thread-safety or immutability.

Made the test dig into the SANs and compare the data directly, instead
of relying on the `Collection.equals` implementation, which could be
anything, just like with the other objects above.

Result:
`LazyX509Certificate` is now thread-safe.

Should fix weird flaky test failures, such as:

```
[ERROR] Failures:
[ERROR]   LazyX509CertificateTest.testLazyX509Certificate:83 expected: java.util.Collections$UnmodifiableCollection@757bd65c<[[2, www.foo.com]]> but was: java.util.Collections$UnmodifiableCollection@2c32b649<[[2, www.foo.com]]>
```

Co-authored-by: Chris Vest <[email protected]>
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.

4 participants