Why does this code block indefinitely?
fun main() = runBlocking {
val myFlow: Flow<String> = flow {
emit("abc")
}.flowOn(Dispatchers.Main)
myFlow.collect {
println(it)
}
}
Everything seems to work when choosing a different dispatcher for flowOn or when removing flowOn entirely.
Since only a single value is emitted into the Flow, I would expect this function to return quickly in any case, though.
Core issue is you're using runBlocking which causes a deadlock. When you start the flow flowOn tries to re-post the work to main thread but it's already suspended (and blocked, since it's within runBlocking) by collect.