Hello, In a Kotlin multiplatform SDK, when trying to send a POST request and building http request b...
j
Hello, In a Kotlin multiplatform SDK, when trying to send a POST request and building http request body, I have some random crash EXC_BAD_ACCESS(KERN_INVALID_ADDRESS)
Copy code
Thread
0x6bea58 serialize + 11 (CollectRequestBodyDto.kt:11)
0x6882e4 encodeSerializableValue + 69 (StreamingJsonEncoder.kt:69)
0x6623e8 serialize + 69 (CollectionSerializers.kt:69)
0x6882e4 encodeSerializableValue + 69 (StreamingJsonEncoder.kt:69)
0x6bdda8 serialize + 6 (CollectRequestBodyDto.kt:6)
0x6882e4 encodeSerializableValue + 69 (StreamingJsonEncoder.kt:69)
0x678c68 encodeToString + 32 (JsonStreams.kt:32)
0x675f40 invokeSuspend + 95 (SerialFormat.kt:95)
0x676208 serialize + 54 (KotlinxSerializationConverter.kt:54)
0x63eef4 invokeSuspend + 40 (ContentConverter.kt:40)
0x640000 invoke + 142 (ContentNegotiation.kt:142)
0x6dc9c4 invoke + 1 ([K][Suspend]Functions:1)
0x635fa4 invokeSuspend + 78 (KtorCallContexts.kt:78)
0x6361c8 invoke + 77 (KtorCallContexts.kt:77)
0x6d6a0c invoke + 1 ([K][Suspend]Functions:1)
0x6032ac loop + 120 (SuspendFunctionGun.kt:120)
0x602f8c proceed + 78 (SuspendFunctionGun.kt:78)
0x62b8c0 invokeSuspend + 41 (PipelineContext.kt:41)
0x62bbd0 invoke + 149 (HttpCallValidator.kt:149)
0x6d6a0c invoke + 1 ([K][Suspend]Functions:1)
0x6032ac loop + 120 (SuspendFunctionGun.kt:120)
0x602f8c proceed + 78 (SuspendFunctionGun.kt:78)
0x6316d0 invokeSuspend + 41 (PipelineContext.kt:41)
0x631918 invoke + 40 (HttpRequestLifecycle.kt:40)
0x6dc8f0 invoke + 1 ([K][Suspend]Functions:1)
0x631c20 invokeSuspend + 27 (HttpRequestLifecycle.kt:27)
0x632130 invoke + 20 (HttpRequestLifecycle.kt:20)
0x6d6bb4 invoke + 1 ([K][Suspend]Functions:1)
0x631804 invoke + 40 (HttpRequestLifecycle.kt:40)
0x6d6a0c invoke + 1 ([K][Suspend]Functions:1)
0x6032ac loop + 120 (SuspendFunctionGun.kt:120)
0x602f8c proceed + 78 (SuspendFunctionGun.kt:78)
0x603158 execute + 98 (SuspendFunctionGun.kt:98)
0x60181c execute + 74 (Pipeline.kt:74)
0x61c7e8 invokeSuspend + 193 (HttpClient.kt:193)
0x63c414 invokeSuspend + 190 (HttpClient.kt:190)
0x6d1510 invokeSuspend + 111 (HttpStatement.kt:111)
0x6d17c8 invoke + 42 (TheWireRemoteDataSource.kt:42)
0x6dc8f0 invoke + 1 ([K][Suspend]Functions:1)
0x6d0d18 invokeSuspend + 67 (TheWireRemoteDataSource.kt:67)
0x6d09c0 invokeSuspend + 65 (TheWireRemoteDataSource.kt:65)
0x6d0be4 sendEvents + 63 (TheWireRemoteDataSource.kt:63)
0x6cfd58 invokeSuspend + 94 (TheWireQueueEventsCollector.kt:94)
0x6cffc8 sendRequest + 97 (TheWireQueueEventsCollector.kt:97)
0x6d01e4 invokeSuspend + 72 (TheWireQueueEventsCollector.kt:72)
0x6d0720 invoke + 66 (TheWireQueueEventsCollector.kt:66)
0x6d6938 invoke + 1 ([K][Suspend]Functions:1)
0x5281e4 invokeSuspend + 72 (IntrinsicsNative.kt:72)
0x526fa8 resumeWith + 50 (ContinuationImpl.kt:50)
0x5c605c run + 1 (Result.kt:1)
0x5c6f60 run + 115 (LimitedDispatcher.kt:115)
0x5d182c invokeSuspend + 106 (MultithreadedDispatchers.kt:106)
0x526fa8 resumeWith + 50 (ContinuationImpl.kt:50)
0x5c605c run + 1 (Result.kt:1)
0x5a6108 processNextEvent + 15 (ObjectiveCUtils.kt:15)
0x5ce1e0 runBlocking + 49 (EventLoop.common.kt:49)
0x5d1eb0 $<bridge-UNN>invoke(){} + 119 (MultithreadedDispatchers.kt:119)
0x6d4d08 invoke + 1 ([K][Suspend]Functions:1)
0x785fe0 Worker::processQueueElement(bool) + 6900
0x785ab0 (anonymous namespace)::workerRoutine(void*) + 5572
0x606c _pthread_start + 136
0x10d8 thread_start + 8
I have omitted some fields here, but basically it seems that the crash is produced when trying to serialize the sealed class 'Default':
Copy code
@Serializable
internal data class CollectRequestBodyDto(
    @SerialName("events") val events: List<EventDto>
)

@Serializable
internal data class EventDto(
    ...
    @SerialName("data") val data: DataDto,
)


@Serializable
internal sealed class DataDto {
    @Serializable
    data class Default(
            @SerialName("category") val category: String? = null,
            @SerialName("event") val event: String? = null,
            ...
    ) : DataDto()

    @Serializable
    data class Search(
        ...
    ) : DataDto()
}
Looking at the stack trace, it seems that the crash is located at this line: https://github.com/Kotlin/kotlinx.serialization/blob/71b5d5ee99f953b60756c4aaa2ac8e12c3fae605/formats/json/commonMain/src/kotlinx/serialization/json/internal/StreamingJsonEncoder.kt#L69
Copy code
override fun <T> encodeSerializableValue(serializer: SerializationStrategy<T>, value: T) {
        encodePolymorphically(serializer, value) { discriminatorName, serialName ->
            polymorphicDiscriminator = discriminatorName   --> Crash is located here
            polymorphicSerialName = serialName
        }
    }
To provide more context, this is working without issues in Android and failing randomly in iOS, and it is executed inside a background process that is sending analytics events. To solve the situation, I will try to avoid sealed classes and build the json in some other way (like buildJsonObject), but I would like to know any hint about the root cause of this issue. This is working for 99% of the users, but randomly failing from time to time. I am not able to reproduce the issue but I see error logs in crashlytics.... Any hint? Thanks!