Repository navigation
Don't assume CertificateFactory is thread-safe - #16350
Conversation
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]]> ```
|
Should me maybe just create a new Factory every time and just remove the queue ? |
|
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. |
Which is typically thousands of time per second in Gatling :( |
|
Is this only an issue with custom/exotic CertificateFactory implementations? Could the behavior be controlled with a System property? |
|
Right, these are used to wrap the peer certs, not just the local cert. Let's use the Recycler, then. |
|
@normanmaurer @slandelle Please take a look. |
|
Build failures are flaky tests. Merging. |
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)
|
Auto-port PR for 4.1: #16364 |
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)
|
Auto-port PR for 5.0: #16366 |
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]>
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]>
Motivation:
We cannot assume that
CertificateFactoryimplementations 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
CertificateFactoryevery time.Creating these factories should not be the most expensive thing, regardless of provider used.
Made the
wrappedfield volatile to ensure visibility across threads, effectively obtaining safe publication for theX509Certificateobject. This is important becauseX509Certificate, just like theCertificateFactory, 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.equalsimplementation, which could be anything, just like with the other objects above.Result:
LazyX509Certificateis now thread-safe.Should fix weird flaky test failures, such as: