7122142 : (ann) Race condition between isAnnotationPresent and getAnnotations
peter.levart at gmail.com
Thu Jul 4 17:46:16 UTC 2013
On 07/04/2013 07:34 PM, Joel Borggrén-Franck wrote:
>>> >>Also, can you please explain to me why the update race is safe. I have done the/some research myself but I would like to know which angles you have covered.
>> >Well, one thing is that AnnotationType class is now effectively final, meaning that all it's state is referenced using final fields. If a reference to an instance of such class is obtained via data-race, all it's state is observed consistent by the thread that obtained the reference. The other thing is racy caching. If two or more threads are concurrently requesting AnnotationType for the same Class, many of them might construct and use it's own instance and the one published "latest" will finally be the one being cached. Since all such AnnotationType instances are constructed from the same input data, they are equivalent and it doesn't matter which one is used by which thread.
> Actually they aren't, something have been bugging me and I finally figured it out today. The problem is with default valued elements. Previously all threads have been seeing the same instance of default values but now that will only be guaranteed for Classes, Strings and Integers inside the value cache. While I don't think it is in the spec that annotation default value instances should be == for all threads I don't think it is a race we should introduce. Given this I think you should use some approach to produce a winner that every thread will use (like in the other annotations patch). My gut feeling is that CASing in a result will be better under contention that the current lock solution (while also correct and deadlock free) for a net win.
Well Joel, you have a point. I'll introduce volatile read and CAS...
>> >There is a caveat though. What if class is redefined? That's a separate issue and will have to be resolved in a separate patch. I'm planing to prepare a patch after this one gets through. The patch will remove contention from caching of annotations and may also take care of AnnotationType caching at the same time.
> I can't imagine this solution being more broken that the current situation:) or?
No, It is broken more or less to the same extent. If class is redefined,
old AnnotationType remains cached. It's not trivial to fix that. Imagine
all the annotation instances that cache default values taken out of
annotation type at their initialization, etc...
More information about the core-libs-dev