https://kotlinlang.org logo
#ktor
Title
# ktor
j

John O'Reilly

09/29/2020, 8:00 PM
Anyone by chance also run in to following after updating to Ktor 1.4.1? https://youtrack.jetbrains.com/issue/KTOR-1087
r

russhwolf

09/29/2020, 8:39 PM
I wonder if sqldelight is causing you to pull in
1.3.9-native-mt
instead of
1.3.9-native-mt-2
j

John O'Reilly

09/29/2020, 8:40 PM
I'm using
isForce = true
on the kotlinx-coroutines dependency
r

russhwolf

09/29/2020, 8:43 PM
Check
./gradlew dependencies
anyway and see if there's anything surprising
j

John O'Reilly

09/29/2020, 8:45 PM
yeah, it's using
mt-2
e.g. for sqldelight
Copy code
org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9 -> 1.3.9-native-mt-2
I updated KaMPKit to 1.4.1 locally and not seeing any issues....I tried making calls to
ensureNeverFrozen()
in my project that it's using but then get different crash
r

russhwolf

09/30/2020, 1:27 PM
That should help diagnose where your freeze is coming from
j

John O'Reilly

09/30/2020, 1:29 PM
it shows
eopleInSpaceApi@2f31fb8 has failed, first blocker is com.surrus.common.remote.PeopleInSpaceApi
which I guess is point that ktor client request is being made
full stack
Copy code
kotlin.native.concurrent.FreezingException: freezing of <no name provided>_1@3b81ce8 has failed, first blocker is com.surrus.common.remote.PeopleInSpaceApi@35c88a8
    at 0   PeopleInSpaceSwiftUI                0x000000010d3ceccd kfun:kotlin.Throwable#<init>(kotlin.String?){} + 93
    at 1   PeopleInSpaceSwiftUI                0x000000010d3c787b kfun:kotlin.Exception#<init>(kotlin.String?){} + 91
    at 2   PeopleInSpaceSwiftUI                0x000000010d3c7acb kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 91
    at 3   PeopleInSpaceSwiftUI                0x000000010d3fd8f3 kfun:kotlin.native.concurrent.FreezingException#<init>(kotlin.Any;kotlin.Any){} + 643
    at 4   PeopleInSpaceSwiftUI                0x000000010d3fef77 ThrowFreezingException + 231
    at 5   PeopleInSpaceSwiftUI                0x000000010d4ee10f FreezeSubgraph + 2815
    at 6   PeopleInSpaceSwiftUI                0x000000010d50733b Kotlin_Worker_freezeInternal + 27
    at 7   PeopleInSpaceSwiftUI                0x000000010d3fda1e kfun:kotlin.native.concurrent#freeze@0:0(){0§<kotlin.Any?>}0:0 + 62
    at 8   PeopleInSpaceSwiftUI                0x000000010d758df2 kfun:io.ktor.util.collections.internal.MapNode.<no name provided>_1.<init>#internal.33 + 338
    at 9   PeopleInSpaceSwiftUI                0x000000010d75776d kfun:io.ktor.util.collections.internal.MapNode#<init>(1:0;1:1){} + 701
    at 10  PeopleInSpaceSwiftUI                0x000000010d748461 kfun:io.ktor.util.collections.ConcurrentMap.put$lambda-3#internal + 1137
    at 11  PeopleInSpaceSwiftUI                0x000000010d74c935 kfun:io.ktor.util.collections.ConcurrentMap.$put$lambda-3$FUNCTION_REFERENCE$25.invoke#internal + 165
    at 12  PeopleInSpaceSwiftUI                0x000000010d7473d7 kfun:io.ktor.util.collections.ConcurrentMap.locked#internal + 439
    at 13  PeopleInSpaceSwiftUI                0x000000010d745adb kfun:io.ktor.util.collections.ConcurrentMap#put(1:0;1:1){}1:1? + 379
    at 14  PeopleInSpaceSwiftUI                0x000000010d7ce6e1 kfun:io.ktor.client.HttpClientConfig#install(io.ktor.client.features.HttpClientFeature<0:0,0:1>;kotlin.Function1<0:0,kotlin.Unit>){0§<kotlin.Any>;1§<kotlin.Any>} + 1041
    at 15  PeopleInSpaceSwiftUI                0x000000010d32ac62 kfun:com.surrus.common.remote.PeopleInSpaceApi.<init>$lambda-4$lambda-3#internal + 386
    at 16  PeopleInSpaceSwiftUI                0x000000010d32b59f kfun:com.surrus.common.remote.PeopleInSpaceApi.$<init>$lambda-4$lambda-3$FUNCTION_REFERENCE$16.invoke#internal + 95
    at 17  PeopleInSpaceSwiftUI                0x000000010d32b61f kfun:com.surrus.common.remote.PeopleInSpaceApi.$<init>$lambda-4$lambda-3$FUNCTION_REFERENCE$16.$<bridge-UNNN>invoke(-1:0){}#internal + 95
    at 18  PeopleInSpaceSwiftUI                0x000000010d7c8013 kfun:io.ktor.client#HttpClient(io.ktor.client.engine.HttpClientEngineFactory<0:0>;kotlin.Function1<io.ktor.client.HttpClientConfig<0:0>,kotlin.Unit>){0§<io.ktor.client.engine.HttpClientEngineConfig>}io.ktor.client.HttpClient + 595
    at 19  PeopleInSpaceSwiftUI                0x000000010d81ae1e kfun:io.ktor.client#HttpClient(kotlin.Function1<io.ktor.client.HttpClientConfig<*>,kotlin.Unit>){}io.ktor.client.HttpClient + 638
    at 20  PeopleInSpaceSwiftUI                0x000000010d32ae7f kfun:com.surrus.common.remote.PeopleInSpaceApi.<init>$lambda-4#internal + 223
    at 21  PeopleInSpaceSwiftUI                0x000000010d32b15d kfun:com.surrus.common.remote.PeopleInSpaceApi.$<init>$lambda-4$FUNCTION_REFERENCE$13.invoke#internal + 157
    at 22  PeopleInSpaceSwiftUI                0x000000010d3ffe15 kfun:kotlin.native.concurrent.FreezeAwareLazyImpl.getOrInit#internal + 1045
    at 23  PeopleInSpaceSwiftUI                0x000000010d4005ae kfun:kotlin.native.concurrent.FreezeAwareLazyImpl#<get-value>(){}1:0 + 670
    at 24  PeopleInSpaceSwiftUI                0x000000010d3254a3 kfun:com.surrus.common.remote.PeopleInSpaceApi.<get-client>#internal + 323
    at 25  PeopleInSpaceSwiftUI                0x000000010d3261d7 kfun:com.surrus.common.remote.PeopleInSpaceApi.$fetchPeopleCOROUTINE$0#invokeSuspend(kotlin.Result<kotlin.Any?>){}kotlin.Any? + 2983
    at 26  PeopleInSpaceSwiftUI                0x000000010d327d94 kfun:com.surrus.common.remote.PeopleInSpaceApi#fetchPeople(){}com.surrus.common.remote.AstroResult + 244
    at 27  PeopleInSpaceSwiftUI                0x000000010d32e186 kfun:com.surrus.common.repository.PeopleInSpaceRepository.$fetchPeople$lambda-1COROUTINE$5.invokeSuspend#internal + 726
    at 28  PeopleInSpaceSwiftUI                0x000000010d3f07f6 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl#resumeWith(kotlin.Result<kotlin.Any?>){} + 758
    at 29  PeopleInSpaceSwiftUI                0x000000010d5bba52 kfun:kotlinx.coroutines.DispatchedTask#run(){} + 2802
looking for differences with KaMPKit.....but at least ktor interaction looks to be much the same
l

louiscad

09/30/2020, 1:58 PM
Looks like something @ribesg encountered.
That depressing "freezing of <no name provided>" 😅
r

ribesg

09/30/2020, 1:59 PM
I’ve encountered them all
😅 1
And I’m far from done.
j

John O'Reilly

09/30/2020, 2:00 PM
this has only started happening for this project after changing from ktor 1.4.0 to 1.4.1 (or perhaps some of the related dependency updates)
I'm still not clear what "freezing of <no name provided>_1@3b81ce8 has failed" is supposed to mean exactly.....is it that ktor did attempt to freeze something (to allow sharing across threads) but it failed (thus leading to original
InvalidMutabilityException
) (with "Frozen during lazy computation" message)
fwiw this is where that "Frozen during lazy computation" message is coming from https://github.com/JetBrains/kotlin-native/blob/master/runtime/src/main/kotlin/kotlin/native/concurrent/Lazy.kt
and that in turn is used by
Copy code
public actual fun <T> lazy(initializer: () -> T): Lazy<T> = FreezeAwareLazyImpl(initializer)
and that looks to be difference between PeopleInSpace and KaMPKit....I have following
Copy code
private val client by lazy {
        HttpClient() {
I'm getting Koin error when I remove use of lazy....looking to fix that (hopefully) and see if that "resolves" the original issue.....but seems like this is bug since those version updates (also updated https://youtrack.jetbrains.com/issue/KTOR-1087)
❤️ 1
the koin error (
InstanceCreationException: Could not create instance for [Single:'com.surrus.common.remote.PeopleInSpaceApi
) seems in turn to be related to another
Copy code
InvalidMutabilityException: mutation attempt of frozen com.surrus.common.remote.PeopleInSpaceApi@
r

russhwolf

09/30/2020, 3:45 PM
Are you hitting something Koin-related from a background thread? Koin tries to keep its initialization main-threaded to avoid unexpected freezing.
j

John O'Reilly

09/30/2020, 3:46 PM
I don't believe I am....pretty much using same pattern as used in KaMPKit
but will do closer comparison again and see if I can see any difference
seems to be triggered by changing
HttpClient
to not use
by lazy
(but again KaMPKit is also doing that so not sure yet what difference is)
so, I guess the change to not use
by lazy
means that the
HttpClient
instance is being created when
PeopleInSpaceApi
is created ....triggering
mutation attempt of frozen com.surrus.common.remote.PeopleInSpaceApi
looks like flow is a little different in KaMPKit in that it has
Copy code
breedModel = BreedModel()
in
NativeViewModel
....and
BreedModel
then uses Koin to inject dependencies (like
KtorApi
)
(anyway, going off topic from original
Ktor
issue 🙂 )
r

russhwolf

09/30/2020, 4:41 PM
I think it's relevant, because it may be an issue of application logic and K/N threading rather than a ktor thing
j

John O'Reilly

09/30/2020, 5:39 PM
I wonder if fact that the repository class is being created in Swift code is a factor
l

louiscad

09/30/2020, 5:40 PM
Can you check it's init on the main thread in Swift code?
j

John O'Reilly

09/30/2020, 5:40 PM
yeah, checked that
looks like a few concurrency related changes made to
HttpClient
for 1.4.1 release https://github.com/ktorio/ktor/commit/3471ee7e00a0fdf09375d9d16617d5540cc02a9a#diff-cac393be67723a02e3a8cc96c0f298a5
jetbrains folks have reproduced issue https://youtrack.jetbrains.com/issue/KTOR-1087
n

nrobi

11/07/2020, 8:51 PM
Facing the same issue, @John O'Reilly did you find out anything more in the last month?
j

John O'Reilly

11/07/2020, 8:52 PM
No, still haven't updated to that version.....that issue still seems to be open
r

ribesg

11/08/2020, 4:18 PM
The Ktor client 1.4+ never worked outside the main thread
👍 1
n

nrobi

11/08/2020, 4:53 PM
Yupp, I was getting first
IncorrectDereferenceException: illegal attempt to access non-shared object from other thread
, which makes sense and was fixed by explicitly freezing the KtorApi Then I was hitting
InvalidMutabilityException
, converted the
KtorApi
to an object and made it
@ThreadLocal
, but the
InvalidMutabilityException
still occured. • The IncorrectDereferenceException should be fixed by: https://github.com/ktorio/ktor/pull/2092, with the new release • I still don’t quite get, why
KtorApi
needs to be explicitly frozen, if it’s an object • If I’m correct even if
@ThreadLocal
introduces side-effects/new issues, it should’ve fixed
InvalidMutabilityException
r

ribesg

11/09/2020, 11:29 AM
Just make your request calls on the main thread.
9 Views