Description
Describe the bug
The primary crash in our Kotlin Multiplatform project is related to the following force cast in DispatchedTask.kt
:
final override fun run() {
assert { resumeMode != MODE_UNINITIALIZED } // should have been set before dispatching
val taskContext = this.taskContext
var fatalException: Throwable? = null
try {
val delegate = delegate as DispatchedContinuation<T> // CRASH here: delegate is CompletedContinuation
val continuation = delegate.continuation
withContinuationContext(continuation, delegate.countOrElement) {
In crashlytics the exception is shown as follows:
Fatal Exception: kotlinx.coroutines.CoroutinesInternalError
Fatal exception in coroutines machinery for DispatchedContinuation[MainDispatcher, Continuation @ 0]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers Caused by: kotlin.ClassCastException: class kotlin.coroutines.native.internal.CompletedContinuation cannot be cast to class kotlinx.coroutines.internal.DispatchedContinuation
Now I understand this crash may be caused by invalid usage of the API, however a class cast exception should never occur. Also it is very hard to debug this way. As of yet, I have no clue if we cause this error ourselves or one of the libraries (e.g. KTOR) causes it.
I propose a solution where at the point the invalid call is made an IllegalStateException or similar exception is thrown, describing the invalid usage of the API, so the code can be fixed at that point.
Provide a Reproducer
I unfortunately don't know how to exactly reproduce this problem. I don't know which call causes this error.