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

Don't use double checked locking for CertLdapLoginModule initialization #276

Merged
merged 2 commits into from
Dec 19, 2016

Conversation

paterczm
Copy link
Contributor

https://en.wikipedia.org/wiki/Double-checked_locking

Locking on each getRoleSets call (every Lightblue request). Hope it does not affect performance.

LdapConfiguration will be initialized only once - this will improve performance.

if (options.containsKey(ROLES_CACHE_EXPIRY_MS)) {
rolesCacheExpiry = Integer.parseInt((String)options.get(ROLES_CACHE_EXPIRY_MS));
}
synchronized(LdapRolesProvider.class) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will synchronize every time initialize is called. Why not use volatile?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Volatile doesn't mean what you think, either
A commonly suggested nonfix is to declare the resource field of SomeClass as volatile. However, while the JMM prevents writes to volatile variables from being reordered with respect to one another and ensures that they are flushed to main memory immediately, it still permits reads and writes of volatile variables to be reordered with respect to nonvolatile reads and writes. That means -- unless all Resource fields are volatile as well -- thread B can still perceive the constructor's effect as happening after resource is set to reference the newly created Resource.

(from http://www.javaworld.com/article/2074979/java-concurrency/double-checked-locking--clever--but-broken.html)

If I understand this correctly (and unless something has changed, it's a really old article), volatile will not affect the properties of LdapRolesProvider, unless they are volatile as well.

There is also https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom, but it only works for initialization which cannot fail, so not good.

@bserdar
Copy link
Contributor

bserdar commented Dec 15, 2016 via email

@paterczm
Copy link
Contributor Author

That article from 2001 probably speaks of volatile implementation prior to jdk5. So I'm following your recommendation.

@bserdar bserdar merged commit bd382d4 into master Dec 19, 2016
@paterczm paterczm deleted the dcl branch December 22, 2016 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants