Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect double-checked locking idiom #1193

Closed
olotenko opened this issue Dec 9, 2019 · 1 comment · Fixed by #1228
Closed

Incorrect double-checked locking idiom #1193

olotenko opened this issue Dec 9, 2019 · 1 comment · Fixed by #1228
Assignees
Labels

Comments

@olotenko
Copy link

olotenko commented Dec 9, 2019

https://github.com/oracle/helidon/blob/1dacd76/common/context/src/main/java/io/helidon/common/context/ListContext.java#L190-L191 - the order of assignments allows to observe instance to be null.

The correct order is:

        private T instance; // no need to declare it volatile
        public T get() {
            if (missing) {
                synchronized (this) {
                    if (missing) {
                        instance = supplier.get();
                        missing = false; // publish only after a valid value is assigned to instance
                    }
                }
            }
            return instance; // missing is volatile, so this read is going to happen
        }

Also, you probably do not need to hold on to supplier after the first use.
ListContext.java.1dacd76.diff.zip - a diff against 1dacd76

@tomas-langer
Copy link
Member

@olotenko could you please have a look at the PR?
I have added a slightly more complex fix - I have created a LazyValue interface that is used to wrap the concept of "get once and cache" and to be properly thread safe (without using the synchronized keyword.
I have also updated our ExecutorService suppliers to use the new component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants