https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
t

Tim Ortel

08/20/2022, 9:19 PM
I have an issue with using coroutines on kotlin native. I need to suspend my coroutine until a call to an api is done. However, the call to the api internally executes the code on another thread. Therefore, the following error is thrown:
Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlinx.coroutines.internal.DispatchedContinuation
Copy code
runBlocking {
    withContext(Dispatchers.Main) {
        suspendCoroutine { c ->
            //handler is called from another thread.
            val handler = { x -> c.resume(x) }

            //Internally, schedules the work on another thread.
            //Therefore, c.resume() fails, as c is frozen.
            some_api_that_schedules_another_thread(handler)
        }
    }
}
Is there any way around of this. Changing the internal API is not possible.
j

John O'Reilly

08/20/2022, 9:25 PM
Not a direct answer but are you able to try the new Kotlin Native memory model?
It's use should spell the end of those
InvalidMutabilityException
errors.
You can enable by adding following to your
gradle.properties
Copy code
kotlin.native.binary.memoryModel=experimental
depending on libraries you're using you might also want to add
Copy code
kotlin.native.binary.freezing=disabled
And I believe you need to be on Kotlin 1.6.20 or later
t

Trevor Stone

08/21/2022, 1:15 AM
You could, as a work around, use something like a mutable state flow, pass that to the handler, emit to it on the other thread and collect on the initial thread. Once it emits you call c cancel without passing it around
21 Views