Attempt to report uncaught exceptions in the test module (#3449)

When when a `Job` doesn't have a parent, failures in it can not
be reported via structured concurrency. Instead, the mechanism of
unhandled exceptions is used. If there is a
`CoroutineExceptionHandler` in the coroutine context or registered
as a `ServiceLoader` service, this gets notified, and otherwise,
something platform-specific happens: on Native, the program
crashes, and on the JVM, by default, the exception is just logged,
though this is configurable via
`Thread.setUncaughtExceptionHandler`.

With tests on the JVM, this is an issue: we want exceptions not
simply *logged*, we want them to fail the test, and this extends
beyond just coroutines. However, JUnit does not override the
uncaught exception handler, and uncaught exceptions do just get
logged:
<https://stackoverflow.com/questions/36648317/how-to-capture-all-uncaucht-exceptions-on-junit-tests>

This is a problem with a widely-used `viewModelScope` on Android.
This is a scope without a parent, and so the exceptions in it are
uncaught. On Android, such uncaught exceptions crash the app by
default, but in tests, they just get logged. Clearly, without
overriding the behavior of uncaught exceptions, the tests are
lacking. This can be solved on the test writers' side via
`setUncaughtExceptionHandler`, but one has to remember to do that.

In this commit, we attempt to solve this issue for the overwhelming
majority of users. To that end, when the test framework is used,
we collect the uncaught exceptions and report them at the end of a
test. This approach is marginally less robust than
`setUncaughtExceptionHandler`: if an exception happened after the
last test using `kotlinx-coroutines-test`, it won't get reported,
for example.

`CoroutineExceptionHandler` is designed in such a way that, when it
is used in a coroutine context, its presence means that the
exceptions are safe in its care and will not be propagated further,
but when used as a service, it has no such property. We, however,
know that our `CoroutineExceptionHandler` reports the exceptions
properly and they don't need to be further logged, and so we had to
extend the behavior of mechanism for uncaught exception handling so
that the handler throws a new kind of exception if the
exception was processed successfully.

Also, because there's no `ServiceLoader` mechanism on JS or Native,
we had to refactor the whole uncaught exception handling mechanism
a bit in the same vein as we had to adapt the `Main` dispatcher
to `Dispatchers.setMain`: by introducing internal setter APIs that
services have to call manually to register in.

Fixes #1205
as thoroughly as we can, given the circumstances.
19 files changed
tree: 2ce59da800296b92a93dc6daa82c52668c8a9b32
  1. .github/
  2. .idea/
  3. benchmarks/
  4. buildSrc/
  5. docs/
  6. dokka-templates/
  7. gradle/
  8. integration/
  9. integration-testing/
  10. js/
  11. kotlinx-coroutines-bom/
  12. kotlinx-coroutines-core/
  13. kotlinx-coroutines-debug/
  14. kotlinx-coroutines-test/
  15. license/
  16. reactive/
  17. site/
  18. ui/
  19. .gitignore
  20. build.gradle
  21. bump-version.sh
  22. CHANGES.md
  23. CODE_OF_CONDUCT.md
  24. CONTRIBUTING.md
  25. coroutines-guide.md
  26. gradle.properties
  27. gradlew
  28. gradlew.bat
  29. knit.properties
  30. LICENSE.txt
  31. README.md
  32. RELEASE.md
  33. settings.gradle
README.md

kotlinx.coroutines

Kotlin Stable JetBrains official project GitHub license Download Kotlin Slack channel

Library support for Kotlin coroutines with multiplatform support. This is a companion version for the Kotlin 1.8.10 release.

suspend fun main() = coroutineScope {
    launch { 
       delay(1000)
       println("Kotlin Coroutines World!") 
    }
    println("Hello")
}

Play with coroutines online here

Modules

Documentation

Using in your projects

Maven

Add dependencies (you can also add other modules that you need):

<dependency>
    <groupId>org.jetbrains.kotlinx</groupId>
    <artifactId>kotlinx-coroutines-core</artifactId>
    <version>1.6.4</version>
</dependency>

And make sure that you use the latest Kotlin version:

<properties>
    <kotlin.version>1.8.10</kotlin.version>
</properties>

Gradle

Add dependencies (you can also add other modules that you need):

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
}

And make sure that you use the latest Kotlin version:

plugins {
    // For build.gradle.kts (Kotlin DSL)
    kotlin("jvm") version "1.8.10"
    
    // For build.gradle (Groovy DSL)
    id "org.jetbrains.kotlin.jvm" version "1.8.10"
}

Make sure that you have mavenCentral() in the list of repositories:

repositories {
    mavenCentral()
}

Android

Add kotlinx-coroutines-android module as a dependency when using kotlinx.coroutines on Android:

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")

This gives you access to the Android Dispatchers.Main coroutine dispatcher and also makes sure that in case of a crashed coroutine with an unhandled exception that this exception is logged before crashing the Android application, similarly to the way uncaught exceptions in threads are handled by the Android runtime.

R8 and ProGuard

R8 and ProGuard rules are bundled into the kotlinx-coroutines-android module. For more details see “Optimization” section for Android.

Avoiding including the debug infrastructure in the resulting APK

The kotlinx-coroutines-core artifact contains a resource file that is not required for the coroutines to operate normally and is only used by the debugger. To exclude it at no loss of functionality, add the following snippet to the android block in your Gradle file for the application subproject:

packagingOptions {
    resources.excludes += "DebugProbesKt.bin"
}

Multiplatform

Core modules of kotlinx.coroutines are also available for Kotlin/JS and Kotlin/Native.

In common code that should get compiled for different platforms, you can add a dependency to kotlinx-coroutines-core right to the commonMain source set:

commonMain {
    dependencies {
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
    }
}

No more additional dependencies are needed, platform-specific artifacts will be resolved automatically via Gradle metadata available since Gradle 5.3.

Platform-specific dependencies are recommended to be used only for non-multiplatform projects that are compiled only for target platform.

JS

Kotlin/JS version of kotlinx.coroutines is published as kotlinx-coroutines-core-js (follow the link to get the dependency declaration snippet) and as kotlinx-coroutines-core NPM package.

Native

Kotlin/Native version of kotlinx.coroutines is published as kotlinx-coroutines-core-$platform where $platform is the target Kotlin/Native platform. List of currently supported targets.

Building and Contributing

See Contributing Guidelines.