GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Don't assume CertificateFactory is thread-safe by chrisvest · Pull Request #16350 · netty/netty

Conversation

@chrisvest

@chrisvest chrisvest commented Feb 24, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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]]>

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@chrisvest
Don't assume CertificateFactory is thread-safe
b892744
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 requested a review from normanmaurer February 24, 2026 19:13
@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

normanmaurer commented Feb 24, 2026

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest

chrisvest commented Feb 24, 2026

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest
Create a new factory every time we unwrap and have less static state
58b8431
@normanmaurer

normanmaurer commented Feb 25, 2026

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@slandelle

slandelle commented Feb 25, 2026

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 :(

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@slandelle

slandelle commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest
Merge branch '4.2' into 4.2-lazy-cert
75430c4
@chrisvest

chrisvest commented Feb 25, 2026

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest
Make LazyX509Certificate use a Recycler for CertificateFactory
4017e5c
@chrisvest

chrisvest commented Feb 25, 2026

Copy link
Copy Markdown
Member Author

@normanmaurer @slandelle Please take a look.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

normanmaurer
normanmaurer approved these changes Feb 25, 2026
View reviewed changes
slandelle
slandelle approved these changes Feb 25, 2026
View reviewed changes

@slandelle slandelle left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Great, thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@chrisvest

chrisvest commented Feb 25, 2026

Copy link
Copy Markdown
Member Author

Build failures are flaky tests. Merging.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest
chrisvest merged commit 8e147e5 into netty:4.2 Feb 25, 2026
15 of 19 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@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
@chrisvest @netty-project-bot
Don't assume CertificateFactory is thread-safe (#16350)
0dad2ff
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

netty-project-bot commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

Auto-port PR for 4.1: #16364

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@netty-project-bot netty-project-bot mentioned this pull request Feb 25, 2026
Auto-port 4.1: Don't assume CertificateFactory is thread-safe #16364
Merged
@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
@chrisvest @netty-project-bot
Don't assume CertificateFactory is thread-safe (#16350)
de5cd8d
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

netty-project-bot commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #16366

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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
@netty-project-bot netty-project-bot mentioned this pull request Feb 25, 2026
Auto-port 5.0: Don't assume CertificateFactory is thread-safe #16366
Merged
normanmaurer pushed a commit that referenced this pull request Feb 26, 2026
@netty-project-bot @chrisvest
Auto-port 4.1: Don't assume CertificateFactory is thread-safe (#16364)
ccf03c4
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
@netty-project-bot @chrisvest
Auto-port 5.0: Don't assume CertificateFactory is thread-safe (#16366)
6c58583
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]>
@lhotari lhotari mentioned this pull request Mar 25, 2026
[fix][client] Fix thread-safety and refactor MessageCryptoBc key management apache/pulsar#25400
Merged
3 of 12 tasks
@normanmaurer normanmaurer mentioned this pull request Oct 1, 2026
child5 #17695
Closed
@normanmaurer normanmaurer mentioned this pull request Oct 26, 2026
crisp nova cceda091 #18109
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@normanmaurer normanmaurer normanmaurer approved these changes
@slandelle slandelle slandelle approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.2.11.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@chrisvest @normanmaurer @slandelle @netty-project-bot
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.