Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 329226 - [DOM/AST] Wrong synchronization in jdt.core.dom package
Summary: [DOM/AST] Wrong synchronization in jdt.core.dom package
Status: CLOSED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2010-11-01 17:57 EDT by daolaf CLA
Modified: 2020-02-08 11:56 EST (History)
5 users (show)

See Also:


Attachments
The patch solving the synchronization problem (68.24 KB, patch)
2010-11-01 17:58 EDT, daolaf CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description daolaf CLA 2010-11-01 17:57:35 EDT
Build Identifier: 3.7

A lot of synchronization in the jdt.core.dom package is done wrongly with the double-checked locking pattern which is not working correctly.

See http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html.

I have fixed the affected classes and will attach a patch.

Reproducible: Always
Comment 1 daolaf CLA 2010-11-01 17:58:38 EDT
Created attachment 182178 [details]
The patch solving the synchronization problem
Comment 2 Jim des Rivieres CLA 2010-11-08 15:47:42 EST
Thanks for pointing out this latent bug, which has been there from the early days of Eclipse (c. 2001). I'm likely to have introduced the same bug in other code I've written - so all parts of the JDT AST implementation that does lazy init should be checked.

I find it interesting that this bug has not shown itself. Why? Is it because the incidence of concurrent access to an AST is rare?  Is it because the JVMs in use are not attempting the kinds of compiler optimizations that would cause the code to fail? Is it because the code is not being run on multi-processor hardware? 

Declaring all those getter methods as synchronized will affect performance. We also need to be concerned about introducing possible deadlocks.

Eclipse 3.7 can safely assume Java 5. As described in the "Fixing Double-Checked Locking using Volatile" section of the document mentioned in reference 1, another option for Java 5 or later is to declare the fields volatile (but leave the methods unsynchronized. This will also affect performance.

It would be good to understand which approach will have the lesser performance impact in practice, bearing in mind that different JVMs might suffer differently.
Comment 3 Ayushman Jain CLA 2010-11-09 06:11:24 EST
This is interesting. Thanks for the patch. We need to check what other places in the compiler code we have used the same pattern. 

I guess adding the synchronised modifier seems the best approach to me. Srikanth, what's your opinion on this? How can we gauge the performance impact with this change?
Comment 4 Olivier Thomann CLA 2010-11-09 09:37:13 EST
(In reply to comment #3)
> I guess adding the synchronized modifier seems the best approach to me.
> Srikanth, what's your opinion on this? How can we gauge the performance impact
> with this change?
I would check both ways to fix it: synchronizing the method or making fields volatile. We need to check the performance impact of both and select the best one.
Ayushman, could you please take care of this ?
Thanks.
Comment 5 Ayushman Jain CLA 2010-11-09 09:43:00 EST
(In reply to comment #3)
>[..]

As a side note, its not fully clear to me why the double locking mechanism wont work with Java in a platform-independent way (as written in the above document). Java's spec to generate code is a standard that will be followed, and i'm sure javac or a javac-compatible compiler will make sure that proper code is generated and that the generated code doesnt break the thread-safetyness. Are we sure that the current java memory model allows generation of improper bytecode for the double-locking mechanism, or are we just doing this because the above research document says so?

The very fact that we never ran into problems makes it seem that the java memory model specifies generation of good code for the double checking mechanism.
Comment 6 Olivier Thomann CLA 2011-04-01 11:59:09 EDT
Ayushman, please take care of this for 3.7.
Comment 7 Olivier Thomann CLA 2011-04-12 11:17:33 EDT
I tag it as 3.7M7. Please make sure one of the recommended fixes end up being released.
Comment 8 Ayushman Jain CLA 2011-04-14 01:41:45 EDT
(In reply to comment #7)
> I tag it as 3.7M7. Please make sure one of the recommended fixes end up being
> released.

Olivier, we can't use volatile currenly because its a 1.5 feature. Making all methods synchronized might hit performance (which may also affect clients since this is in the DOM ast nodes). Anyway, we have never really seen any problem in this area till yet. So I propose to defer this till we migrate to 1.5 when we can make the fields volatile. What do you think?
Comment 9 Srikanth Sankaran CLA 2011-04-14 02:19:41 EDT
(In reply to comment #8)
> (In reply to comment #7)
> > I tag it as 3.7M7. Please make sure one of the recommended fixes end up being
> > released.
> 
> Olivier, we can't use volatile currenly because its a 1.5 feature.

This is not correct, volatile has been in the language well before 1.5

> this is in the DOM ast nodes). Anyway, we have never really seen any problem in
> this area till yet.

Don't disagree with the above though.
Comment 10 Olivier Thomann CLA 2011-04-14 08:36:15 EDT
Let's target 3.8. This will be done at the same time we convert JDT/Core APIs to 1.5.
Comment 11 Ayushman Jain CLA 2011-04-14 08:42:54 EDT
(In reply to comment #9)
>[..]
> This is not correct, volatile has been in the language well before 1.5

Only in name. The semantics of volatile keyword have only been strengthened from 1.5 onwards. It has issues in 1.4. See the 4th code snippet at:
http://en.wikipedia.org/wiki/Double-checked_locking
Comment 12 Srikanth Sankaran CLA 2011-04-15 01:00:06 EDT
(In reply to comment #11)
> (In reply to comment #9)
> >[..]
> > This is not correct, volatile has been in the language well before 1.5
> 
> Only in name. The semantics of volatile keyword have only been strengthened
> from 1.5 onwards. It has issues in 1.4.

Thanks for the pointer Ayush, I hadn't known this.
Comment 13 Ayushman Jain CLA 2012-01-30 00:58:23 EST
We still haven't moved to 1.5, so moving it out of 3.8
Comment 14 Eclipse Genie CLA 2020-02-08 11:56:22 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.