Seems as though the issue I am running into is a s...
# kotlin-native
n
Seems as though the issue I am running into is a severe bug with Kotlin Native's memory model when Kotlin Coroutines are used, and C callbacks are involved. Below is the stack trace:
Copy code
Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared org.digieng.kmqtt.client.MqttClient@66c4d8 from other thread
    at kfun:kotlin.Throwable#<init>(kotlin.String?){} (0x2a7e09)
    at kfun:kotlin.Exception#<init>(kotlin.String?){} (0x2a1a07)
    at kfun:kotlin.RuntimeException#<init>(kotlin.String?){} (0x2a1bb7)
    at kfun:kotlin.native.IncorrectDereferenceException#<init>(kotlin.String){} (0x2cb1c7)
    at ThrowIllegalObjectSharingException (0x2cd977)
    at  (0x3e4b1b)
    at _ZNK16KRefSharedHolder3refIL11ErrorPolicy2EEEP9ObjHeaderv (0x3e4943)
    at Kotlin_Interop_derefStablePointer (0x3e48de)
    at kfun:kotlinx.cinterop.StableRef#get(){}1:0 (0x2692e6)
    at kfun:org.digieng.kmqtt.client.onDeliveryComplete#internal (0x37529e)
    at _6f72672e64696769656e673a6b6d7174742d636c69656e74_knbridge46 (0x37609b)
    at MQTTClient_cycle (0x410d96)
    at MQTTClient_run (0x40c52d)
    at  (0x7f4eb1b756db)
    at clone (0x7f4eb1686a3f)
    at  ((nil))
Aborted (core dumped)
After examining the libcurl ( https://github.com/JetBrains/kotlin-native/tree/master/samples/libcurl ) and curl ( https://github.com/JetBrains/kotlin-native/tree/master/samples/curl ) Kotlin Native samples there are two major differences (stuff not present). First difference is that the initRuntimeIfNeeded function isn't called inside the event handlers (C callbacks), and the second difference is that Kotlin Coroutines aren't used. Is the issue that is being encountered one of the corner cases mentioned in the article ( https://blog.jetbrains.com/kotlin/2020/07/kotlin-native-memory-management-roadmap/ )? Would highly appreciate some guidance from the Kotlin Native team on using Kotlin Coroutines with Kotlin Native's memory model (guides, key things to avoid, best practices etc).
The issue is occurring from the library side ( https://gitlab.com/napperley/kmqtt-client ).