https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

Amritansh

11/10/2020, 10:18 PM
I am trying to make a post request using Ktor HttpClient in commonMain on a singleThread using Dispatcher.Default and I am getting this exception on iOS
Copy code
Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared <object>@c1c6c8 from other thread
    at 0   shared                              0x000000010ac441bd kfun:kotlin.Throwable#<init>(kotlin.String?){} + 93 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Throwable.kt:23:37)
    at 1   shared                              0x000000010ac3d0ab kfun:kotlin.Exception#<init>(kotlin.String?){} + 91 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Exceptions.kt:23:44)
    at 2   shared                              0x000000010ac3d26b kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 91 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Exceptions.kt:34:44)
    at 3   shared                              0x000000010ac7006b kfun:kotlin.native.IncorrectDereferenceException#<init>(kotlin.String){} + 91 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/native/Runtime.kt:30:36)
    at 4   shared                              0x000000010ac7438d ThrowIllegalObjectSharingException + 621 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt:98:11)
    at 5   shared                              0x000000010ad5a002 _ZN12_GLOBAL__N_128throwIllegalSharingExceptionEP9ObjHeader + 34
    at 6   shared                              0x000000010ad5e52e _ZNK27BackRefFromAssociatedObject3refEv + 78
    at 7   shared                              0x000000010ad44bd4 _ZN12_GLOBAL__N_111toKotlinImpEP11objc_objectP13objc_selectorPP9ObjHeader + 52
    at 8   shared                              0x000000010ad87c81 Kotlin_Interop_refFromObjC + 65
    at 9   shared                              0x000000010aff65ea _696f2e6b746f723a6b746f722d636c69656e742d696f73_knbridge28 + 330 (/opt/buildAgent/work/a85294440dc5c6e/ktor-client/ktor-client-ios/darwin/src/io/ktor/client/engine/ios/IosResponseReader.kt:100:14)
    at 10  shared                              0x000000010afff37a _696f2e6b746f723a6b746f722d636c69656e742d696f73_kncfun30 + 74
    at 11  CFNetwork                           0x00007fff22f31ade _CFNetworkHTTPConnectionCacheSetLimit + 207606
    at 12  Foundation                          0x00007fff25752d51 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
    at 13  Foundation                          0x00007fff25752c49 -[NSBlockOperation main] + 98
    at 14  Foundation                          0x00007fff25755bd6 __NSOPERATION_IS_INVOKING_MAIN__ + 17
    at 15  Foundation                          0x00007fff25751e49 -[NSOperation start] + 731
    at 16  Foundation                          0x00007fff25756530 __NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION__ + 17
    at 17  Foundation                          0x00007fff2575603a __NSOQSchedule_f + 182
    at 18  libdispatch.dylib                   0x000000010b75a3c9 _dispatch_block_async_invoke2 + 83
    at 19  libdispatch.dylib                   0x000000010b74bd48 _dispatch_client_callout + 8
    at 20  libdispatch.dylib                   0x000000010b759de6 _dispatch_main_queue_callback_4CF + 1500
    at 21  CoreFoundation                      0x00007fff23bd4049 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    at 22  CoreFoundation                      0x00007fff23bceca9 __CFRunLoopRun + 2329
    at 23  CoreFoundation                      0x00007fff23bce066 CFRunLoopRunSpecific + 438
    at 24  GraphicsServices                    0x00007fff384c0bb0 GSEventRunModal + 65
    at 25  UIKitCore                           0x00007fff48092d4d UIApplicationMain + 1621
    at 26  iosApp-sample                       0x000000010a8b905b main + 75 (/User/iosApp-sample/iosApp-sample/AppDelegate.swift:4:7)
    at 27  libdyld.dylib                       0x00007fff5227ec25 start + 1
This is what my implementation looks like
Copy code
fun postRequest() {
        MainScope().launch {
            withContext(Dispatchers.Default) {
                 HttpClient().post("URL"){
                   body = "some json"
                 }
            }
        }
    }
This request is working fine for the Android application and only throws the exception in iOS. Also if I try to run HttpClient().post on the main thread, it works fine on iOS and I am successfully able to send the request. ktorVersion = "1.4.1" coroutinesVersion = "1.3.9-native-mt-2" Based on this documentationhttps://github.com/Kotlin/kotlinx.coroutines/blob/native-mt/kotlin-native-sharing.md#switching-threads for
"1.3.9-native-mt-2"
my understanding is it should work. Any help is appreciated!
t

Thomas

11/10/2020, 11:17 PM
Update Ktor to 1.4.2 and it should work
a

Amritansh

11/10/2020, 11:37 PM
thanks for replying Now I am getting a new error after the update😛
Copy code
Uncaught Kotlin exception: kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for DispatchedContinuation[MainDispatcher, Continuation @ $postRequest$lambda-1COROUTINE$1]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
    at 0   dogtag                              0x0000000103e8730c kfun:kotlin.Error#<init>(kotlin.String?;kotlin.Throwable?){} + 124 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Exceptions.kt:14:63)
    at 1   dogtag                              0x0000000103ffa913 kfun:kotlinx.coroutines.CoroutinesInternalError#<init>(kotlin.String;kotlin.Throwable){} + 115 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/Exceptions.common.kt:28:77)
    at 2   dogtag                              0x0000000104042bfa kfun:kotlinx.coroutines.DispatchedTask#handleFatalException(kotlin.Throwable?;kotlin.Throwable?){} + 938 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt:93:22)
    at 3   dogtag                              0x00000001040427e8 kfun:kotlinx.coroutines.DispatchedTask#run(){} + 3512 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt:64:13)
    at 4   dogtag                              0x000000010406b0f5 kfun:kotlinx.coroutines.DarwinMainDispatcher.dispatch$lambda-0#internal + 85 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/nativeDarwin/src/Dispatchers.kt:35:19)
    at 5   dogtag                              0x000000010406b4ab kfun:kotlinx.coroutines.DarwinMainDispatcher.$dispatch$lambda-0$FUNCTION_REFERENCE$43.invoke#internal + 59 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/nativeDarwin/src/Dispatchers.kt:34:51)
    at 6   dogtag                              0x000000010406b50b kfun:kotlinx.coroutines.DarwinMainDispatcher.$dispatch$lambda-0$FUNCTION_REFERENCE$43.$<bridge-UNN>invoke(){}#internal + 59 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/nativeDarwin/src/Dispatchers.kt:34:51)
    at 7   dogtag                              0x000000010406c5c4 _6f72672e6a6574627261696e732e6b6f746c696e783a6b6f746c696e782d636f726f7574696e65732d636f7265_knbridge8 + 180 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/nativeDarwin/src/Dispatchers.kt:34:51)
    at 8   libdispatch.dylib                   0x0000000104983dd4 _dispatch_call_block_and_release + 12
    at 9   libdispatch.dylib                   0x0000000104984d48 _dispatch_client_callout + 8
    at 10  libdispatch.dylib                   0x0000000104992de6 _dispatch_main_queue_callback_4CF + 1500
    at 11  CoreFoundation                      0x00007fff23bd4049 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    at 12  CoreFoundation                      0x00007fff23bceca9 __CFRunLoopRun + 2329
    at 13  CoreFoundation                      0x00007fff23bce066 CFRunLoopRunSpecific + 438
    at 14  GraphicsServices                    0x00007fff384c0bb0 GSEventRunModal + 65
    at 15  UIKitCore                           0x00007fff48092d4d UIApplicationMain + 1621
    at 16  iosApp-sample                       0x0000000103b0405b main + 75 (/Users/atripathi/DogTag/iosApp-sample/iosApp-sample/AppDelegate.swift:4:7)
    at 17  libdyld.dylib                       0x00007fff5227ec25 start + 1
Caused by: kotlin.native.IncorrectDereferenceException: Trying to access top level value not marked as @ThreadLocal or @SharedImmutable from non-main thread
    at 0   dogtag                              0x0000000103e8e52d kfun:kotlin.Throwable#<init>(kotlin.String?){} + 93 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Throwable.kt:23:37)
    at 1   dogtag                              0x0000000103e8741b kfun:kotlin.Exception#<init>(kotlin.String?){} + 91 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Exceptions.kt:23:44)
    at 2   dogtag                              0x0000000103e875db kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 91 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Exceptions.kt:34:44)
    at 3   dogtag                              0x0000000103eba3db kfun:kotlin.native.IncorrectDereferenceException#<init>(kotlin.String){} + 91 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/native/Runtime.kt:30:36)
    at 4   dogtag                              0x0000000103edbb61 ThrowIncorrectDereferenceException + 129 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/native/internal/RuntimeUtils.kt:103:11)
    at 5   dogtag                              0x0000000103fc5c89 CheckIsMainThread + 25
    at 6   dogtag                              0x00000001041a57e3 kfun:io.ktor.http.<get-HeaderFieldValueSeparators>#internal + 19 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-http/common/src/io/ktor/http/HeaderValueWithParameters.kt:10:9)
    at 7   dogtag                              0x00000001041a6e24 kfun:io.ktor.http.checkNeedEscape#internal + 356 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-http/common/src/io/ktor/http/HeaderValueWithParameters.kt:86:13)
    at 8   dogtag                              0x00000001041a65d5 kfun:io.ktor.http.HeaderValueWithParameters#toString(){}kotlin.String + 2101 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-http/common/src/io/ktor/http/HeaderValueWithParameters.kt:41:21)
    at 9   dogtag                              0x00000001041fb0f7 kfun:io.ktor.client.engine#mergeHeaders(io.ktor.http.Headers;io.ktor.http.content.OutgoingContent;kotlin.Function2<kotlin.String,kotlin.String,kotlin.Unit>){} + 3095 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/engine/Utils.kt:45:37)
    at 10  dogtag                              0x000000010423adfe kfun:io.ktor.client.engine.ios.IosClientEngine.$executeCOROUTINE$0#invokeSuspend(kotlin.Result<kotlin.Any?>){}kotlin.Any? + 5630 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-ios/darwin/src/io/ktor/client/engine/ios/IosClientEngine.kt:40:13)
    at 11  dogtag                              0x000000010423c2a4 kfun:io.ktor.client.engine.ios.IosClientEngine#execute(io.ktor.client.request.HttpRequestData){}io.ktor.client.request.HttpResponseData + 308 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-ios/darwin/src/io/ktor/client/engine/ios/IosClientEngine.kt:21:22)
    at 12  dogtag                              0x00000001041f813b kfun:io.ktor.client.engine.HttpClientEngine.$executeWithinCallContext$lambda-1COROUTINE$11.invokeSuspend#internal + 731 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/engine/HttpClientEngine.kt:86:13)
    at 13  dogtag                              0x0000000103eafc16 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl#resumeWith(kotlin.Result<kotlin.Any?>){} + 758 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/coroutines/ContinuationImpl.kt:30:39)
    at 14  dogtag                              0x00000001040424ff kfun:kotlinx.coroutines.DispatchedTask#run(){} + 2767 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt:39:50)
    at 15  dogtag                              0x0000000103ff5ecf kfun:kotlinx.coroutines.EventLoopImplBase#processNextEvent(){}kotlin.Long + 799 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/EventLoop.common.kt:274:18)
    at 16  dogtag                              0x000000010405a228 kfun:kotlinx.coroutines#runEventLoop(kotlinx.coroutines.EventLoop?;kotlin.Function0<kotlin.Boolean>){} + 872 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Builders.kt:80:40)
    at 17  dogtag                              0x0000000104062b85 kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.start$lambda-0#internal + 389 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Workers.kt:49:17)
    at 18  dogtag                              0x0000000104062d5b kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.$start$lambda-0$FUNCTION_REFERENCE$35.invoke#internal + 59 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Workers.kt:47:24)
    at 19  dogtag                              0x0000000104062dbb kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.$start$lambda-0$FUNCTION_REFERENCE$35.$<bridge-UNN>invoke(){}#internal + 59 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Workers.kt:47:24)
    at 20  dogtag                              0x0000000103ebdfe7 WorkerLaunchpad + 183 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt:69:54)
    at 21  dogtag                              0x0000000103fbb09f _ZN6Worker19processQueueElementEb + 3135
    at 22  dogtag                              0x0000000103fba446 _ZN12_GLOBAL__N_113workerRoutineEPv + 54
    at 23  libsystem_pthread.dylib             0x00007fff524602eb _pthread_body + 126
    at 24  libsystem_pthread.dylib             0x00007fff52463249 _pthread_start + 66
    at 25  libsystem_pthread.dylib             0x00007fff5245f40d thread_start + 13
(lldb)
a

arnis71

11/11/2020, 8:41 AM
ktor calls for native target should only be done from the ui thread. to do this in common module just make dedicated ktor dispatcher and make it IO for jvm and Main for native
t

Thomas

11/11/2020, 8:48 AM
Ktor on iOS should work on Dispatchers.Default as well
I'm using that and it works fine
a

Amritansh

11/11/2020, 1:56 PM
@Thomas can you share your dependency version for coroutine ?
t

Thomas

11/11/2020, 6:33 PM
@Amritansh I used a manually built version of Ktor that worked. The fix was supposed to be in Ktor 1.4.2 but unfortunately this is a new issue. See: https://github.com/ktorio/ktor/pull/2193
7 Views