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

Unsafe use of iterator from concurrent collection in Observable through forEach #810

Open
thoutbeckers opened this issue Sep 23, 2024 · 1 comment
Assignees
Labels
✏️architecture Structure of the libraries and design patterns used 🐛bug Something isn't working

Comments

@thoutbeckers
Copy link
Collaborator

thoutbeckers commented Sep 23, 2024

This usage is hidden because old package level methods are used dating back to kotlin native's old memory model, but a concurrent list of the observers is iterated through which is unsafe, as any modification while iterating will cause a crash. forEach usage should always be wrapped in synchronized.

This does raise the question if ConcurrentList should even conform to the List interface, it would be better to expose the List only from inside synchronized (as it already does).

val l = ConcurrentMutableList()
// current l is a `List` so you can do this:
l.forEach { /* crashes when the list is changed from the outside*/ } 
l.synchronized {
     forEach { } // already possible and safe.
}

Maybe a ConcurrentList interface should be considered that does not allow mutation, which can be created with a toConcurrentList()/asConcurrentList on a ConcurrentMutableList

@thoutbeckers
Copy link
Collaborator Author

synchronized usage still leaves open same thread modification (using re-entrant locks), instead copy-before-iterate should be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✏️architecture Structure of the libraries and design patterns used 🐛bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants