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

Parent coroutine shall wait for its children to complete #125

Closed
elizarov opened this issue Sep 15, 2017 · 3 comments
Closed

Parent coroutine shall wait for its children to complete #125

elizarov opened this issue Sep 15, 2017 · 3 comments

Comments

@elizarov
Copy link
Contributor

elizarov commented 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:

  1. 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.

  2. 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
      }
  }
}
  1. 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.
@elizarov 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
@elizarov
Copy link
Contributor Author

This is now fixed in develop branch.

@elizarov
Copy link
Contributor Author

Released in version 0.19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants