question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scope for "one-for-one" supervision (children don't crash parent and siblings)

See original GitHub issue

The default behavior of children coroutines in Kotlin loosely corresponds to “one-for-all” supervision behavior – when one crashed child kills all siblings (and parent, too). There is no automatic restart in Kotlin, but that is completely different topic. The default in Kotlin is chosen this way, because this is a good default for a use-case of parallel decomposition when one large job is decomposed it smaller jobs that work in parallel. It also makes coroutines ephemeral – a coroutine can always delegate a piece of its work to a child without an outside world noticing it.

However, sometimes, children coroutines are independend, but are still children in the sense that parent cancellation has to cancel them, too, however, their crash should not kill parent and all sibling coroutines, but shall be handled independently. Currently, it requires a bit of boilerplate to write:

launch { // child
    try {
        doSomething() // child's job
    } catch(e: Throwable) {
        // handle failure
    }
}

In a particular project, the above boilerplate can be readily incapsulated into a function together with project-specific error-handling code:

fun CoroutineScope.launchAndHandle(suspend block: CoroutineScope.() -> Unit) {
    launch { // child
        try {
            block()
        } catch(e: Throwable) {
            // handle failure
        }
    }
}

However, these kinds of functions cannot be readily provided by kotlinx.coroutines, because they are not composable – one needs to define such function for each type of coroutine builder.

Instead, the proposal is to add a new CoroutineScope builder function, so that one can write:

__independentCoroutineScope {
    launch { // child
        doSomething() // child's job
    }
}

The __independentCoroutineScope function (the good actual name is to be found) creates a CoroutineScope of a special kind that makes all the coroutines launched directly from inside of it to have this special behavior – they are cancelled when the parent is cancelled, but their exceptions are handled independently by an installed CoroutineExceptionException which can be inherited from a parent or can be specified in this builder explicitly:

__independentCoroutineScope(CoroutineExceptionHandler { .... }) {
    launch { // child
        doSomething() // child's job
    }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
elizarovcommented, Sep 21, 2018

Tentative names are supervisorScope { ... } for a scope that would handle exceptions of its children independently and SupervisorJob() for an explicit job object constructor with the same properties.

2reactions
elizarovcommented, Sep 18, 2018

@jcornaz We’ll use a special implementation of Job for that scope (one that does not cancel all children on a crash of one of them), so another way to expose this feature is to provide a separate __IsolatedJob() constructor, in your example:

override val coroutineContext = __IsolatedJob() + Dispatchers.UI
Read more comments on GitHub >

github_iconTop Results From Across the Web

Parent Supervision to Prevent Injuries to Young Children
Prevention of injuries in young children, three dimensions: attention, proximity, and extent of continuity in attending and proximity.
Read more >
Supervising Children - Division of Early Childhood
Perhaps the single most important regulation is the one that addresses child supervision. A clear understanding of supervision will make it easier for...
Read more >
Maternal Supervision of Children During Their First 3 Years of ...
Objective The present study examined the effect of child gender and maternal depressive symptoms on routine supervisory practices of mothers longitudinally.
Read more >
Child Neglect: A Guide for Prevention, Assessment and ...
Since the late 1970s, the Child Abuse and Neglect. User Manual Series has provided guidance on child protection to hundreds of thousands of....
Read more >
If lifecycleScope is supervisor, why its child coroutine's failure ...
The output is unexpected, because Coroutine #2 doesn't finish its work due to the app crash. testSupervisorScope: Coroutine #1 start ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found