kosi-libs / Kodein-DB

Multiplatform NoSQL database
https://docs.kodein.org/kodein-db
MIT License
281 stars 23 forks source link

ConcurrentModificationException on find #51

Open tepel opened 2 years ago

tepel commented 2 years ago

Hi, I'm developing an android application using kodein-db and I have been using this code to get all results from MyData class:

val list = db.find(MyData::class).all().useModels { it.toList() }

The data is updated frequently by other threads and sometimes I get a java.util.ConcurrentModificationException


java.util.ConcurrentModificationException
        at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:757)
        at java.util.LinkedHashMap$LinkedEntryIterator.next(LinkedHashMap.java:790)
        at java.util.LinkedHashMap$LinkedEntryIterator.next(LinkedHashMap.java:790)
        at java.util.HashMap.putMapEntries(HashMap.java:511)
        at java.util.HashMap.putAll(HashMap.java:784)
        at org.kodein.db.impl.model.cache.ModelCacheImpl.renewInternals(ModelCacheImpl.kt:149)
        at org.kodein.db.impl.model.cache.ModelCacheImpl.copyIfNeeded(ModelCacheImpl.kt:159)
        at org.kodein.db.impl.model.cache.ModelCacheImpl.access$copyIfNeeded(ModelCacheImpl.kt:12)
        at org.kodein.db.impl.model.cache.ModelCacheImpl$getOrRetrieveEntry$1.invoke(ModelCacheImpl.kt:128)
        at org.kodein.db.impl.model.cache.ModelCacheImpl$getOrRetrieveEntry$1.invoke(ModelCacheImpl.kt:123)
        at org.kodein.db.impl.model.cache.ModelCacheImpl$getOrRetrieveEntry$2.invoke(ModelCacheImpl.kt:291)
        at org.kodein.db.impl.model.cache.ModelCacheImpl$getOrRetrieveEntry$2.invoke(ModelCacheImpl.kt:143)
        at org.kodein.db.impl.model.cache.ModelCacheImpl.getOrRetrieveEntry(ModelCacheImpl.kt:123)
        at org.kodein.db.impl.model.cache.ModelCacheImpl.getOrRetrieveEntry(ModelCacheImpl.kt:143)
        at org.kodein.db.impl.model.cache.CachedModelCursor.model(CachedModelCursor.kt:23)
        at org.kodein.db.impl.CursorImpl.model(CursorImpl.kt:13)
        at org.kodein.db.CursorKt$asModelSequence$$inlined$asSequence$1.invokeSuspend(Cursor.kt:47)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlin.sequences.SequenceBuilderIterator.hasNext(SequenceBuilder.kt:140)
        at kotlin.sequences.SequencesKt___SequencesKt.toCollection(_Sequences.kt:786)
        at kotlin.sequences.SequencesKt___SequencesKt.toMutableList(_Sequences.kt:816)
        at kotlin.sequences.SequencesKt___SequencesKt.toList(_Sequences.kt:807)
        at xxxx.MyDb.getAll(MyDb.kt:43)
        at xxxx.SomeDataSource.getAll(SomeDataSource.kt:18)
        at xxxx.UiListController$init$1$invokeSuspend$$inlined$collect$1.emit(Collect.kt:135)
        at kotlinx.coroutines.flow.FlowKt__ChannelsKt.emitAllImpl$FlowKt__ChannelsKt(Channels.kt:62)
        at kotlinx.coroutines.flow.FlowKt__ChannelsKt.access$emitAllImpl$FlowKt__ChannelsKt(Channels.kt:1)
        at kotlinx.coroutines.flow.FlowKt__ChannelsKt$emitAllImpl$1.invokeSuspend(Unknown Source:14)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at androidx.compose.ui.platform.AndroidUiDispatcher.performTrampolineDispatch(AndroidUiDispatcher.android.kt:81)
        at androidx.compose.ui.platform.AndroidUiDispatcher.access$performTrampolineDispatch(AndroidUiDispatcher.android.kt:41)
        at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.run(AndroidUiDispatcher.android.kt:57)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)

Is this expected and should I just try again? Or should I use some other way of getting all entries?

SalomonBrys commented 2 years ago

This is definitely not expected. I'll investigate.