Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@adamgfraser
Copy link
Contributor

Currently code like this will not work:

for {
  ref    <- Ref.make(0)
  fibers <- ZIO.foreachPar(1 to 100)(_ => ref.update(_ + 1).fork)
  _      <- ZIO.foreach(fibers)(_.await)
  value  <- ref.get
} yield assert(value)(equalTo(100))

The issue is that foreachPar forks a fiber to perform each of the effects and under the supervision model when that fiber terminates it terminates the fibers in its scope, including the fiber that is updating the Ref in this case.

The fix is to use the transplant operator similar to how we did with zipWithPar to make sure that when fibers are forked within one of the effects in foreachPar they are forked on the original scope and not the scope of the fiber internally created by foreachPar. Conceptually this is the same issue as with zipWithPar, it looks like we just didn't push the change through to foreachPar.

@adamgfraser adamgfraser requested a review from jdegoes November 14, 2020 15:39
@adamgfraser adamgfraser requested a review from mijicd November 24, 2020 20:19
@jdegoes
Copy link
Member

jdegoes commented Nov 26, 2020

Excellent catch! 🎉 💪

@jdegoes jdegoes merged commit 0677a51 into zio:master Nov 26, 2020
@adamgfraser adamgfraser deleted the transplant branch November 26, 2020 07:01
atooni pushed a commit to blended-zio/zio that referenced this pull request Dec 1, 2020
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

Successfully merging this pull request may close these issues.

3 participants