Skip to content
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

Inconsistent documentation on whether failure in async{} cancels the parent #2566

Closed
jesseschalken opened this issue Mar 6, 2021 · 0 comments
Labels
docs KDoc and API reference

Comments

@jesseschalken
Copy link

jesseschalken commented Mar 6, 2021

The documentation for Job.children says:

Uncaught exception in a child, by default, cancels parent. In particular, this applies to children created with launch coroutine builder. Note that async and other future-like coroutine builders do not have uncaught exceptions by definition, since all their exceptions are caught and are encapsulated in their result.

However the documentation for async says:

The resulting coroutine has a key difference compared with similar primitives in other languages and frameworks: it cancels the parent job (or outer scope) on failure to enforce structured concurrency paradigm. To change that behaviour, supervising parent (SupervisorJob or supervisorScope) can be used.

So does an exception thrown within async{} cancel the parent or not?

A simple test shows that exceptions thrown in async{} do cancel the parent and are not "caught and encapsulated in their result". The documentation for Job.children is wrong.

import kotlinx.coroutines.*

fun main() {
    runBlocking { foo() }
}

suspend fun foo() {
    coroutineScope {
        val result = async {
            throw RuntimeException("oh no")
        }

        try {
            result.await()
        } catch (e: Throwable) {
            // ignore
        }
    }
}
Exception in thread "main" java.lang.RuntimeException: oh no
 at FileKt$foo$2$1.invokeSuspend (File.kt:10) 
 at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith (ContinuationImpl.kt:33) 
 at kotlinx.coroutines.DispatchedTask.run (DispatchedTask.kt:106) 

https://pl.kotl.in/XJMI9RswS

@qwwdfsad qwwdfsad added the docs KDoc and API reference label Mar 11, 2021
elizarov added a commit that referenced this issue Mar 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs KDoc and API reference
Projects
None yet
Development

No branches or pull requests

2 participants