IncorrectDereferenceException on iOS with HttpClient

ktorVersion = “1.4.1”
coroutinesVersion = “1.3.9-native-mt-2”

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 in iOS

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

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.

Github issue : [native] IncorrectDereferenceException on iOS with HttpClient · Issue #2372 · Kotlin/kotlinx.coroutines · GitHub

1 Like

I have exactly the same problem with ktorVersion = "1.6.0" and coroutines “1.5.0-native-mt”.

@TomWayne I think the issue was fixed as you can see in the GitHub link above. are you passing a mutable object when making the request? iOS does not allow passing mutable objects between threads.