Hello, I’m experiencing runtime error when running...
# coroutines
s
Hello, I’m experiencing runtime error when running below code on kotlin 1.7.20
Copy code
class Foo {
    suspend fun bar() {
        foo()
    }

    suspend fun foo(): Long {
        delay(1000)
        return 1000L;
    }
}

suspend fun <T> process(fn: suspend () -> T, fn2: (T) -> Unit) {
    fn2(fn())
}


suspend fun main() {
    process(Foo()::bar) { println("Success") }
}
stack trace:
Copy code
Exception in thread "main" java.lang.ClassCastException: class java.lang.Long cannot be cast to class kotlin.Unit (java.lang.Long is in module java.base of loader 'bootstrap'; kotlin.Unit is in unnamed module of loader 'app')
	at aoc.leetcode.AppKt$main$3.invoke(App.kt:22)
	at aoc.leetcode.AppKt.process(App.kt:17)
	at aoc.leetcode.AppKt$process$1.invokeSuspend(App.kt)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:178)
	at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:166)
	at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:397)
	at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:431)
	at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:420)
	at kotlinx.coroutines.CancellableContinuationImpl.resumeUndispatched(CancellableContinuationImpl.kt:518)
	at kotlinx.coroutines.EventLoopImplBase$DelayedResumeTask.run(EventLoop.common.kt:500)
	at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:284)
	at kotlinx.coroutines.DefaultExecutor.run(DefaultExecutor.kt:108)
	at java.base/java.lang.Thread.run(Thread.java:829)
I think this problem occurs because functions that return immediately after a suspend function call are optimized not to create a
ContinuationImpl
. (Instead, it returns inner suspend function immediately without considering the type of it.) The above can be found through the decompiled code of the following three functions.
s
Nice find! I see the same issue on my machine. I think you should create a ticket for this at kotl.in/issue 🎫
s
Thank you! I was going to ask where to report it