https://kotlinlang.org logo
#kodein
Title
# kodein
h

Hylke Bron

08/11/2022, 12:56 PM
Hi, in my project I am using Kodein DI for the DI framework. It works brilliantly, but today me and my colleague ran into the following issue on ios:
Copy code
Uncaught Kotlin exception: kotlin.NullPointerException
    at 0   LibName                             0x102a360e2        kfun:kotlin.Exception#<init>(){} + 50 
    at 1   LibName                             0x102a3616f        kfun:kotlin.RuntimeException#<init>(){} + 31 
    at 2   LibName                             0x102a53a35        ThrowNullPointerException + 165 
    at 3   LibName                             0x102a63771        kfun:kotlin.collections.HashMap#get(1:0){}1:1? + 177 
    at 4   LibName                             0x102be18e0        kfun:org.kodein.di.bindings.StandardScopeRegistry#getOrCreate(kotlin.Any;kotlin.Boolean;kotlin.Function0<org.kodein.di.bindings.Reference<kotlin.Any>>){}kotlin.Any + 304 
    at 5   LibName                             0x102be42ca        kfun:org.kodein.di.bindings.Singleton.$getFactory$lambda-3$FUNCTION_REFERENCE$90.invoke#internal + 650 
    at 6   LibName                             0x102bdcb94        kfun:org.kodein.di.DIContainer.$provider$lambda-0$FUNCTION_REFERENCE$79.invoke#internal + 212 
    at 7   LibName                             0x102bdc10d        kfun:org.kodein.di.$Instance$lambda-0$FUNCTION_REFERENCE$78.invoke#internal + 1949 
    at 8   LibName                             0x102bfb243        kfun:org.kodein.di.DIProperty.$provideDelegate$lambda-0$FUNCTION_REFERENCE$124.invoke#internal + 547 
    at 9   LibName                             0x102a41708        kfun:kotlin.native.concurrent.SynchronizedLazyImpl#<get-value>(){}1:0 + 2024 
    at 10  LibName                             0x102c139da        kfun:my.company.name.<get-dependency>#internal + 186
The reproduction path is weird, we had to start, stop and start the application before it would crash. I dont have the time to distill a better reproduction path unfortunately, but i suspect it has something to do with race conditions while obtaining a singleton value. In my kotlin code I launch two coroutine jobs, which are executed as soon as the ios app starts:
Copy code
coroutineScope {
    launch { /*something that eventually will use the DI container; i.e. private val dependency: Dependency by instance()*/ }
    launch { /*something that eventually will use the DI container; i.e. private val dependency: Dependency by instance()*/ }
}
I think this might be a concurrency issue with the native implementation of this library. Maybe its related to the fact that I am using the new experimental memory model. (which works fine by the way). My current solution is that instead of binding instances with
bindSingleton { instance }
, I now use
bindEagerSingleton { instance }
. The fact that this solves my issue also points in the direction that there are some concurrency issues in the di library. No need for direct assistence, but maybe if someone of the kodein team reads this they might gain some new insight.
4 Views