Ktor: ClassCastException launching

Created on 1 Jan 2020  路  8Comments  路  Source: ktorio/ktor

Ktor Version and Engine Used (client or server and name)
kotlin_version=1.3.61
ktor_version=1.3.0-rc2
coroutines_version=1.3.3-native-mt

Describe the bug
Calling testNetworkCall() from ViewController#viewDidLoad in iOS leads to a kotlin.ClassCastException

  GlobalScope.launch {
    HttpClient { }.get {
      url {
        takeFrom("https://api.basebeta.com")
        encodedPath = "/rankings"
      }
    }
  }

Full stacktrace

Uncaught Kotlin exception: kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for AwaitContinuation(Shareable[used]){HttpResponseData=(statusCode=200 OK)}@cb3ac8. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
        at 0   BaseBetaCommonApi                   0x0000000111c1ac63 kfun:kotlin.Error.<init>(kotlin.String?;kotlin.Throwable?)kotlin.Error + 115
        at 1   BaseBetaCommonApi                   0x0000000111d20703 kfun:kotlinx.coroutines.CoroutinesInternalError.<init>(kotlin.String;kotlin.Throwable)kotlinx.coroutines.CoroutinesInternalError + 115
        at 2   BaseBetaCommonApi                   0x0000000111d86cba kfun:kotlinx.coroutines.DispatchedTask.handleFatalException$kotlinx-coroutines-core(kotlin.Throwable?;kotlin.Throwable?) + 938
        at 3   BaseBetaCommonApi                   0x0000000111d868b0 kfun:kotlinx.coroutines.DispatchedTask.run() + 3568
        at 4   BaseBetaCommonApi                   0x0000000111d1b908 kfun:kotlinx.coroutines.EventLoopImplBase.processNextEvent()ValueType + 792
        at 5   BaseBetaCommonApi                   0x0000000111da26f1 kfun:kotlinx.coroutines.runEventLoop$kotlinx-coroutines-core(kotlinx.coroutines.EventLoop?;kotlin.Function0<kotlin.Boolean>) + 881
        at 6   BaseBetaCommonApi                   0x0000000111da9d12 kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.start$lambda-0#internal + 402
        at 7   BaseBetaCommonApi                   0x0000000111da9efb kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.$start$lambda-0$FUNCTION_REFERENCE$146.invoke#internal + 59
        at 8   BaseBetaCommonApi                   0x0000000111da9f5b kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.$start$lambda-0$FUNCTION_REFERENCE$146.$<bridge-UNN>invoke()#internal + 59
        at 9   BaseBetaCommonApi                   0x0000000111c4fd31 WorkerLaunchpad + 177
        at 10  BaseBetaCommonApi                   0x0000000112052d89 _ZN6Worker19processQueueElementEb + 2569
        at 11  BaseBetaCommonApi                   0x0000000112053336 _ZN12_GLOBAL__N_113workerRoutineEPv + 54
        at 12  libsystem_pthread.dylib             0x00007fff51bfe2eb _pthread_body + 126
        at 13  libsystem_pthread.dylib             0x00007fff51c01249 _pthread_start + 66
        at 14  libsystem_pthread.dylib             0x00007fff51bfd40d thread_start + 13
