Skip to content

Callbackflow close exception misbehavior with timeout #4071

Closed
@dddj698

Description

@dddj698

Describe the bug

When callbackflow is chained with timeout directly, and closed with an exception, the exception won't be sent to downstream.

Coroutines version 1.8.0. Kotlin version 1.9.21.

Provide a Reproducer

val flow = callbackFlow<Int> {
    close(IllegalStateException("Test Exception"))
}.timeout(5.seconds)
try {
    flow.collect()
} catch (e: Exception) {
    println("exception got: $e")
}

I expect this to print:
exception got: java.lang.IllegalStateException: Test Exception

But no exception is caught.

However, if I add any operator between callbackflow and timeout, it works as expected,

val flow = callbackFlow<Int> {
    close(IllegalStateException("Test Exception"))
}.catch{throw it}.timeout(5.seconds)
try {
    flow.collect()
} catch (e: Exception) {
    println("exception got: $e")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions