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
ConsumingQueueIterator is not thread safe #6141
Comments
The ConsumingQueueIterator class is an iterator that is designed to consume elements from a queue as they are encountered. The computeNext method is used to compute the next element in the iterator, by removing the element from the queue and returning it. As you pointed out, the current implementation of the computeNext method is not thread-safe, because it is not using an atomic operation to remove the element from the queue. This means that if multiple threads are accessing the iterator concurrently, it is possible for one thread to remove an element from the queue just before another thread tries to remove the same element, causing the second thread to throw a NoSuchElementException. To fix this issue, you could modify the computeNext method to use an atomic operation, such as poll, to remove the element from the queue. This would ensure that the element is removed from the queue in a thread-safe manner, preventing the NoSuchElementException from being thrown. However, it's worth noting that this change would modify the semantics of the ConsumingQueueIterator, as poll does not throw an exception when the queue is empty, unlike remove. This means that the computeNext method would return null when the queue is empty, instead of throwing a NoSuchElementException. It's also unclear why the computeNext method is public, as it is intended to be called by the iterator itself, rather than by external code. You may want to consider changing the visibility of the computeNext method to protected or private to reflect this. |
Thanks, yes, that's the reason. This might be nice to document, since users might reasonably assume thread-safety for a
That does sound like an accident. The method needs to be at least |
Raised a cr to contribute. [First contribution to the package 🙂]. |
related to #6141 addressing comment #6305 (comment) Fixes #6307 RELNOTES=n/a PiperOrigin-RevId: 503964969
related to #6141 addressing comment #6305 (comment) Fixes #6307 RELNOTES=n/a PiperOrigin-RevId: 504046688
|
The code of
ConsumingQueueIterator
is not thread safe because it is not using an atomic operation incomputeNext
we have this stacktrace once in our whole cluster
Instead of
I suggest sth like this but this changes the semantics since queued
null
s mess this up... ofc its questionable if it makes sense at allfor me its also unclear why computeNext is public...
The text was updated successfully, but these errors were encountered: