You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The logic behind parent-child relations between coroutines shall be changed so that parent coroutine automatically waits for its children to complete.
This is needed for several use-cases:
Predictable exception propagation in parent-child relations. The use-case for child coroutine is that the child carries out a subtask on parent's behalf and if something goes wrong (child crashes), then the parent should predictably crash, too. Right now, a child that was fired with launch cancels its parent on crash, but if parent had already completed, then there is nothing else a child can do. So, it is somewhat random behavior now -- if a child crashes before parent's completion, then exception gets propagated, otherwise it does not.
We'd like for Rx-inspired merge operation to have simple implementation. The current implementation given in the reactive guide is not correct under existing parent-child semantics. It stops publishing as soon as the source channel is over, without waiting to merge all the received channels. With the change to the parent-child relation (parent waits) it starts to work as expected automagically:
fun <T> Publisher<Publisher<T>>.merge(context: CoroutineContext) = publish<T>(context) {
consumeEach { pub -> // for each publisher received on the source channel
launch(coroutineContext) { // launch a child coroutine
pub.consumeEach { send(it) } // resend all element from this publisher
}
}
}
Reduce surprising behavior. The fact that children are cancelled when parent completes normally is surprising. It is hard to trouble-shoot and to figure out what is going on.
The text was updated successfully, but these errors were encountered:
elizarov
changed the title
Parent coroutine shall wait for its children to complete automtically
Parent coroutine shall wait for its children to complete
Sep 15, 2017
The logic behind parent-child relations between coroutines shall be changed so that parent coroutine automatically waits for its children to complete.
This is needed for several use-cases:
Predictable exception propagation in parent-child relations. The use-case for child coroutine is that the child carries out a subtask on parent's behalf and if something goes wrong (child crashes), then the parent should predictably crash, too. Right now, a child that was fired with
launch
cancels its parent on crash, but if parent had already completed, then there is nothing else a child can do. So, it is somewhat random behavior now -- if a child crashes before parent's completion, then exception gets propagated, otherwise it does not.We'd like for Rx-inspired
merge
operation to have simple implementation. The current implementation given in the reactive guide is not correct under existing parent-child semantics. It stops publishing as soon as the source channel is over, without waiting to merge all the received channels. With the change to the parent-child relation (parent waits) it starts to work as expected automagically:The text was updated successfully, but these errors were encountered: