Ktor: Error Ktor running on background thread on iOS

Created on 31 Dec 2019  路  4Comments  路  Source: ktorio/ktor

Ktor Version and Engine Used (client or server and name)
Ktor client version 1.3.0-rc2
build.gradle:

ext.ktor_version = '1.3.0-rc2'
ext.kotlin_coroutines_version = '1.3.3-native-mt'

sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib-common')
                implementation ("io.ktor:ktor-client-core:$ktor_version") {
                    exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-core-common"
                }
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$kotlin_coroutines_version"
            }
        }
        iosMain {
            dependencies {
                implementation ("io.ktor:ktor-client-ios:$ktor_version") {
                    exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-core-native"
                    exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-core-common"
                }
                implementation ("io.ktor:ktor-client-core-native:$ktor_version") {
                    exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-core-native"
                    exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-core-common"
                }
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$kotlin_coroutines_version"
            }
        }
    }

Describe the bug
Executing an API request on the background thread results in kotlin.native.concurrent.InvalidMutabilityException
Full stacktrace

kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen io.ktor.client.request.HttpRequestPipeline@b5b748
        at 0   app                                 0x0000000105688a87 kfun:kotlin.Throwable.<init>(kotlin.String?)kotlin.Throwable + 87
        at 1   app                                 0x0000000105682215 kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception + 85
        at 2   app                                 0x0000000105681dd5 kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException + 85
        at 3   app                                 0x00000001056b6005 kfun:kotlin.native.concurrent.InvalidMutabilityException.<init>(kotlin.String)kotlin.native.concurrent.InvalidMutabilityException + 85
        at 4   app                                 0x00000001056b77d8 ThrowInvalidMutabilityException + 680
        at 5   app                                 0x00000001059a8c58 MutationCheck + 104
        at 6   app                                 0x00000001058c9400 kfun:io.ktor.util.pipeline.Pipeline.<set-interceptors>#internal + 96
        at 7   app                                 0x00000001058c98f5 kfun:io.ktor.util.pipeline.Pipeline.notSharedInterceptorsList#internal + 85
        at 8   app                                 0x00000001058c8a8a kfun:io.ktor.util.pipeline.Pipeline.cacheInterceptors#internal + 650
        at 9   app                                 0x00000001058c97a4 kfun:io.ktor.util.pipeline.Pipeline.sharedInterceptorsList#internal + 340
        at 10  app                                 0x00000001058c5f57 kfun:io.ktor.util.pipeline.Pipeline.createContext$ktor-utils(#GENERIC_kotlin.Any;#GENERIC_kotlin.Any)io.ktor.util.pipeline.PipelineExecutor<#GENERIC_kotlin.Any> + 263
        at 11  app                                 0x00000001058c5d71 kfun:io.ktor.util.pipeline.Pipeline.execute(#GENERIC_kotlin.Any;#GENERIC_kotlin.Any)#GENERIC_kotlin.Any + 273
        at 12  app                                 0x000000010590797a kfun:io.ktor.client.HttpClient.$executeCOROUTINE$6.invokeSuspend(kotlin.Result<kotlin.Any?>)kotlin.Any? + 666
        at 13  app                                 0x0000000105907cc4 kfun:io.ktor.client.HttpClient.execute(io.ktor.client.request.HttpRequestBuilder)io.ktor.client.call.HttpClientCall + 308
        at 14  app                                 0x000000010593bb7c kfun:io.ktor.client.statement.HttpStatement.$executeUnsafeCOROUTINE$35.invokeSuspend(kotlin.Result<kotlin.Any?>)kotlin.Any? + 956
        at 15  app                                 0x000000010593be8b kfun:io.ktor.client.statement.HttpStatement.executeUnsafe$ktor-client-core()io.ktor.client.statement.HttpResponse + 235
        at 16  app                                 0x000000010594b557 kfun:sample.ClientApi.$getQuote$lambda-0COROUTINE$0.invokeSuspend#internal + 6775
        at 17  app                                 0x00000001056a9ec8 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl.resumeWith(kotlin.Result<kotlin.Any?>) + 712
        at 18  app                                 0x00000001057ca07c kfun:kotlinx.coroutines.DispatchedTask.run() + 2780
        at 19  app                                 0x000000010577fa18 kfun:kotlinx.coroutines.EventLoopImplBase.processNextEvent()ValueType + 792
        at 20  app                                 0x00000001057e3e31 kfun:kotlinx.coroutines.runEventLoop$kotlinx-coroutines-core(kotlinx.coroutines.EventLoop?;kotlin.Function0<kotlin.Boolean>) + 881
        at 21  app                                 0x00000001057eb412 kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.start$lambda-0#internal + 402
        at 22  app                                 0x00000001057eb5fb kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.$start$lambda-0$FUNCTION_REFERENCE$71.invoke#internal + 59
        at 23  app                                 0x00000001057eb65b kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.$start$lambda-0$FUNCTION_REFERENCE$71.$<bridge-UNN>invoke()#internal + 59
        at 24  app                                 0x00000001056b73a1 WorkerLaunchpad + 177
        at 25  app                                 0x00000001059b1909 _ZN6Worker19processQueueElementEb + 2569
        at 26  app                                 0x00000001059b1eb6 _ZN12_GLOBAL__N_113workerRoutineEPv + 54
        at 27  libsystem_pthread.dylib             0x00007fff51bfe2eb _pthread_body + 126
        at 28  libsystem_pthread.dylib             0x00007fff51c01249 _pthread_start + 66
        at 29  libsystem_pthread.dylib             0x00007fff51bfd40d thread_start + 13

To Reproduce
Steps to reproduce the behavior:

  1. Clone sample app from https://github.com/aleksey-chugaev/ktor-mt-app
  2. Run iosApp in emulator
  3. See error

Expected behavior
Should successfully execute the request.

bug

Most helpful comment

@e5l Any updates on the native mt support? Would love to test this. I've been working on alternatives in the interim, but if this is happening soon, I'd rather not.

All 4 comments

@e5l Any updates on the native mt support? Would love to test this. I've been working on alternatives in the interim, but if this is happening soon, I'd rather not.

Any update on this in light of 1.4 related changes?

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

Yep, can't reproduce the problem with recent update.

Was this page helpful?
0 / 5 - 0 ratings