Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does launch swallow exceptions in kotlin coroutines?

The following test succeeds with Process finished with exit code 0. Note, this test does print the exception to the logs, but does not fail the test (which is the behavior I want).

@Test
fun why_does_this_test_pass() {
    val job = launch(Unconfined) {
        throw IllegalStateException("why does this exception not fail the test?")
    }

    // because of `Unconfined` dispatcher, exception is thrown before test function completes
}

As expected, this test fails with Process finished with exit code 255

@Test
fun as_expected_this_test_fails() {
    throw IllegalStateException("this exception fails the test")
}

Why do these tests not behave the same way?

like image 355
ZakTaccardi Avatar asked Jun 05 '18 19:06

ZakTaccardi


2 Answers

Compare your test with the following one that does not use any coroutines, but starts a new thread instead:

@Test
fun why_does_this_test_pass() {
    val job = thread { // <-- NOTE: Changed here
        throw IllegalStateException("why does this exception not fail the test?")
    }
    // NOTE: No need for runBlocking any more
    job.join() // ensures exception is thrown before test function completes
}

What happens here? Just like the test with launch, this test passes if you run it, but the exception gets printed on the console.

So, using launch to start a new coroutine is very much like using thread to start a new thread. If it fails, the error gets handled by uncaught exception handler in thread and by CoroutineExceptionHandler (see it in the docs) by launch. Exceptions in launch are not swallowed, but are handled by the coroutine exception handler.

If you want exception to propagate to the test, you shall replace launch with async and replace join with await in your code. See also this question: What is the difference between launch/join and async/await in Kotlin coroutines

UPDATE: Kotlin coroutines had recently introduced the concept of "Structured Concurrency" to avoid this kind of exception loss. The code in this question does not compile anymore. To compile it, you'd have to either explicitly say GlobalScope.launch (as in "I confirm that it Ok to loose my exceptions, here is my signature") or wrap the test into runBlocking { ... }, in which case exception is not lost.

like image 116
Roman Elizarov Avatar answered Nov 11 '22 01:11

Roman Elizarov


I was able to create an exception throwing CoroutineContext for tests.

val coroutineContext = Unconfined + CoroutineExceptionHandler { _, throwable ->
        throw throwable
    }

Though this would probably not be suitable for production. Maybe need to catch cancellation exceptions or something, I'm not sure

like image 32
ZakTaccardi Avatar answered Nov 11 '22 01:11

ZakTaccardi