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 kotlin.collections.HashMap

See original GitHub issue

Describe the bug

  • kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.collections.HashMap

To Reproduce If I execute the same Request from the same HTTP Client, the Request executes Successfully with the following config:

HttpClient {
                defaultRequest {
                    header("Authorization", "Bearer ${token.access_token}")
                }
            }

but when I add

install(JsonFeature)  {
                    serializer =  KotlinxSerializer(
            Json {
                isLenient = true
                ignoreUnknownKeys = true
            }
        )
}

and use like following, then following StackTrace is printed with said Exception.

HttpClient {
                defaultRequest {
                    header("Authorization", "Bearer ${token.access_token}")
                }
               install(JsonFeature)  {
                    serializer = kotlinxSerializer
                }
            }

Expected behavior Response Should be parsed properly without any hashmap freeze.

StackTrace:

kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.collections.HashMap@2be0d88
    at 0   SpotiFlyer                          0x00000001080fb4bf kfun:kotlin.Throwable#<init>(kotlin.String?){} + 95 (/Users/teamcity2/buildAgent/work/11ac87a349af04d5/runtime/src/main/kotlin/kotlin/Throwable.kt:23:37)
    at 1   SpotiFlyer                          0x00000001080f37cd kfun:kotlin.Exception#<init>(kotlin.String?){} + 93 (/Users/teamcity2/buildAgent/work/11ac87a349af04d5/runtime/src/main/kotlin/kotlin/Exceptions.kt:23:44)
    at 2   SpotiFlyer                          0x00000001080f3a3d kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 93 (/Users/teamcity2/buildAgent/work/11ac87a349af04d5/runtime/src/main/kotlin/kotlin/Exceptions.kt:34:44)
    at 3   SpotiFlyer                          0x000000010812afcd kfun:kotlin.native.concurrent.InvalidMutabilityException#<init>(kotlin.String){} + 93 (/Users/teamcity2/buildAgent/work/11ac87a349af04d5/runtime/src/main/kotlin/kotlin/native/concurrent/Freezing.kt:22:60)
    at 4   SpotiFlyer                          0x000000010812c7cf ThrowInvalidMutabilityException + 431 (/Users/teamcity2/buildAgent/work/11ac87a349af04d5/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt:93:11)
    at 5   SpotiFlyer                          0x0000000108223770 MutationCheck + 128
    at 6   SpotiFlyer                          0x000000010810d8b2 kfun:kotlin.collections.HashMap.<set-length>#internal + 82 (/Users/teamcity2/buildAgent/work/11ac87a349af04d5/runtime/src/main/kotlin/kotlin/collections/HashMap.kt:16:17)
    at 7   SpotiFlyer                          0x00000001081126b1 kfun:kotlin.collections.HashMap#addKey(1:0){}kotlin.Int + 1233 (/Users/teamcity2/buildAgent/work/11ac87a349af04d5/runtime/src/main/kotlin/kotlin/collections/HashMap.kt:292:36)
    at 8   SpotiFlyer                          0x000000010810eaa5 kfun:kotlin.collections.HashMap#put(1:0;1:1){}1:1? + 357 (/Users/teamcity2/buildAgent/work/11ac87a349af04d5/runtime/src/main/kotlin/kotlin/collections/HashMap.kt:68:21)
    at 9   SpotiFlyer                          0x0000000108684f43 kfun:kotlinx.serialization.json.internal.DescriptorSchemaCache#set(kotlinx.serialization.descriptors.SerialDescriptor;kotlinx.serialization.json.internal.DescriptorSchemaCache.Key<0:0>;0:0){0§<kotlin.Any>} + 771 (/opt/buildAgent/work/b2fef8360e1bcf3d/formats/json/commonMain/src/kotlinx/serialization/json/internal/SchemaCache.kt:23:58)
    at 10  SpotiFlyer                          0x00000001086852f0 kfun:kotlinx.serialization.json.internal.DescriptorSchemaCache#getOrPut(kotlinx.serialization.descriptors.SerialDescriptor;kotlinx.serialization.json.internal.DescriptorSchemaCache.Key<0:0>;kotlin.Function0<0:0>){0§<kotlin.Any>}0:0 + 576 (/opt/buildAgent/work/b2fef8360e1bcf3d/formats/json/commonMain/src/kotlinx/serialization/json/internal/SchemaCache.kt:29:9)
    at 11  SpotiFlyer                          0x00000001086878c3 kfun:kotlinx.serialization.json.internal.StreamingJsonDecoder.getJsonElementIndex#internal + 899 (/opt/buildAgent/work/b2fef8360e1bcf3d/formats/json/commonMain/src/kotlinx/serialization/json/internal/StreamingJsonDecoder.kt:113:30)
    at 12  SpotiFlyer                          0x0000000108688288 kfun:kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeObjectIndex#internal + 664 (/opt/buildAgent/work/b2fef8360e1bcf3d/formats/json/commonMain/src/kotlinx/serialization/json/internal/StreamingJsonDecoder.kt:143:36)
    at 13  SpotiFlyer                          0x0000000108686e16 kfun:kotlinx.serialization.json.internal.StreamingJsonDecoder#decodeElementIndex(kotlinx.serialization.descriptors.SerialDescriptor){}kotlin.Int + 502 (/opt/buildAgent/work/b2fef8360e1bcf3d/formats/json/commonMain/src/kotlinx/serialization/json/internal/StreamingJsonDecoder.kt:75:30)
    at 14  SpotiFlyer                          0x0000000107e42de9 kfun:com.shabinder.common.models.spotify.Playlist.$serializer#deserialize(kotlinx.serialization.encoding.Decoder){}com.shabinder.common.models.spotify.Playlist + 8137
    at 15  SpotiFlyer                          0x000000010868304e kfun:kotlinx.serialization.json.internal#decodeSerializableValuePolymorphic@kotlinx.serialization.json.JsonDecoder(kotlinx.serialization.DeserializationStrategy<0:0>){0§<kotlin.Any?>}0:0 + 2798 (/opt/buildAgent/work/b2fef8360e1bcf3d/formats/json/commonMain/src/kotlinx/serialization/json/internal/Polymorphic.kt:63:29)
    at 16  SpotiFlyer                          0x00000001086860ca kfun:kotlinx.serialization.json.internal.StreamingJsonDecoder#decodeSerializableValue(kotlinx.serialization.DeserializationStrategy<0:0>){0§<kotlin.Any?>}0:0 + 202 (/opt/buildAgent/work/b2fef8360e1bcf3d/formats/json/commonMain/src/kotlinx/serialization/json/internal/StreamingJsonDecoder.kt:32:16)
    at 17  SpotiFlyer                          0x0000000108662261 kfun:kotlinx.serialization.json.Json#decodeFromString(kotlinx.serialization.DeserializationStrategy<0:0>;kotlin.String){0§<kotlin.Any?>}0:0 + 753 (/opt/buildAgent/work/b2fef8360e1bcf3d/formats/json/commonMain/src/kotlinx/serialization/json/Json.kt:95:28)
    at 18  SpotiFlyer                          0x00000001086a87e9 kfun:io.ktor.client.features.json.serializer.KotlinxSerializer#read(io.ktor.client.call.TypeInfo;io.ktor.utils.io.core.Input){}kotlin.Any + 1081 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-features/ktor-client-json/ktor-client-serialization/common/src/io/ktor/client/features/json/serializer/**KotlinxSerializer.kt:**37:21)
    at 19  SpotiFlyer                          0x000000010860c0d5 kfun:io.ktor.client.features.json.JsonFeature.Feature.$install$lambda-1COROUTINE$1.invokeSuspend#internal + 2805 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-features/ktor-client-json/common/src/io/ktor/client/features/json/**JsonFeature.kt**:161:53)
    at 20  SpotiFlyer                          0x000000010860cb7a kfun:io.ktor.client.features.json.JsonFeature.Feature.$install$lambda-1COROUTINE$1.invoke#internal + 394 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-features/ktor-client-json/common/src/io/ktor/client/features/json/**JsonFeature.kt**:155:78)
    at 21  SpotiFlyer                          0x00000001085361e9 kfun:io.ktor.util.pipeline.SuspendFunctionGun.$loop$<anonymous>_2COROUTINE$8.invokeSuspend#internal + 697 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-utils/common/src/io/ktor/util/pipeline/SuspendFunctionGun.kt:17:2)
    at 22  SpotiFlyer                          0x000000010853678d kfun:io.ktor.util.pipeline.SuspendFunctionGun.$loop$<anonymous>_2COROUTINE$8.invoke#internal + 269 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-utils/common/src/io/ktor/util/pipeline/SuspendFunctionGun.kt:16:23)
    at 23  SpotiFlyer                          0x0000000108534a9f kfun:io.ktor.util.pipeline.SuspendFunctionGun.loop#internal + 1295 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-utils/common/src/io/ktor/util/pipeline/SuspendFunctionGun.kt:160:99)
    at 24  SpotiFlyer                          0x000000010853404f kfun:io.ktor.util.pipeline.SuspendFunctionGun#proceed(){}1:0 + 415 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-utils/common/src/io/ktor/util/pipeline/SuspendFunctionGun.kt:116:13)
    at 25  SpotiFlyer                          0x0000000108534250 kfun:io.ktor.util.pipeline.SuspendFunctionGun#proceedWith(1:0){}1:0 + 224 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-utils/common/src/io/ktor/util/pipeline/SuspendFunctionGun.kt:126:16)
    at 26  SpotiFlyer                          0x00000001085c8904 kfun:io.ktor.client.features.HttpCallValidator.Companion.$install$lambda-2COROUTINE$19.invokeSuspend#internal + 948 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/features/HttpCallValidator.kt:125:21)
    at 27  SpotiFlyer                          0x00000001085c91fa kfun:io.ktor.client.features.HttpCallValidator.Companion.$install$lambda-2COROUTINE$19.invoke#internal + 394 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/features/HttpCallValidator.kt:123:61)
    at 28  SpotiFlyer                          0x00000001085361e9 kfun:io.ktor.util.pipeline.SuspendFunctionGun.$loop$<anonymous>_2COROUTINE$8.invokeSuspend#internal + 697 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-utils/common/src/io/ktor/util/pipeline/SuspendFunctionGun.kt:17:2)
    at 29  SpotiFlyer                          0x000000010853678d kfun:io.ktor.util.pipeline.SuspendFunctionGun.$loop$<anonymous>_2COROUTINE$8.invoke#internal + 269 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-utils/common/src/io/ktor/util/pipeline/SuspendFunctionGun.kt:16:23)

Environment

  • Kotlin version: 1.4.32
  • Library version: 1.2.0
  • Kotlin platforms: Native(IOS)
  • Gradle version: 6.8

Issue Analytics

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

github_iconTop GitHub Comments

27reactions
sandwwraithcommented, May 14, 2021

Another workaround is to disable useAlternativeNames in Json configuration

11reactions
sven4allcommented, Jun 20, 2021

When can we expect the 1.2.2 update with this fix included? This issue is blocking some other library upgrades we should do in our project, so would be really nice if an release can be made in the near future.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kodein + Ktor = mutation attempt of frozen kotlin.collections ...
Ktor freezes itself and all of its configuration in order to ensure that it can be used across threads in Kotlin/Native.
Read more >
kotlin.native.concurrent.InvalidMutabilityException : KT-34311
Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.collections.HashMap@83fe2288 ; Type, Bug B.
Read more >
[Solved]-kotlin.native.concurrent.InvalidMutabilityException
Coding example for the question kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.collections.HashMap@3ee0f08 in KMM ...
Read more >
Kotlin.Native.Concurrent.Invalidmutabilityexception - ADocLib
Everything.Enter search request.Visible to issue readers InvalidMutabilityException: mutation attempt of frozen kotlin.collections.HashMap@83fe2288. Native: ...
Read more >
Immutability and concurrency in Kotlin/Native
String , kotlin.Int , and other primitive types, along with AtomicInt and AtomicReference , are frozen by default. If a mutating operation is ......
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