Skip to content

withTimeout never thrown TimeoutCancellationException #2394

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

Closed
francismariano opened this issue Nov 16, 2020 · 3 comments
Closed

withTimeout never thrown TimeoutCancellationException #2394

francismariano opened this issue Nov 16, 2020 · 3 comments
Labels

Comments

@francismariano
Copy link

Hello guys,

Here is a basic test with timeout in an Android app. In this case, println never is called because TimeoutCancellationException is not thrown, in normal behavior the app should to crash, but it is not.

fun testeTimeout() {
    mainScope.launch {
        val ret = getTimeoutInt()
        println(ret)
    }
}

suspend fun getTimeoutInt() : Int {
    return withTimeout(1000) {
        delay(5000)
        10
    }
}

However, if the getTimeoutInt is enclosed with try/catch the exception is thrown normally.

fun testeTimeout() {
    mainScope.launch {
        try {
            val ret = getTimeoutInt()
            println(ret)
        } catch (e: Exception) {
            println("ret = $e)
        }
    }
}

ret = kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 1000 ms

Is it a bug that?
Kotlin : 1.4.20-RC
Coroutine: kotlinx-coroutines-android:1.4.1

Thank you

@qwwdfsad
Copy link
Collaborator

TimeoutCancellationException is successfully thrown from delay method (you can see it by wrapping it into try-finally or try-catch) and coroutine from testeTimeout is terminated. It's just not printed to the console

@francismariano
Copy link
Author

I see the exception when I wrapping it with try-catch.

But I think that the application should be crashed if the TimeoutCancellationException is successfully thrown and it is not catch with try-catch.

@qwwdfsad
Copy link
Collaborator

I see, this is because TimeoutCancellationException is CancellationException and CancellationException can neither crash an application nor get reported.
Let me close it as a duplicate of #1374

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