question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlinx.coroutines.StandaloneCoroutine@28a82d8

See original GitHub issue

works on android, breaks on ios

code:

fun triggerNetworkCall(){
      GlobalScope.apply {
            launch(Background) {
                try {
                    val data = repository.getSettings()
                } catch (t: Throwable) {
                    Logger.d("ktor", t.message?:"null")
                }
            }
        }
}
 suspend fun getSettings(): String? {
        val response =  apiConfig.getReferralData()
        return response
}
suspend fun getReferralData(): String? {
       val localClient = HttpClient(PlatformHttpClient.httpClientEngine){
            install(JsonFeature)
        }
        return localClient.post {
            url {
                protocol = URLProtocol.HTTPS
                host = "postman-echo.com"
                encodedPath = "post"
            }
            contentType(ContentType.Application.Json)
            body = "{}"
            HttpMethod.Post
        }
}

This post request works in android but fails in iOS with following stack trace:

Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlinx.coroutines.StandaloneCoroutine@1a8b4e8

whereas a get request works on both platforms. Following is the function being used:

suspend fun getReferralData(): String? {
       val localClient = HttpClient(PlatformHttpClient.httpClientEngine){
            install(JsonFeature)
        }

        val address = Url("https://postman-echo.com/get?foo1=bar1&foo2=bar2")
        return localClient.get {
            url {
                url(address.toString())
            }
        }
}

Dispatchers definition: iOS

internal actual val Main: CoroutineDispatcher = NsQueueDispatcher(dispatch_get_main_queue())

internal actual val Background: CoroutineDispatcher = Main


internal class NsQueueDispatcher(
    private val dispatchQueue: dispatch_queue_t
) : CoroutineDispatcher() {
    override fun dispatch(context: CoroutineContext, block: Runnable) {
        dispatch_async(dispatchQueue) {
            block.run()
        }
    }
}

android

internal actual val Main: CoroutineDispatcher = Dispatchers.Main

internal actual val Background: CoroutineDispatcher = Dispatchers.Default

dependencies

commonMain {

            dependencies {
                implementation "org.jetbrains.kotlin:kotlin-stdlib-common:1.3.61"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.14.0"
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.2-1.3.60"
                implementation "co.touchlab:stately:0.9.4"
                implementation "co.touchlab:stately-collections:0.9.4"
                implementation "io.ktor:ktor-client-core:1.2.6"
                implementation "io.ktor:ktor-client-json:1.2.6"
                implementation "io.ktor:ktor-client-logging:1.2.6"
                implementation "io.ktor:ktor-client-serialization:1.2.6"
                implementation "com.github.aakira:napier:1.1.0"

            }
        }

        androidMain {
            dependencies {
                implementation "org.jetbrains.kotlin:kotlin-stdlib:1.3.61"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.14.0"
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2-1.3.60"
                implementation "io.ktor:ktor-client-core-jvm:1.2.6"
                implementation "io.ktor:ktor-client-json-jvm:1.2.6"
                implementation "io.ktor:ktor-client-logging-jvm:1.2.6"
                implementation "io.ktor:ktor-client-serialization-jvm:1.2.6"
                implementation "io.ktor:ktor-client-android:1.2.6"
                implementation "com.github.aakira:napier-android:1.1.0"
            }
        }

        iosMain {
            dependencies {
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:0.14.0"
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.3.2-1.3.60"

                implementation "io.ktor:ktor-client-core-native:1.2.6"
                implementation "io.ktor:ktor-client-json-native:1.2.6"
                implementation "io.ktor:ktor-client-logging-native:1.2.6"
                implementation "io.ktor:ktor-client-serialization-native:1.2.6"
                implementation "io.ktor:ktor-client-ios:1.2.6"
                implementation "com.github.aakira:napier-ios:1.1.0"
            }
        }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:23 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
sdtahericommented, Aug 26, 2020

This issue happens on version 1.4.0 while on 1.3.2-1.4.0-rc everything works fine.

7reactions
xiaobailong24commented, Aug 31, 2020

Having a very similar issue on 1.4.0 when doing a simple get request on ios: Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlinx.coroutines.ChildHandleNode@31dca08 reverting back to 1.3.2-1.4.0-rc works like a charm.

same error with me.

@joaquim-verges I solve it.

kotlin_version=1.4.0
kotlinx_coroutines_version=1.3.9-native-mt
kotlinx_ktor_version=1.4.0
kotlinx_serialization_version=1.0.0-RC
// ...
sourceSets {
        val commonMain by sourceSets.getting {
            dependencies {
                api("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinx_coroutines_version")
                api("org.jetbrains.kotlinx:kotlinx-serialization-core:$kotlinx_serialization_version")
                api("io.ktor:ktor-client-core:$kotlinx_ktor_version")
                api("io.ktor:ktor-client-json:$kotlinx_ktor_version")
                api("io.ktor:ktor-client-serialization:$kotlinx_ktor_version")
            }
        }

        val iosMain by sourceSets.getting {
            dependencies {
                implementation("io.ktor:ktor-client-ios:$kotlinx_ktor_version")
            }
        }
        val iosX64Main by sourceSets.getting {
            dependencies {
            }
        }
        val iosArm64Main by sourceSets.getting {
            dependencies {
            }
        }
        val androidMain by sourceSets.getting {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinx_coroutines_version")
                implementation("io.ktor:ktor-client-android:$kotlinx_ktor_version")
            }
        }
    }

interface IUserApi {
    suspend fun users(): List<User>
}

expect val httpClientEngine: HttpClientEngine

class UserApi : IUserApi {

    private val baseUrl = "https://jsonplaceholder.typicode.com"
    private val client = HttpClient(httpClientEngine) {
        install(JsonFeature) {
            serializer = KotlinxSerializer(json = kotlinx.serialization.json.Json {
                isLenient = false
                ignoreUnknownKeys = true
                allowSpecialFloatingPointValues = true
                useArrayPolymorphism = false
            })
        }
    }

    override suspend fun users(): List<User> {
        return client.get {
            setupCall(Endpoint.USERS)
        }
    }

    private fun HttpRequestBuilder.setupCall(endpoint: Endpoint) {
        url {
            takeFrom(urlString = baseUrl)
            encodedPath += endpoint.path
        }
    }
}

import io.ktor.client.engine.*
import io.ktor.client.engine.ios.*

actual val httpClientEngine: HttpClientEngine
    get() = Ios.create()

Read more comments on GitHub >

github_iconTop Results From Across the Web

kotlin.native.concurrent.InvalidMutabilityException - YouTrack
I am trying to upload an image as multiform data using KMM. For android, it's working fine, and able to upload it. But...
Read more >
kotlin.native.concurrent.InvalidMutabilityException: mutation ...
I find an issue filed here https://github.com/Kotlin/kotlinx.serialization/issues/1450. I tried one of the workaround i.e. ...
Read more >
InvalidMutabilityException - Kotlin Programming Language
Exception thrown whenever we attempt to mutate frozen objects. Parameters. where - a frozen object that was attempted to mutate. Constructors. Native.
Read more >
kotlin.native.concurrent.InvalidMutabilityException
kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlinx.coroutines.StandaloneCoroutine@28a82d8.
Read more >
The weird and wonderful world of threading in Kotlin-Native
kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen com.example.project.MyClass@fe10a8.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found