https://kotlinlang.org logo
#coroutines
Title
# coroutines
w

wakingrufus

05/14/2021, 7:12 PM
what is a good way to approach debugging
Copy code
ClassCastException: kotlin.coroutines.intrinsics.CoroutineSingletons cannot be cast to ...
I am using
suspend fun
as well as Flows, but I am not sure where to start with this.
stacktrace here in case that helps:
Copy code
Exception in thread "main" java.lang.ClassCastException: kotlin.coroutines.intrinsics.CoroutineSingletons cannot be cast to io.ktor.client.statement.HttpResponse
        at com.github.wakingrufus.kalibrate.example.MainKt$main$2$4$1$1$1.invoke(Main.kt:68)
        at com.github.wakingrufus.kalibrate.example.MainKt$main$2$4$1$1$1.invoke(Main.kt)
        at com.github.wakingrufus.kalibrate.scenario.Step.invoke(Step.kt:15)
        at com.github.wakingrufus.kalibrate.scenario.StepContainer$invoke$1.invokeSuspend(Simulation.kt:93)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:277)
        at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:86)
        at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:61)
        at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
        at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt)
        at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
        at com.github.wakingrufus.kalibrate.scenario.StepContainer.invoke(Simulation.kt:91)
        at com.github.wakingrufus.kalibrate.scenario.Simulation.runSetup(Simulation.kt:75)
        at com.github.wakingrufus.kalibrate.scenario.Simulation$singletonWorkPattern$1$1$1.invokeSuspend(Simulation.kt:25)
        at com.github.wakingrufus.kalibrate.scenario.Simulation$singletonWorkPattern$1$1$1.invoke(Simulation.kt)
        at kotlinx.coroutines.flow.SafeFlow.collectSafely(Builders.kt:61)
        at kotlinx.coroutines.flow.AbstractFlow.collect(Flow.kt:212)
        at kotlinx.coroutines.flow.FlowKt__MergeKt$flattenConcat$$inlined$unsafeFlow$1.collect(SafeCollector.common.kt:114)
        at kotlinx.coroutines.flow.internal.ChannelFlowMerge$collectTo$$inlined$collect$1$lambda$1.invokeSuspend(Merge.kt:69)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:277)
        at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:86)
        at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:61)
        at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
        at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt)
        at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
        at com.github.wakingrufus.kalibrate.KalibrateDslBuilder.invoke(KalibrateDsl.kt:92)
        at com.github.wakingrufus.kalibrate.KalibrateDslKt$kalibrate$1.invoke(KalibrateDsl.kt:125)
        at com.github.wakingrufus.kalibrate.KalibrateDslKt$kalibrate$1.invoke(KalibrateDsl.kt)
        at com.xenomachina.argparser.SystemExitExceptionKt.mainBody(SystemExitException.kt:74)
        at com.xenomachina.argparser.SystemExitExceptionKt.mainBody$default(SystemExitException.kt:72)
        at com.github.wakingrufus.kalibrate.KalibrateDslKt.kalibrate(KalibrateDsl.kt:124)
        at com.github.wakingrufus.kalibrate.example.MainKt.main(Main.kt:16)
e

elizarov

05/14/2021, 8:31 PM
#ktor
w

wakingrufus

05/14/2021, 9:13 PM
ah ok, i can ask there. I was thinking that I am probably doing somethign generally wrong with structured concurrency
thanks
e

elizarov

05/15/2021, 10:09 AM
It looks like ktor-specific problem
b

bartvh

05/16/2021, 7:17 PM
Did you find a solution? I'm experiencing the same issue, and Google only turns up discussions of old bugs.
w

wakingrufus

05/16/2021, 8:08 PM
Not yet. Going to pick it back up on monday
i took the issue to #ktor and then opened a youtrack bug
i got around this by getting rid of my suspend methods, and just using flows, and then surrounding just the ktor client calls with
runBlocking{}
b

bartvh

05/18/2021, 11:42 AM
As it happens I also got around it... accidentally, while refactoring my code in preparation for using Apache HTTP client instead of Ktor 😅 It probably disappeared because I made some functions non-inline (letting them return
String
instead of
receive<reified T>
), but can't say for sure which change exactly did it. Glad it did though, since I didn't have to replace the Ktor client entirely in the end.
w

wakingrufus

05/18/2021, 2:10 PM
interesting. i feel like wrapping all my ktor calls with
runBlocking
seems against the spirit. I am trying to maximize the throughput in terms of req/sec and so I feel like there must be a better way....
i have an inline function involved as well
but i dont think i can get rid of it
since the ktor client need the reified generic
4 Views