Skip to content

kotlin.coroutines.jvm.internal.CompletedContinuation cannot be cast to kotlinx.coroutines.internal.DispatchedContinuation #2719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Mygod opened this issue May 19, 2021 · 4 comments

Comments

@Mygod
Copy link

Mygod commented May 19, 2021

I am seeing the following error reported by users. I cannot reproduce this personally. What can I do to investigate this error?

Fatal Exception: kotlinx.coroutines.CoroutinesInternalError
Fatal exception in coroutines machinery for AwaitContinuation(DispatchedContinuation[Dispatchers.IO, Continuation at be.mygod.librootkotlinx.RootServer.execute(RootServer.kt:255)@868b2dc]){Completed}@d3b5fe5. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
kotlinx.coroutines.DispatchedTask.handleFatalException (DispatchedTask.java:144)
kotlinx.coroutines.DispatchedTask.run (DispatchedTask.java:115)
kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely (CoroutineScheduler.java:571)
kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask (CoroutineScheduler.java:750)
kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker (CoroutineScheduler.java:678)
kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run (CoroutineScheduler.java:665)
Caused by java.lang.ClassCastException
kotlin.coroutines.jvm.internal.CompletedContinuation cannot be cast to kotlinx.coroutines.internal.DispatchedContinuation
kotlinx.coroutines.CoroutineDispatcher.releaseInterceptedContinuation (CoroutineDispatcher.java:108)
kotlin.coroutines.jvm.internal.ContinuationImpl.releaseIntercepted (ContinuationImpl.java:118)
kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith (BaseContinuationImpl.java:39)
kotlinx.coroutines.DispatchedTask.run (DispatchedTask.java:106)
kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely (CoroutineScheduler.java:571)
kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask (CoroutineScheduler.java:750)
kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker (CoroutineScheduler.java:678)
kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run (CoroutineScheduler.java:665)

Related code: https://github.com/Mygod/VPNHotspot/blob/v2.11.6/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt#L255

@Togrias
Copy link

Togrias commented May 20, 2021

Do you or the users happen to use value (inline) classes? Including kotlin 1.5 Result?

@Mygod
Copy link
Author

Mygod commented May 20, 2021

Value class yes but as far as I'm concerned it is not used in this part of the code. Not sure about Result so presumably not.

@Togrias
Copy link

Togrias commented May 21, 2021

I'm encountering similar issues on a multiplatform project. No problems on javascript or native, only jvm causes issues. I suspect a compiler issue.

Update: I refactored Result into my own normal class Result2 and the issue seemed to go away. Seems like I'm right that it's a kotlin compiler issue and not a coroutines issue. Try refactoring value classes into normal classes?

See: https://youtrack.jetbrains.com/issue/KT-45685

@qwwdfsad
Copy link
Member

This is indeed a regression in Kotlin 1.5.0, unfortunately, we cannot workaround it on kotlinx.coroutines site.

The root cause of this issue is the compiler trying to optimize any boxing of value classes, but in some places coroutines code generation relies on it (e.g. resumeWith(Result)), so just replacing Result with the same handwritten type resolves the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants