Ktor: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen io.ktor.client.request.HttpRequestPipeline@545e18f8

Created on 19 Jan 2019  路  12Comments  路  Source: ktorio/ktor

Ktor Version

implementation("io.ktor:ktor-client-ios:1.1.1")

Ktor Engine Used(client or server and name)

Client

JVM Version, Operating System and Relevant Context

MBP 2017 JVM 1.8

Feedback

This exception was printed out,and the test can't print out the expected result

    suspend fun getDemoJSONDataFromNet(): String {


        val result = client.get<String>("https://en.wikipedia.org/wiki/Main_Page")

        Logger.d(tag, "the return data is $result")

        return result
    }

fun getDataIos(callBack: (result: String) -> Unit) {
    runBlocking {
       val job = async {
            val result = HttpUtil.getDemoJSONDataFromNet()
            Logger.d("SimpleIos","result value is $result")
            callBack(result)
        }
        job.await()
    }
}
    @Test
    fun testGetData() {
        getDataIos {
            println(it)
        }
    }



md5-dfd5c57e94575f8848597fc13aef230b



Instances of kotlin.Error, kotlin.RuntimeException and subclasses aren't propagated from Kotlin to Objective-C/Swift.
Other exceptions can be propagated as NSError if method has or inherits @Throws annotation.
Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen io.ktor.client.request.HttpRequestPipeline@2ef2a08
        at 0   app                                 0x000000010dba2896 kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception + 70
        at 1   app                                 0x000000010dba27b6 kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException + 70
        at 2   app                                 0x000000010dba40e6 kfun:kotlin.native.concurrent.InvalidMutabilityException.<init>(kotlin.String)kotlin.native.concurrent.InvalidMutabilityException + 70
        at 3   app                                 0x000000010dc3c6b8 ThrowInvalidMutabilityException + 280
        at 4   app                                 0x000000010dc5ee48 MutationCheck + 24
        at 5   app                                 0x000000010dce8a83 kfun:io.ktor.util.pipeline.Pipeline.<set-interceptors>#internal + 67
        at 6   app                                 0x000000010dce86a6 kfun:io.ktor.util.pipeline.Pipeline.notSharedInterceptorsList#internal + 70
        at 7   app                                 0x000000010dce84d6 kfun:io.ktor.util.pipeline.Pipeline.cacheInterceptors#internal + 710
        at 8   app                                 0x000000010dce6292 kfun:io.ktor.util.pipeline.Pipeline.sharedInterceptorsList#internal + 98
        at 9   app                                 0x000000010dce61ca kfun:io.ktor.util.pipeline.Pipeline.createContext$ktor-utils(#GENERIC_kotlin.Any;#GENERIC_kotlin.Any)io.ktor.util.pipeline.PipelineExecutor<#GENERIC_kotlin.Any> + 90
        at 10  app                                 0x000000010dce60f0 kfun:io.ktor.util.pipeline.Pipeline.execute(#GENERIC_kotlin.Any;#GENERIC_kotlin.Any)#GENERIC_kotlin.Any + 96
        at 11  app                                 0x000000010dd313a8 kfun:io.ktor.client.HttpClient.$execute$COROUTINE$1.invokeSuspend(kotlin.Result<kotlin.Any?>)kotlin.Any? + 296
        at 12  app                                 0x000000010dd31522 kfun:io.ktor.client.HttpClient.execute(io.ktor.client.request.HttpRequestBuilder)io.ktor.client.call.HttpClientCall + 130
        at 13  app                                 0x000000010dd32696 kfun:io.ktor.client.call.$call$COROUTINE$3.invokeSuspend(kotlin.Result<kotlin.Any?>)kotlin.Any? + 758
        at 14  app                                 0x000000010dd32852 kfun:[email protected].(kotlin.coroutines.SuspendFunction1<io.ktor.client.request.HttpRequestBuilder,kotlin.Unit>)io.ktor.client.call.HttpClientCall + 130
        at 15  app                                 0x000000010dd330eb kfun:[email protected].(io.ktor.client.request.HttpRequestBuilder)io.ktor.client.call.HttpClientCall + 123
        at 16  app                                 0x000000010db97f3f kfun:sample.HttpUtil.$getDemoJSONDataFromNet$COROUTINE$0.invokeSuspend(kotlin.Result<kotlin.Any?>)kotlin.Any? + 1551
        at 17  app                                 0x000000010db97821 kfun:sample.HttpUtil.getDemoJSONDataFromNet()kotlin.String + 113
        at 18  app                                 0x000000010dba0de9 kfun:sample.$getDataIos$lambda-1$lambda-0$COROUTINE$3.invokeSuspend(kotlin.Result<kotlin.Any?>)kotlin.Any? + 313
        at 19  app                                 0x000000010dbd4801 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl.resumeWith(kotlin.Result<kotlin.Any?>) + 385
        at 20  app                                 0x000000010dca7b89 kfun:kotlinx.coroutines.DispatchedTask.run() + 1129
        at 21  app                                 0x000000010dca92d6 kfun:kotlinx.coroutines.EventLoopImpl.processNextEvent()ValueType + 422
        at 22  app                                 0x000000010dccf7f7 kfun:kotlinx.coroutines.BlockingCoroutine.joinBlocking#internal + 263
        at 23  app                                 0x000000010dccf5d7 kfun:kotlinx.coroutines.runBlocking(kotlin.coroutines.CoroutineContext;kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,#GENERIC>)Generic + 983
        at 24  app                                 0x000000010dccfb4c kfun:kotlinx.coroutines.runBlocking$default(kotlin.coroutines.CoroutineContext;kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,#GENERIC>;kotlin.Int)Generic + 204
        at 25  app                                 0x000000010dba06e4 kfun:sample.getDataIos(kotlin.Function1<kotlin.String,kotlin.Unit>) + 100
        at 26  app                                 0x000000010dba0618 kfun:sample.main(kotlin.Array<kotlin.String>) + 216
        at 27  iosApp                              0x000000010d897774 $S6iosApp14ViewControllerC11viewDidLoadyyFTo + 36
        at 28  UIKitCore                           0x0000000112ea84e1 -[UIViewController loadViewIfRequired] + 1186
bug

Most helpful comment

Don't freeze your HttpClient, it doesn't work.

That means don't put it inside an object for example, or inside an instance of a class stored inside an object, etc

All 12 comments

Hi @wuseal, thanks for the report.
Could you change runBlocking to launch(Dispatchers.Unconfined)?

I'm seeing this same issue on 1.2.0-rc using the Native client on Linux. I've tried using launch(Dispatches.Unconfined) as suggested by @e5l and the exception appears to be the same:

Versions:
Multiplatform Plugin 1.3.31
io.ktor:ktor-client-core-native:1.2.0-rc
io.ktor:ktor-client-curl:1.2.0-rc
org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.2.1

kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen io.ktor.client.request.HttpRequestPipeline@18834a8
        at  (0x398d9a)
        at kfun:io.ktor.util.pipeline.Pipeline.execute(#GENERIC_kotlin.Any;#GENERIC_kotlin.Any)#GENERIC_kotlin.Any (0x363e9d)
        at kfun:io.ktor.client.HttpClient.$execute$COROUTINE$11.invokeSuspend#internal (0x374d60)
        at kfun:io.ktor.client.call.$call$COROUTINE$15.invokeSuspend#internal (0x374a5e)
        at kfun:[email protected].(io.ktor.client.request.HttpRequestBuilder)io.ktor.client.call.HttpClientCall (0x370c3e)
        at kfun:com.batchofcode.lambdaruntime.client.CommonAwsRuntimeClient.$processRequests$COROUTINE$1.invokeSuspend#internal (0x36c0b8)
        at kfun:com.batchofcode.lambdaruntime.client.CommonAwsRuntimeClient.$run$COROUTINE$0.invokeSuspend#internal (0x36b82c)
        at kfun:com.batchofcode.lambdaruntime.client.LambdaRuntimeClient.$run$lambda-0$COROUTINE$3.invokeSuspend#internal (0x28e65d)
        at kfun:kotlin.coroutines.native.internal.BaseContinuationImpl.resumeWith(kotlin.Result<kotlin.Any?>) (0x27aa64)
        at kfun:kotlinx.coroutines.resumeCancellable$kotlinx-coroutines-core@kotlin.coroutines.Continuation<#GENERIC>.(#GENERIC)Generic (0x27d90d)
        at kfun:kotlinx.coroutines.intrinsics.startCoroutineCancellable$kotlinx-coroutines-core@kotlin.coroutines.SuspendFunction1<#GENERIC,#GENERIC>.(#GENERIC;kotlin.coroutines.Continuation<#GENERIC>)Generic (0x28da9c)
        at kfun:kotlinx.coroutines.launch$default@kotlinx.coroutines.CoroutineScope.(kotlin.coroutines.CoroutineContext;kotlinx.coroutines.CoroutineStart;kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,kotlin.Unit>;kotlin.Int)kotlinx.coroutines.Job (0x28c4cf)
        at Konan_start (0x28bd0d)
        at Konan_run_start (0x28ba73)
        at Konan_main (0x28b9c7)
        at __libc_start_main (0x7f9ff76d7445)
        at  (0x260029)
        at  ((nil))
kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for DispatchedContinuation[Unconfined, Continuation @ $<init>$lambda-2$COROUTINE$6]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers; caused by kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlinx.atomicfu.AtomicRef@18393a8
        at kfun:kotlinx.coroutines.DispatchedTask.run() (0x27f54a)
        at kfun:kotlinx.coroutines.EventLoop.processUnconfinedEvent()ValueType (0x27e636)
        at kfun:kotlinx.coroutines.resumeCancellable$kotlinx-coroutines-core@kotlin.coroutines.Continuation<#GENERIC>.(#GENERIC)Generic (0x27d9b8)
        at kfun:kotlinx.coroutines.intrinsics.startCoroutineCancellable$kotlinx-coroutines-core@kotlin.coroutines.SuspendFunction1<#GENERIC,#GENERIC>.(#GENERIC;kotlin.coroutines.Continuation<#GENERIC>)Generic (0x28da9c)
        at kfun:kotlinx.coroutines.launch$default@kotlinx.coroutines.CoroutineScope.(kotlin.coroutines.CoroutineContext;kotlinx.coroutines.CoroutineStart;kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,kotlin.Unit>;kotlin.Int)kotlinx.coroutines.Job (0x28c4cf)
        at Konan_start (0x28bd0d)
        at Konan_run_start (0x28ba73)
        at Konan_main (0x28b9c7)
        at __libc_start_main (0x7f9ff76d7445)
        at  (0x260029)
        at  ((nil))

Having the same issue right now, with ktor 1.1.4 and 1.2.0-rc

Don't freeze your HttpClient, it doesn't work.

That means don't put it inside an object for example, or inside an instance of a class stored inside an object, etc

Thanks @Ribesg that totally was my problem.

Same here, thanks @Ribesg!

Having the same issue in version 1.2.2 only on IOS even if using ThreadLocal

Fixed in 1.2.4

Hello, World! The issue is marked as fixed but I can still reproduce it (with 1.2.4).

Here's my stacktrace:

Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen io.ktor.client.request.HttpRequestPipeline@8b60cd28
        at 0   sample-native-osx.kexe              0x000000010cd1b545 kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception + 85 (/Users/teamcity/buildAgent/work/4d622a065c544371/runtime/src/main/kotlin/kotlin/Exceptions.kt:23:44)
        at 1   sample-native-osx.kexe              0x000000010cd1a6c5 kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException + 85 (/Users/teamcity/buildAgent/work/4d622a065c544371/runtime/src/main/kotlin/kotlin/Exceptions.kt:34:44)
        at 2   sample-native-osx.kexe              0x000000010cd4dc45 kfun:kotlin.native.concurrent.InvalidMutabilityException.<init>(kotlin.String)kotlin.native.concurrent.InvalidMutabilityException + 85 (/Users/teamcity/buildAgent/work/4d622a065c544371/runtime/src/main/kotlin/kotlin/native/concurrent/Freezing.kt:22:60)
        at 3   sample-native-osx.kexe              0x000000010cd4f4d8 ThrowInvalidMutabilityException + 680 (/Users/teamcity/buildAgent/work/4d622a065c544371/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt:83:11)
        at 4   sample-native-osx.kexe              0x000000010d04da88 MutationCheck + 104
        at 5   sample-native-osx.kexe              0x000000010cee1dd0 kfun:io.ktor.util.pipeline.Pipeline.<set-interceptors>#internal + 96 (/opt/buildAgent/work/a85294440dc5c6e/ktor-utils/common/src/io/ktor/util/pipeline/Pipeline.kt:383:13)
        at 6   sample-native-osx.kexe              0x000000010cee22d5 kfun:io.ktor.util.pipeline.Pipeline.notSharedInterceptorsList#internal + 85 (/opt/buildAgent/work/a85294440dc5c6e/ktor-utils/common/src/io/ktor/util/pipeline/Pipeline.kt:410:9)
        at 7   sample-native-osx.kexe              0x000000010cee1431 kfun:io.ktor.util.pipeline.Pipeline.cacheInterceptors#internal + 673 (/opt/buildAgent/work/a85294440dc5c6e/ktor-utils/common/src/io/ktor/util/pipeline/Pipeline.kt:264:9)
        at 8   sample-native-osx.kexe              0x000000010cee2182 kfun:io.ktor.util.pipeline.Pipeline.sharedInterceptorsList#internal + 354 (/opt/buildAgent/work/a85294440dc5c6e/ktor-utils/common/src/io/ktor/util/pipeline/Pipeline.kt:397:13)
        at 9   sample-native-osx.kexe              0x000000010cede8a7 kfun:io.ktor.util.pipeline.Pipeline.createContext$ktor-utils(#GENERIC_kotlin.Any;#GENERIC_kotlin.Any)io.ktor.util.pipeline.PipelineExecutor<#GENERIC_kotlin.Any> + 263 (/opt/buildAgent/work/a85294440dc5c6e/ktor-utils/common/src/io/ktor/util/pipeline/Pipeline.kt:33:38)
        at 10  sample-native-osx.kexe              0x000000010cede6c1 kfun:io.ktor.util.pipeline.Pipeline.execute(#GENERIC_kotlin.Any;#GENERIC_kotlin.Any)#GENERIC_kotlin.Any + 273 (/opt/buildAgent/work/a85294440dc5c6e/ktor-utils/common/src/io/ktor/util/pipeline/Pipeline.kt:27:9)
        at 11  sample-native-osx.kexe              0x000000010cf2a12d kfun:io.ktor.client.HttpClient.$executeCOROUTINE$18.invokeSuspend(kotlin.Result<kotlin.Any?>)kotlin.Any? + 653 (/opt/buildAgent/work/a85294440dc5c6e/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt:141:25)
        at 12  sample-native-osx.kexe              0x000000010cf2a444 kfun:io.ktor.client.HttpClient.execute(io.ktor.client.request.HttpRequestBuilder)io.ktor.client.call.HttpClientCall + 308 (/opt/buildAgent/work/a85294440dc5c6e/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt:140:13)
        at 13  sample-native-osx.kexe              0x000000010cf3167e kfun:io.ktor.client.call.$callCOROUTINE$21.invokeSuspend(kotlin.Result<kotlin.Any?>)kotlin.Any? + 1390 (/opt/buildAgent/work/a85294440dc5c6e/ktor-client/ktor-client-core/common/src/io/ktor/client/call/HttpClientCall.kt:140:5)
        at 14  sample-native-osx.kexe              0x000000010cf319d4 kfun:[email protected].(kotlin.coroutines.SuspendFunction1<io.ktor.client.request.HttpRequestBuilder,kotlin.Unit>)io.ktor.client.call.HttpClientCall + 308 (/opt/buildAgent/work/a85294440dc5c6e/ktor-client/ktor-client-core/common/src/io/ktor/client/call/HttpClientCall.kt:139:9)
        at 15  sample-native-osx.kexe              0x000000010cf3499d kfun:[email protected].(io.ktor.client.request.HttpRequestBuilder)io.ktor.client.call.HttpClientCall + 301 (/opt/buildAgent/work/a85294440dc5c6e/ktor-client/ktor-client-core/common/src/io/ktor/client/call/utils.kt:30:76)
        at 16  sample-native-osx.kexe              0x000000010cfe033e kfun:org.jraf.klibqonto.internal.client.QontoService.$getOrganizationCOROUTINE$9.invokeSuspend(kotlin.Result<kotlin.Any?>)kotlin.Any? + 3246 (/Users/bod/gitrepo/klibqonto/library/src/commonMain/kotlin/org/jraf/klibqonto/internal/client/QontoService.kt:42:0)
        at 17  sample-native-osx.kexe              0x000000010cfe0e4b kfun:org.jraf.klibqonto.internal.client.QontoService.getOrganization()org.jraf.klibqonto.internal.api.model.organizations.ApiOrganizationEnvelope + 235 (/Users/bod/gitrepo/klibqonto/library/src/commonMain/kotlin/org/jraf/klibqonto/internal/client/QontoService.kt:41:13)
        at 18  sample-native-osx.kexe              0x000000010cfdad62 kfun:org.jraf.klibqonto.internal.client.QontoClientImpl.$getOrganizationCOROUTINE$4.invokeSuspend(kotlin.Result<kotlin.Any?>)kotlin.Any? + 594 (/Users/bod/gitrepo/klibqonto/library/src/commonMain/kotlin/org/jraf/klibqonto/internal/client/QontoClientImpl.kt:74:0)
        at 19  sample-native-osx.kexe              0x000000010cfdb0bb kfun:org.jraf.klibqonto.internal.client.QontoClientImpl.getOrganization()org.jraf.klibqonto.model.organizations.Organization + 235 (/Users/bod/gitrepo/klibqonto/library/src/commonMain/kotlin/org/jraf/klibqonto/internal/client/QontoClientImpl.kt:72:22)
        at 20  sample-native-osx.kexe              0x000000010cfee91b kfun:Sample.$main$lambda-1COROUTINE$3.invokeSuspend#internal + 731 (/Users/bod/gitrepo/klibqonto/samples/sample-native-osx/src/macosMain/kotlin/Sample.kt:73:53)
        at 21  sample-native-osx.kexe              0x000000010cd428ca kfun:kotlin.coroutines.native.internal.BaseContinuationImpl.resumeWith(kotlin.Result<kotlin.Any?>) + 730 (/Users/teamcity/buildAgent/work/4d622a065c544371/runtime/src/main/kotlin/kotlin/coroutines/ContinuationImpl.kt:26:0)
        at 22  sample-native-osx.kexe              0x000000010ce701ef kfun:kotlinx.coroutines.DispatchedTask.run() + 2783 (/opt/teamcity-agent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/Dispatched.kt:227:0)
        at 23  sample-native-osx.kexe              0x000000010ce75136 kfun:kotlinx.coroutines.EventLoopImplBase.processNextEvent()ValueType + 710 (/opt/teamcity-agent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/EventLoop.common.kt:270:20)
        at 24  sample-native-osx.kexe              0x000000010cea1042 kfun:kotlinx.coroutines.BlockingCoroutine.joinBlocking#internal + 1570 (/opt/teamcity-agent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Builders.kt:60:0)
        at 25  sample-native-osx.kexe              0x000000010cea0271 kfun:kotlinx.coroutines.runBlocking(kotlin.coroutines.CoroutineContext;kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,#GENERIC>)Generic + 1233 (/opt/teamcity-agent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Builders.kt:50:22)
        at 26  sample-native-osx.kexe              0x000000010cea07e4 kfun:kotlinx.coroutines.runBlocking$default(kotlin.coroutines.CoroutineContext;kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,#GENERIC>;kotlin.Int)Generic + 372 (/opt/teamcity-agent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Builders.kt:33:8)
        at 27  sample-native-osx.kexe              0x000000010cfedf8c kfun:Sample.main() + 188 (/Users/bod/gitrepo/klibqonto/samples/sample-native-osx/src/macosMain/kotlin/Sample.kt:70:9)
        at 28  sample-native-osx.kexe              0x000000010cfef055 kfun:main() + 117 (/Users/bod/gitrepo/klibqonto/samples/sample-native-osx/src/macosMain/kotlin/Sample.kt:160:21)

Ideas?

Using @Ribesg trick worked too for me (using a class instead of an object- but I understood this was not necessary after the fix?

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

Fixed in 1.4.1

Was this page helpful?
0 / 5 - 0 ratings