Shubo
02/23/2025, 3:58 AMApplication
as the scope, the flow always encounters a context preservation error. The error disappears if I turn off development mode.
Simple reproducible example:
fun Application.module() {
configureRouting()
launch {
flow {
emit(1)
emit(2)
emit(3)
}.collect {
println("Collected: $it")
}
}
}
with application.yaml
ktor:
application:
modules:
- com.example.ApplicationKt.module
deployment:
port: 8080
development: true
The error I receive is
Exception in thread "main" java.lang.IllegalStateException: Flow invariant is violated:
Flow was collected in [StandaloneCoroutine{Active}@64c2b546, io.ktor.server.application.ClassLoaderAwareContinuationInterceptor@7e094740],
but emission happened in [StandaloneCoroutine{Active}@64c2b546, io.ktor.server.application.ClassLoaderAwareContinuationInterceptor@7e094740].
Please refer to 'flow' documentation or use 'flowOn' instead
at kotlinx.coroutines.flow.internal.SafeCollector_commonKt.checkContext(SafeCollector.common.kt:84)
at kotlinx.coroutines.flow.internal.SafeCollector.checkContext(SafeCollector.kt:132)
at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:109)
at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:82)
at com.example.ApplicationKt$module$1$1.invokeSuspend(Application.kt:19)
at com.example.ApplicationKt$module$1$1.invoke(Application.kt)
Aleksei Tirman [JB]
02/24/2025, 9:09 AM