Skip to content

TimeoutCancellationException inconsistently bubbled up #3716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
thadcodes opened this issue Apr 14, 2023 · 1 comment
Open

TimeoutCancellationException inconsistently bubbled up #3716

thadcodes opened this issue Apr 14, 2023 · 1 comment
Labels

Comments

@thadcodes
Copy link

Describe the bug

When a TimeoutCancelationException occurs inside a flow it gets thrown and can be caught in the catch block. But if it is thrown in a flow that is attached with a flatMap the exception is ignored/swallowed.

Provide a Reproducer

The code at the bottom of this section running in https://play.kotlinlang.org/ using version kotlin 1.8.20 emits the following

This should never execute
Found error java.lang.RuntimeException
Found error kotlinx.coroutines.TimeoutCancellationException: Timed out immediately

but it should emit the following:

Found error kotlinx.coroutines.TimeoutCancellationException: Timed out immediately
Found error java.lang.RuntimeException
Found error kotlinx.coroutines.TimeoutCancellationException: Timed out immediately
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.*

@kotlinx.coroutines.ExperimentalCoroutinesApi
fun main() = runBlocking {
    runTest {
        withTimeout(0L) {
                // Do nothing this will not execute
            }
    }
    
    runTest {
        throw RuntimeException()
    }
    flow<Boolean> {
        emit(true)
        //throw RuntimeException()
        withTimeout(0L) {
            // Do nothing this will not execute
 		}
    }

    .onCompletion {
        if(it == null) println("This should never execute")
    }
    .catch { println("Found error $it") }
    .collect()
}


suspend fun runTest(block: suspend FlowCollector<Boolean>.() -> Unit) = flow {
        emit(true)
    }.flatMapLatest {
        flow<Boolean> {
            block()                      
        }
    }.onCompletion {
        if(it == null) println("This should never execute")
    }
    .catch { println("Found error $it") }
    .collect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants