kotlinx.coroutines: java.lang.ClassCastException: kotlin.coroutines.jvm.internal.CompletedContinuation cannot be cast to kotlinx.coroutines.internal.DispatchedContinuation

21:05:05	ERROR:	AndroidRuntime : kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for CancellableContinuation(DispatchedContinuation[Dispatchers.IO, Continuation at kotlinx.coroutines.channels.AbstractChannel.receiveCatching-JP2dKIU(AbstractChannel.kt:632)@8d8755]){Completed}@c3cb56a. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
21:05:05	ERROR:	AndroidRuntime : at kotlinx.coroutines.DispatchedTask.handleFatalException(DispatchedTask.kt:144)
21:05:05	ERROR:	AndroidRuntime : at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:115)
21:05:05	ERROR:	AndroidRuntime : at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
21:05:05	ERROR:	AndroidRuntime : at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
21:05:05	ERROR:	AndroidRuntime : at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
21:05:05	ERROR:	AndroidRuntime : at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
21:05:05	ERROR:	AndroidRuntime : Caused by: java.lang.ClassCastException: kotlin.coroutines.jvm.internal.CompletedContinuation cannot be cast to kotlinx.coroutines.internal.DispatchedContinuation
21:05:05	ERROR:	AndroidRuntime : at kotlinx.coroutines.CoroutineDispatcher.releaseInterceptedContinuation(CoroutineDispatcher.kt:108)
21:05:05	ERROR:	AndroidRuntime : at kotlin.coroutines.jvm.internal.ContinuationImpl.releaseIntercepted(ContinuationImpl.kt:118)
21:05:05	ERROR:	AndroidRuntime : at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:39)
21:05:05	ERROR:	AndroidRuntime : at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
21:05:05	ERROR:	AndroidRuntime : ... 4 more

kotlin version: 1.5.10 coroutines version: 1.5.0

I’m not sure how to reproduce, it sometimes happens during espresso tests

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 22
  • Comments: 20 (8 by maintainers)

Commits related to this issue

Most upvoted comments

In our android app it crashes when using stateflow/sharedflow + debounce. So I was able to repro this with just launching ton of different coroutines which use stateflow+debounce.

class TestRunner(private val scope: CoroutineScope) {

    fun run() {
        scope.launch {
            runStressTest()
        }
    }

    suspend fun runStressTest() = withContext(Dispatchers.Default) {
        repeat(100) { a ->
            repeat(100) { b ->
                launch {
                    runStressTestOnce((a + 1) * 10, (b + 1) * 10)
                }
            }
        }
    }

    suspend fun runStressTestOnce(delay: Int, debounce: Int) = coroutineScope {
        val stateflow = MutableStateFlow(0)
        launch {
            repeat(Integer.MAX_VALUE) { i ->
                stateflow.emit(i)
                delay(delay.toLong())
            }
        }
        var last = 0
        stateflow.debounce(debounce.toLong()).collect { i ->
            if (i - last > 100) {
                println("${delay}x${debounce} => $i")
                last = i
            }
        }
    }
}

Then I ran TestRunner(scope).run() in Application.

it’s always (5 out 5 launches) crashing with the following stacktrace:

java.lang.IllegalStateException: Inconsistent state CancellableContinuation(DispatchedContinuation[Dispatchers.Default, Continuation at kotlinx.coroutines.flow.StateFlowImpl.collect(StateFlow.kt:353)@deec246]){Active}@5fe3c07
        at kotlinx.coroutines.internal.DispatchedContinuation.tryReleaseClaimedContinuation(DispatchedContinuation.kt:169)
        at kotlinx.coroutines.CancellableContinuationImpl.releaseClaimedReusableContinuation(CancellableContinuationImpl.kt:323)
        at kotlinx.coroutines.CancellableContinuationImpl.getResult(CancellableContinuationImpl.kt:280)
        at kotlinx.coroutines.channels.AbstractSendChannel.sendSuspend(AbstractChannel.kt:1152)
        at kotlinx.coroutines.channels.AbstractSendChannel.send(AbstractChannel.kt:136)
        at kotlinx.coroutines.channels.ChannelCoroutine.send(Unknown Source:2)
        at kotlinx.coroutines.flow.FlowKt__DelayKt$debounceInternal$1$values$1$invokeSuspend$$inlined$collect$1.emit(Collect.kt:135)
        at kotlinx.coroutines.flow.StateFlowImpl.collect(StateFlow.kt:348)
        at kotlinx.coroutines.flow.StateFlowImpl$collect$1.invokeSuspend(Unknown Source:15)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
    
    java.lang.IllegalStateException: Inconsistent state CancellableContinuation(DispatchedContinuation[Dispatchers.Default, Continuation at kotlinx.coroutines.flow.StateFlowImpl.collect(StateFlow.kt:353)@deec246]){Active}@5fe3c07
        at kotlinx.coroutines.internal.DispatchedContinuation.tryReleaseClaimedContinuation(DispatchedContinuation.kt:169)
        at kotlinx.coroutines.CancellableContinuationImpl.releaseClaimedReusableContinuation(CancellableContinuationImpl.kt:323)
        at kotlinx.coroutines.CancellableContinuationImpl.getResult(CancellableContinuationImpl.kt:280)
        at kotlinx.coroutines.channels.AbstractSendChannel.sendSuspend(AbstractChannel.kt:1152)
        at kotlinx.coroutines.channels.AbstractSendChannel.send(AbstractChannel.kt:136)
        at kotlinx.coroutines.channels.ChannelCoroutine.send(Unknown Source:2)
        at kotlinx.coroutines.flow.FlowKt__DelayKt$debounceInternal$1$values$1$invokeSuspend$$inlined$collect$1.emit(Collect.kt:135)
        at kotlinx.coroutines.flow.StateFlowImpl.collect(StateFlow.kt:348)
        at kotlinx.coroutines.flow.StateFlowImpl$collect$1.invokeSuspend(Unknown Source:15)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)

@qwwdfsad it is, run it multiple times and sometimes it fails with exact classcastexception: to increase your chances (i don’t know why) run more coroutines:

suspend fun runTest() = withContext(Dispatchers.Default) {
        repeat(300) { a ->
            repeat(300) { b ->
                launch {
                    runTestOnce(a + 50, b + 50)
                }
            }
        }
    }

produces

Exception in thread "DefaultDispatcher-worker-8" kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for CancellableContinuation(DispatchedContinuation[Dispatchers.Default, Continuation at kotlinx.coroutines.flow.StateFlowImpl.collect(StateFlow.kt:353)@6476f03]){Cancelled}@10ee2b07. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
	at kotlinx.coroutines.DispatchedTask.handleFatalException(DispatchedTask.kt:144)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:115)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
Caused by: java.lang.ClassCastException: class kotlin.coroutines.jvm.internal.CompletedContinuation cannot be cast to class kotlinx.coroutines.internal.DispatchedContinuation (kotlin.coroutines.jvm.internal.CompletedContinuation and kotlinx.coroutines.internal.DispatchedContinuation are in unnamed module of loader 'app')
	at kotlinx.coroutines.CoroutineDispatcher.releaseInterceptedContinuation(CoroutineDispatcher.kt:108)
	at kotlin.coroutines.jvm.internal.ContinuationImpl.releaseIntercepted(ContinuationImpl.kt:118)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:39)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
	... 4 more

in most of the runs (and it fails faster)

It’s reproducible even without launching android app just by adding:

fun main() {
    TestRunner(CoroutineScope(Dispatchers.Default + Job())).run()
    Thread.sleep(1_000_000)
}

The fix will be released with Kotlin 1.5.20-RC (~roughly next week)

We still don’t have reproducible case but all of our crashes happen in background. Is it same for everyone?

It would be really helpful if anybody could provide a reproduced even if it’s a set of espresso tests that fail 1 in 1000 times. Without that, it’s quite hard to do anything meaningful about this error