Skip to content

Coroutine is unstable in notebook #490

@Cyberavater

Description

@Cyberavater

sample code

val scope = CoroutineScope(Dispatchers.Default)
val sharedFlow = flow {
    while (true) {
        emit(Unit)
        delay(1.seconds)
    }
}.onEach {
    println("Emitted")
}.shareIn(
    scope,
    started = SharingStarted.WhileSubscribed(),
)

val job = Job()
runBlocking {
    //5 listeners
    repeat(5) { listenerId ->
        launch(job) {
            sharedFlow.collect {
                println("Collected by $listenerId")
            }
        }
    }

    delay(10.seconds)
    job.complete()
    println("Done")
}

Output last part:

Collected by 0
Collected by 1
Collected by 2
Collected by 3
Collected by 4
Emitted
Emitted
Collected by kotlin.Unit
Collected by kotlin.Unit
Collected by kotlin.Unit
Collected by kotlin.Unit
Collected by kotlin.Unit
Collected by 0
Collected by 1
Collected by 2
Collected by 3
Collected by 4
Emitted
Collected by 0
Collected by 1
Collected by 2
Collected by 3
Collected by 4

see Collected by kotlin.Unit

This is problematic, you won't see this happen while running as regular jvm project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions