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?
John O'Reilly
08/20/2022, 9:26 PM
It's use should spell the end of those
InvalidMutabilityException
errors.
John O'Reilly
08/20/2022, 9:27 PM
You can enable by adding following to your
gradle.properties
Copy code
kotlin.native.binary.memoryModel=experimental
John O'Reilly
08/20/2022, 9:28 PM
depending on libraries you're using you might also want to add
Copy code
kotlin.native.binary.freezing=disabled
John O'Reilly
08/20/2022, 9:29 PM
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