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

John O'Reilly

08/18/2020, 8:57 PM
Using Ktor on client for Kotlin Multiplatform project and getting exception with 1.4 set of libraries when running on iOS with
native-mt
kotlinx coroutines (and also updated to
CIO
engine as it seems is now required to use this). Following is set of dependencies I'm using (also updated in following branch - https://github.com/joreilly/BikeShare/tree/1.4). (exception in thread....)
Copy code
const val kotlin = "1.4.0"
    const val kotlinxCoroutines = "1.3.9-native-mt"
    const val ktor = "1.4.0"
    const val kotlinxSerialization = "1.0.0-RC"
Copy code
HttpClient: REQUEST: <http://api.citybik.es/v2/networks/galway>
HttpClient: METHOD: HttpMethod(value=GET)
HttpClient: COMMON HEADERS
HttpClient: -> Accept: application/json
HttpClient: -> Accept-Charset: UTF-8
HttpClient: CONTENT HEADERS
HttpClient: BODY Content-Type: null
HttpClient: BODY START
HttpClient: 
HttpClient: BODY END
HttpClient: RESPONSE: 200 OK
HttpClient: METHOD: HttpMethod(value=GET)
HttpClient: FROM: <http://api.citybik.es/v2/networks/galway>
HttpClient: COMMON HEADERS
HttpClient: -> Access-Control-Allow-Origin: *
HttpClient: -> Content-Length: 5198
HttpClient: -> Content-Type: application/json
HttpClient: -> Date: Tue, 18 Aug 2020 20:50:16 GMT
HttpClient: -> Server: nginx/1.15.9 (Ubuntu)
HttpClient: -> Vary: Accept-Encoding
HttpClient: -> Via: kong/1.2.1
HttpClient: -> X-Kong-Proxy-Latency: 1
HttpClient: -> X-Kong-Upstream-Latency: 58
HttpClient: -> X-RateLimit-Limit-minute: 180
HttpClient: -> X-RateLimit-Remaining-minute: 179
HttpClient: BODY Content-Type: application/json
HttpClient: BODY START
Uncaught Kotlin exception: kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for DispatchedContinuation[MainDispatcher, Continuation @ $split$lambda-0$<anonymous>_3COROUTINE$2]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
    at 0   BikeShare                           0x000000010497915c kfun:kotlin.Error#<init>(kotlin.String?;kotlin.Throwable?){} + 124
    at 1   BikeShare                           0x0000000104af113c kfun:kotlinx.coroutines.CoroutinesInternalError#<init>(kotlin.String;kotlin.Throwable){} + 124
    at 2   BikeShare                           0x0000000104b3f3e9 kfun:kotlinx.coroutines.DispatchedTask#handleFatalException(kotlin.Throwable?;kotlin.Throwable?){} + 953
    at 3   BikeShare                           0x0000000104b3efc7 kfun:kotlinx.coroutines.DispatchedTask#run(){} + 3543
    at 4   BikeShare                           0x0000000104b76278 kfun:kotlinx.coroutines.DarwinMainDispatcher.dispatch$lambda-0#internal + 88
    at 5   BikeShare                           0x0000000104b7664e kfun:kotlinx.coroutines.DarwinMainDispatcher.$dispatch$lambda-0$FUNCTION_REFERENCE$41.invoke#internal + 62
    at 6   BikeShare                           0x0000000104b766ae kfun:kotlinx.coroutines.DarwinMainDispatcher.$dispatch$lambda-0$FUNCTION_REFERENCE$41.$<bridge-UNN>invoke(){}#internal + 62
    at 7   BikeShare                           0x0000000104b77767 _6f72672e6a6574627261696e732e6b6f746c696e783a6b6f746c696e782d636f726f7574696e65732d636f7265_knbridge8 + 183
    at 8   libdispatch.dylib                   0x000000010584cf11 _dispatch_call_block_and_release + 12
    at 9   libdispatch.dylib                   0x000000010584de8e _dispatch_client_callout + 8
    at 10  libdispatch.dylib                   0x000000010585bd97 _dispatch_main_queue_callback_4CF + 1149
    at 11  CoreFoundation                      0x00007fff23da1869 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    at 12  CoreFoundation                      0x00007fff23d9c3b9 __CFRunLoopRun + 2041
    at 13  CoreFoundation                      0x00007fff23d9b8a4 CFRunLoopRunSpecific + 404
    at 14  GraphicsServices                    0x00007fff38c39bbe GSEventRunModal + 139
    at 15  UIKitCore                           0x00007fff49325968 UIApplicationMain + 1605
    at 16  BikeShare                           0x00000001048e371b main + 75 (/Users/joreilly/dev/github/BikeShare/ios/BikeShare/BikeShare/AppDelegate.swift:13:7)
    at 17  libdyld.dylib                       0x00007fff520ce1fd start + 1
    at 18  ???                                 0x0000000000000001 0x0 + 1
Caused by: kotlinx.coroutines.CompletionHandlerException: Exception in completion handler ChildCompletion[ChildHandle[DeferredCoroutine{Cancelled}@fa55c8], kotlin.Unit] for DeferredCoroutine{Cancelled}@fa55c8
    at 0   BikeShare                           0x000000010497931c kfun:kotlin.Exception#<init>(kotlin.String?;kotlin.Throwable?){} + 124
    at 1   BikeShare                           0x000000010497956c kfun:kotlin.RuntimeException#<init>(kotlin.String?;kotlin.Throwable?){} + 124
    at 2   BikeShare                           0x0000000104af109c kfun:kotlinx.coroutines.CompletionHandlerException#<init>(kotlin.String;kotlin.Throwable){} + 124
    at 3   BikeShare                           0x0000000104af6e06 kfun:kotlinx.coroutines.JobSupport.notifyCompletion#internal + 1606
    at 4   BikeShare                           0x0000000104af5a38 kfun:kotlinx.coroutines.JobSupport.completeStateFinalization#internal + 1464
    at 5   BikeShare                           0x0000000104af3bc9 kfun:kotlinx.coroutines.JobSupport.finalizeFinishingState#internal + 1705
    at 6   BikeShare                           0x0000000104b00ad4 kfun:kotlinx.coroutines.JobSupport.tryMakeCompletingSlowPath#internal + 2036
    at 7   BikeShare                           0x0000000104b001b0 kfun:kotlinx.coroutines.JobSupport.tryMakeCompleting#internal + 736
    at 8   BikeShare                           0x0000000104affbd2 kfun:kotlinx.coroutines.JobSupport#makeCompletingOnce(kotlin.Any?){}kotlin.Any? + 642
    at 9   BikeShare                           0x0000000104ad42e6 kfun:kotlinx.coroutines.AbstractCoroutine#resumeWith(kotlin.Result<1:0>){} + 310
    at 10  BikeShare                           0x00000001049a214a kfun:kotlin.coroutines.native.internal.BaseContinuationImpl#resumeWith(kotlin.Result<kotlin.Any?>){} + 1450
    at 11  BikeShare                           0x0000000104b3eaa8 kfun:kotlinx.coroutines.DispatchedTask#run(){} + 2232
    at 12  BikeShare                           0x0000000104b76278 kfun:kotlinx.coroutines.DarwinMainDispatcher.dispatch$lambda-0#internal + 88
    ... and 14 more common stack frames skipped
