Fix a race condition that leads to ClassAnalyzer with name CdiInjecteeSkippingClassAnalyzer exceptions #6053
Closed
arjantijms wants to merge 3 commits into
Closed
Conversation
Happens when many threads execute REST client call at the same. Symptom: Occasional IllegalStateException: Could not find an implementation of ClassAnalyzer with name CdiInjecteeSkippingClassAnalyzer from WebTarget.request Reason for the exception: * The injectionManager is set earlier than it's used, it's stored to a shared volatile variable and later picked up * If some thread sets the shared volatile variable to another injectionManager, the original thread would use another threads injectionManager * In case the second thread doesn't bind the ClassAnalyzer class in time before the first thread uses it, the first thread won't have ClassAnalyzer bean available and throws exception
Remove the thread local from the correct component (same as retrieved in the CdiComponentProvider.initialize method) Save a few nanoseconds by calling get instead of set on the thread local, log an error if a leaking injectionManager detected.
If ClientRuntime needs CDI beans, it initializes CdiComponentProvider again, we need to clean up later.
Contributor
Author
|
(this was already approved in the original PR) |
Contributor
|
@arjantijms , isn't it better to reopen my PR and change the target branch? |
Contributor
Author
Yes, I just did that. Sorry for the noise. |
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.
This solves the race condition - keep the effective injectionManager in a thread local. As multiple threads can modify the injectionManager and the
InjectionManagerInjectedCdiTargetinstances are global, effectiveInjectionManager must be scoped to the thread. It's actually scoped to a single WebTarget initialization and removed at the end of the initialization in thedone()method ofCdiComponentProviderThe problem:
A race condition happens when many threads execute REST client call at the same (more specifically, when
WebTarget.request()is called.Symptom:
Occasional
IllegalStateException: Could not find an implementation of ClassAnalyzer with name CdiInjecteeSkippingClassAnalyzerfromWebTarget.requestReason for the exception:
injectionManageris set in the initialization phase, before it's used. It's stored to a shared volatile variable and later picked upinjectionManager, before the original thread uses it, the original thread would use another thread'sinjectionManagerClassAnalyzerclass in time before the first thread uses it, the first thread won't haveClassAnalyzerbean available and throws exceptionA reproducer is described in payara/Payara#7753.
The original fix in Payara fork (53ae843) addresses this by a workaround - a retry in case of exception, which works, because by the next time, the other thread has enough time to bind the
ClassAnalyzerclass. But that solution isn't efficient.(resubmit after #6049)