Caused by: kotlin.ClassCastException: kotlin.coroutines.native.internal.CompletedContinuation cannot be cast to kotlinx.coroutines.DispatchedContinuation
        at 0   BaseBetaCommonApi                   0x0000000111c21227 kfun:kotlin.Throwable.<init>(kotlin.String?)kotlin.Throwable + 87
        at 1   BaseBetaCommonApi                   0x0000000111c1a585 kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception + 85
        at 2   BaseBetaCommonApi                   0x0000000111c1a0c5 kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException + 85
        at 3   BaseBetaCommonApi                   0x0000000111c1ae65 kfun:kotlin.ClassCastException.<init>(kotlin.String?)kotlin.ClassCastException + 85
        at 4   BaseBetaCommonApi                   0x0000000111c6ba5b ThrowClassCastException + 939
        at 5   BaseBetaCommonApi                   0x0000000111d14ef8 kfun:kotlinx.coroutines.CoroutineDispatcher.releaseInterceptedContinuation(kotlin.coroutines.Continuation<#STAR>) + 216
        at 6   BaseBetaCommonApi                   0x0000000111c42400 kfun:kotlin.coroutines.native.internal.ContinuationImpl.releaseIntercepted() + 768
        at 7   BaseBetaCommonApi                   0x0000000111c42a62 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl.resumeWith(kotlin.Result<kotlin.Any?>) + 1234
        at 8   BaseBetaCommonApi                   0x0000000111e8cbf2 kfun:io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith#internal + 930
        at 9   BaseBetaCommonApi                   0x0000000111e8c773 kfun:io.ktor.util.pipeline.SuspendFunctionGun.loop#internal + 1699
        at 10  BaseBetaCommonApi                   0x0000000111e8e651 kfun:io.ktor.util.pipeline.SuspendFunctionGun.object-1.resumeWith#internal + 353
        at 11  BaseBetaCommonApi                   0x0000000111c42b28 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl.resumeWith(kotlin.Result<kotlin.Any?>) + 1432
        at 12  BaseBetaCommonApi                   0x0000000111e8cbf2 kfun:io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith#internal + 930
        at 13  BaseBetaCommonApi                   0x0000000111e8c773 kfun:io.ktor.util.pipeline.SuspendFunctionGun.loop#internal + 1699
        at 14  BaseBetaCommonApi                   0x0000000111e8e651 kfun:io.ktor.util.pipeline.SuspendFunctionGun.object-1.resumeWith#internal + 353
        at 15  BaseBetaCommonApi                   0x0000000111c42b28 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl.resumeWith(kotlin.Result<kotlin.Any?>) + 1432
        at 16  BaseBetaCommonApi                   0x0000000111e8cbf2 kfun:io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith#internal + 930
        at 17  BaseBetaCommonApi                   0x0000000111e8c773 kfun:io.ktor.util.pipeline.SuspendFunctionGun.loop#internal + 1699
        at 18  BaseBetaCommonApi                   0x0000000111e8e651 kfun:io.ktor.util.pipeline.SuspendFunctionGun.object-1.resumeWith#internal + 353
        at 19  BaseBetaCommonApi                   0x0000000111c42b28 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl.resumeWith(kotlin.Result<kotlin.Any?>) + 1432
        at 20  BaseBetaCommonApi                   0x0000000111d8659c kfun:kotlinx.coroutines.DispatchedTask.run() + 2780
        at 21  BaseBetaCommonApi                   0x0000000111d1b908 kfun:kotlinx.coroutines.EventLoopImplBase.processNextEvent()ValueType + 792
        at 22  BaseBetaCommonApi                   0x0000000111da26f1 kfun:kotlinx.coroutines.runEventLoop$kotlinx-coroutines-core(kotlinx.coroutines.EventLoop?;kotlin.Function0<kotlin.Boolean>) + 881
        at 23  BaseBetaCommonApi                   0x0000000111da9d12 kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.start$lambda-0#internal + 402
        at 24  BaseBetaCommonApi                   0x0000000111da9efb kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.$start$lambda-0$FUNCTION_REFERENCE$146.invoke#internal + 59
        at 25  BaseBetaCommonApi                   0x0000000111da9f5b kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.$start$lambda-0$FUNCTION_REFERENCE$146.$<bridge-UNN>invoke()#internal + 59
        at 26  BaseBetaCommonApi                   0x0000000111c4fd31 WorkerLaunchpad + 177
        at 27  BaseBetaCommonApi                   0x0000000112052d89 _ZN6Worker19processQueueElementEb + 2569
        at 28  BaseBetaCommonApi                   0x0000000112053336 _ZN12_GLOBAL__N_113workerRoutineEPv + 54
        at 29  libsystem_pthread.dylib             0x00007fff51bfe2eb _pthread_body + 126

To Reproduce
Copy and paste the code snippet from above into the common module of a multiplatform project. Run the function from ViewController#viewDidLoad in the iOS app.

Expected behavior
I expect the code to execute without throwing a ClassCastException.

bug

All 8 comments

kotlin.ClassCastException: kotlin.coroutines.native.internal.CompletedContinuation cannot be cast to kotlinx.coroutines.DispatchedContinuation
        at 0   BaseBetaCommonApi                   0x0000000111c21227 kfun:kotlin.Throwable.<init>(kotlin.String?)kotlin.Throwable + 87
        at 1   BaseBetaCommonApi                   0x0000000111c1a585 kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception + 85
        at 2   BaseBetaCommonApi                   0x0000000111c1a0c5 kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException + 85
        at 3   BaseBetaCommonApi                   0x0000000111c1ae65 kfun:kotlin.ClassCastException.<init>(kotlin.String?)kotlin.ClassCastException + 85
        at 4   BaseBetaCommonApi                   0x0000000111c6ba5b ThrowClassCastException + 939
        at 5   BaseBetaCommonApi                   0x0000000111d14ef8 kfun:kotlinx.coroutines.CoroutineDispatcher.releaseInterceptedContinuation(kotlin.coroutines.Continuation<#STAR>) + 216
        at 6   BaseBetaCommonApi                   0x0000000111c42400 kfun:kotlin.coroutines.native.internal.ContinuationImpl.releaseIntercepted() + 768
        at 7   BaseBetaCommonApi                   0x0000000111c42a62 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl.resumeWith(kotlin.Result<kotlin.Any?>) + 1234

@qwwdfsad any ideas?

Looks like a bug in releaseContinuation codegen in K/N backend (especially if it is not reproducible on JVM), meaning that Continuation was resumed twice.
Probably related to reusable continuations in coroutines as well

The stack trace contains AwaitContinuation that is used in *Deferred.await() (CompletableDeferred.await() or async {}.await()). In iOS we have only one place where we invoke such function: HttpClientEngine.executeWithinCallContext (it's async + await).

Also, the code is common that never crashed like this before.

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

Getting the same crash with ktor 1.4.1 and coroutines 1.3.9-native-mt-2. How can we fix it?

It seems there is an issue with Kotlin 1.4.10, according to this https://github.com/Kotlin/kotlinx.coroutines/issues/2295
I downgraded ktor to 1.3.2, Kotlin back to version 1.3.72 and coroutines to 1.3.7 and only then it worked, not sure if maybe I had something cached somewhere and it was still compiling with Kotlin 1.4

Should be fixed with ktor 1.5.0

Was this page helpful?
0 / 5 - 0 ratings