Caused by: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlinx.coroutines.AwaitAll.AwaitAllNode@224ce68
    at 0   BikeShare                           0x000000010498052d kfun:kotlin.Throwable#<init>(kotlin.String?){} + 93
    at 1   BikeShare                           0x000000010497926b kfun:kotlin.Exception#<init>(kotlin.String?){} + 91
    at 2   BikeShare                           0x00000001049794bb kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 91
    at 3   BikeShare                           0x00000001049af10b kfun:kotlin.native.concurrent.InvalidMutabilityException#<init>(kotlin.String){} + 91
    at 4   BikeShare                           0x00000001049b09c2 ThrowInvalidMutabilityException + 690
    at 5   BikeShare                           0x0000000104aaa43c MutationCheck + 108
    at 6   BikeShare                           0x0000000104ad6936 kfun:kotlinx.coroutines.AwaitAll.AwaitAllNode.<set-handle>#internal + 102
    at 7   BikeShare                           0x0000000104ad5b08 kfun:kotlinx.coroutines.AwaitAll.await#internal + 1240
    at 8   BikeShare                           0x0000000104ad5071 kfun:kotlinx.coroutines.$awaitAllCOROUTINE$1#invokeSuspend(kotlin.Result<kotlin.Any?>){}kotlin.Any? + 1425
    at 9   BikeShare                           0x0000000104ad5344 kfun:kotlinx.coroutines#awaitAll@kotlin.collections.Collection<kotlinx.coroutines.Deferred<0:0>>(){0§<kotlin.Any?>}kotlin.collections.List<0:0> + 244
    at 10  BikeShare                           0x0000000104c5f3b6 kfun:io.ktor.util.$split$lambda-0COROUTINE$3.invokeSuspend#internal + 2358
    at 11  BikeShare                           0x00000001049a1e96 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl#resumeWith(kotlin.Result<kotlin.Any?>){} + 758
    at 12  BikeShare                           0x0000000104b3ece2 kfun:kotlinx.coroutines.DispatchedTask#run(){} + 2802
    at 13  BikeShare                           0x0000000104b76278 kfun:kotlinx.coroutines.DarwinMainDispatcher.dispatch$lambda-0#internal + 88
    ... and 14 more common stack frames skipped
(lldb)
e

e5l

08/19/2020, 4:05 AM
@Vsevolod Tolstopyatov [JB]
j

John O'Reilly

08/20/2020, 11:13 AM
Is there any examples of Ktor 1.4 working with
native-mt
kotlinx coroutines on iOS or macOS ....have tried a few different things but still getting crashes?
one other thing I noticed is that Ktor 1.4 seems to have dependency on
1.3.8-native-mt-1.4.0-rc1.3.8-native-mt-1.4.0-rc
(which is then "mapped" to
1.3.9-native-mt
)...maybe clutching at straws but just in case that's an issue
Copy code
+--- io.ktor:ktor-client-core:1.4.0
|    \--- io.ktor:ktor-client-core-jvm:1.4.0
|         +--- org.jetbrains.kotlin:kotlin-stdlib:1.4.0 (*)
|         +--- org.slf4j:slf4j-api:1.7.30
|         +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.4.0
|         +--- org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8-native-mt-1.4.0-rc -> 1.3.9-native-mt (*)
🙌 1
t

tylerwilson

08/20/2020, 10:02 PM
I also had a crash on iOS when using coroutines 1.3.9. When I switched to 1.3.8-native-mt-1.4.0-rc everything started working fine. I did not switch to CIO, leaving it to the default engine (NSURLSession on iOS I am pretty sure). That is interesting how you traced through the versions. That is part of what messed me up was the naming of the version for coroutines.
w

william

08/24/2020, 11:51 AM
do you guys have any more info about fixing this?
j

John O'Reilly

08/24/2020, 11:52 AM
@russhwolf seems to have isolated this to issue with ktor logging.....if I disable that then seems to work fine (along with use of CIO on iOS at least for me)
e

e5l

08/24/2020, 11:53 AM
Could you file an YT issue about that?
r

russhwolf

08/24/2020, 11:53 AM
e

e5l

08/24/2020, 11:54 AM
Awesome, thanks
r

russhwolf

08/24/2020, 11:54 AM
I was able to get the default iOS engine working by disabling logging in that sample. Haven't tried CIO yet
j

John O'Reilly

08/24/2020, 11:54 AM
my changes using setup I mentioned are in following now https://github.com/joreilly/BikeShare
ktor/native-mt now seems to be happily working on both iOS and macOS (again, with logging disabled)