We have some issues migrating to Koltin 1.4.10 for...
# kotlin-native
j
We have some issues migrating to Koltin 1.4.10 for our KMM project. Our current KMM project uses the following versions: Kotlin: 1.3.72 Ktor: 1.3.2 coroutines: 1.3.8 serialization: 0.20.0 We are in the process of upgrading our project to the following versions: Kotlin: 1.4.10 Ktor: 1.4.1 coroutines: 1.3.9-native-mt-2 serialization: 1.0.0-RC2 This upgrade works in our small sample app but in our producten app we get ImmutabilityExceptions. We added the ensureNeverFrozen call to better debug this issue and we are seeing that objects are getting freezed, which results in the following output in the exception (example): Caused by: kotlin.native.concurrent.FreezingException: freezing of Value(Loading(staleData=null, creationTimeStaleData=0)) has failed, first blocker is Loading(staleData=null, creationTimeStaleData=0) Since the exact same code works with the older versions, is there still some issue somewhere in ktor or coroutines library or does the native-mt-2 works differently than the single-threaded variant which now exposes a bug in our code?
j
not sure if this is variant of https://youtrack.jetbrains.com/issue/KTOR-1087 (which I only started getting after going from Ktor 1.4.0 to 1.4.1 + related dependencies) maybe try following combination first
Copy code
const val kotlinCoroutines = "1.3.9-native-mt"
    const val ktor = "1.4.0"
    const val kotlinxSerialization = "1.0.0-RC"
j
It might be related but if I use the following versions:
Copy code
val ktorVersion = "1.4.0"
val serializationVersion = "1.0.0-RC"
val coroutinesVersion = "1.3.9-native-mt"
I still get the same exception. However by using the following versions everything works:
Copy code
val ktorVersion = "1.3.2-1.4.0-rc"
val serializationVersion = "1.0.0-RC"
val coroutinesVersion = "1.3.9"
I will add this information to the ticket you created.
r
As of 1.4.+, Ktor now uses the multithreaded variant of coroutines (soonTM to be merged into master) which introduced a lot of changes, bugs and behaviors. Freezing now occurs at different places. One of my issues was that
CoroutineExceptionHandler
(and everything it references) gets frozen if the coroutine it’s attached to does a Ktor Client call https://youtrack.jetbrains.com/issue/KTOR-1073
Make sure you don’t reference things which should not be frozen in
CEH
. Note that referencing a property named, for example,
data
will reference
this.data
and freeze
this
.
I personally replaced most of my
CEH
usages with
try-catch
inside the suspend lambda.
j
You might need to force use of mt version of gradle dependency....in case any transitive dependencies being used
j
I did follow the steps as described here: https://kotlinlang.org/docs/mobile/concurrency-and-coroutines.html#ktor-and-coroutines For example:
Copy code
sourceSets["iosX64Main"].dependencies {
        implementation("io.ktor:ktor-client-ios-iosx64:$ktorVersion")
        implementation("com.soywiz.korlibs.klock:klock-iosx64:$klockVersion")
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion") {
            version {
                strictly(coroutinesVersion)
            }
        }
    }
I also did look at the https://github.com/touchlab/KaMPKit example regarding the dependencies.
k
Those ktor docs are old. We all collectively discovered a PR needs to be merged: https://github.com/JetBrains/kotlin-mobile-docs/pull/24
In theory, if you aren’t ever using the scope of that coroutine in a non-main thread, it shouldn’t freeze, but it’s been a while since I went deep on that (some context: https://medium.com/@kpgalligan/ktor-and-kotlin-native-fb5c06cb920a)
j
If I am correct we (should) always use the scope of the coroutine on the main thread but I am not 100% sure and we are in the process of investigating this issue further. If anything comes up we will let this know.
Further investigation turns out it has to do with this issue: https://github.com/Kotlin/kotlinx.coroutines/issues/2263 We use a mutex in some places to guard against multiple API requests from multiple processes. We did found a place where we tried to modify a frozen object, this is something we are going to fix.