InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
8.76k stars 695 forks source link

ConcurrentModificationException iOS crash on koin 3.5.6 #1863

Open elkhoudiry opened 1 month ago

elkhoudiry commented 1 month ago

Describe the bug I'm having a scope linked to another scope and the application crashes in dev and production on iOS due to kotlin.ConcurrentModificationException. It's so annoying because it happens randomly some times

    at 0    <module>                    0x1065ea423        kfun:kotlin.Exception#<init>(kotlin.String?;kotlin.Throwable?){} + 143 
    at 1    <module>                    0x1065ea737        kfun:kotlin.RuntimeException#<init>(kotlin.String?;kotlin.Throwable?){} + 143 
    at 2    <module>                    0x1065eb56f        kfun:kotlin.ConcurrentModificationException#<init>(kotlin.String?;kotlin.Throwable?){} + 143 
    at 3    <module>                    0x1065eb5e3        kfun:kotlin.ConcurrentModificationException#<init>(){} + 95 
    at 4    <module>                    0x10663c597        kfun:kotlin.collections.ArrayList.Itr.checkForComodification#internal + 243 
    at 5    <module>                    0x10663bd3b        kfun:kotlin.collections.ArrayList.Itr.next#internal + 199 
    at 6    <module>                    0x106c2c747        kfun:kotlin.collections.MutableIterator#next(){}1:0-trampoline + 99 
    at 7    <module>                    0x10689fa4b        kfun:org.koin.core.scope.Scope.findInOtherScope#internal + 359  // I think this is the issue root
    at 8    <module>                    0x10689f743        kfun:org.koin.core.scope.Scope.resolveValue#internal + 2615 
    at 9    <module>                    0x10689ec2b        kfun:org.koin.core.scope.Scope.resolveInstance#internal + 1519 
    at 10   <module>                    0x10689e5e7        kfun:org.koin.core.scope.Scope#get(kotlin.reflect.KClass<*>;org.koin.core.qualifier.Qualifier?;kotlin.Function0<org.koin.core.parameter.ParametersHolder>?){0§<kotlin.Any?>}0:0 + 2179 
    at 11   <module>                    0x106a47ec7        kfun:com.app.InjectionHelper$lambda$6$lambda$2#internal + 435 
    at 12   <module>                    0x106a49dd3        kfun:com.app.InjectionHelper$lambda$6$lambda$2$FUNCTION_REFERENCE$3.invoke#internal + 135 
    at 13   <module>                    0x106c2d7a3        kfun:kotlin.Function2#invoke(1:0;1:1){}1:2-trampoline + 115 
    at 14   <module>                    0x10688f673        kfun:org.koin.core.instance.InstanceFactory#create(org.koin.core.instance.InstanceContext){}1:0 + 943 
    at 15   <module>                    0x10689066f        kfun:org.koin.core.instance.ScopedInstanceFactory#create(org.koin.core.instance.InstanceContext){}1:0 + 495 

To Reproduce Steps to reproduce the behavior: In my case it happens randomly but the general code is like this

private fun scope(): Scope {
        val scope = getKoin().getHandledScope<Scope#1>(Scope#1.getScopeName().value)
        val another = getKoin().getHandledScope<Scope#2>(Scope#2.getScopeName().value)

        scope.linkTo(another)
        return scope
}

fun getScreenState(): Flow<ScreenState> {
     return scope().get<Repository>().screenStateStream // the crash happens here
}

Koin module and version: koin:3.5.6

arnaudgiuliani commented 1 month ago

anyway to reproduce it in a stable way?

elkhoudiry commented 1 month ago

Well it's concurrency issue with race conditions i can't reproduce in a stable way, but for me it happens frequently if i try to enter a screen in the application in which i'm creating the koin scope on init of that screen and leave that screen and enter it again quickly, repeating that multiple time produces this issue