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

Race condition in HitsGroupsTokenFrequencies #379

Open
KCMertens opened this issue Dec 1, 2022 · 3 comments
Open

Race condition in HitsGroupsTokenFrequencies #379

KCMertens opened this issue Dec 1, 2022 · 3 comments
Labels

Comments

@KCMertens
Copy link
Member

Intellij (rightfully) warns about non-atomic operation on volatile variable

Fix this by replacing the int and long with AtomicInteger and AtomicLong

It won't crash, but group sizes may too low because addition from one thread may be overwritten by subsequent addition in another thread.

@KCMertens KCMertens added the bug label Dec 1, 2022
@jan-niestadt
Copy link
Member

Doesn't the fact that globalOccurrences is a ConcurrentHashMap mean this is actually safe?
Not 100% sure, so using Atomic* is probably a good idea. Hopefully this doesn't slow things down too much.

@KCMertens
Copy link
Member Author

Hmm, documentation of ConcurrentHashMap states "retrieval operations do not entail locking", so no, I don't think it prevents it sadly

@jan-niestadt
Copy link
Member

But the documentation for compute() states "The entire method invocation is performed atomically.", and no other operations can occur at this time, right? (docIds.parallellStream() only calls compute() on globalOccurrences).

Again, seeing how hard this is to figure out for sure, going with Atomic* is probably the safest, or at the very least documenting why we think the code is safe as-is (i.e. the bottom line of this discussion, whatever that may turn out to be :-).

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

No branches or pull requests

2 participants