RFR(s): 8023541 Race condition in rmid initialization
Paul.Sandoz at oracle.com
Thu Jan 30 11:13:10 UTC 2014
On Jan 30, 2014, at 3:57 AM, Stuart Marks <stuart.marks at oracle.com> wrote:
> Then, awaitInitialized seems straightforward until you see that the condition is waiting for the value of a final variable to change! JLS sec 17.5  allows all sorts of optimizations for final fields, but they all are qualified with what is essentially a safe publication requirement on the reference:
> An object is considered to be completely initialized when its constructor
> finishes. A thread that can only see a reference to an object after that
> object has been completely initialized is guaranteed to see the correctly
> initialized values for that object's final fields.
>  http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.5
> Unfortunately this code *unsafely* publishes a reference to 'this' which is the root of this whole problem. Under these circumstances I'd prefer to be really conservative about the code here. I can't quite convince myself that a condition loop waiting for a final field to change value is safe. That's why I added a separate field.
I think you have done the right thing in the latest webrev, even though i suspect the runtime does not fully optimize final fields as constants (since it is still possible to update final fields, e.g. see System.out).
It should not be this hard to reason about this stuff, right?
Hopefully updates to the JMM will make this easier to grok, even though this is a naughty case.
More information about the core-libs-dev