-
Couldn't load subscription status.
- Fork 1.4k
Correctly inherit fiber refs in ZIO.raceWith
#1594
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
Correctly inherit fiber refs in ZIO.raceWith
#1594
Conversation
|
|
||
| inheritFiberRefs = inherit.get.flatMap { | ||
| case None => ZIO.unit | ||
| case Some(true) => left2.inheritFiberRefs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a more direct way to encode this: use Ref[Task[Unit]]. Then you can start that Ref off with ZIO.unit, and then you can either stick left2.inheritFiberRefs into that, or right2.inheritFiberRefs into it, depending on the race outcome.
This will simplify the code a lot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, you can't access the currently executing fiber, can you? Because to do the refactoring you propose, I would need to access either left2 or right2 from within arbiter, which is not possible otherwise. Also note that simply inheriting from left or right is not correct, because if the winning fiber failed, the caller might have joined the loosing fiber (which is exactly what raceEither does).
Or maybe I didn't understand you correctly?
| race: Ref[Int], | ||
| waitForLeft: Boolean, | ||
| race: Ref[Boolean], | ||
| inherit: Ref[Option[Boolean]], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would change to Ref[Task[Unit]], see below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above
| loser: Fiber[E1, B], | ||
| race: Ref[Int], | ||
| waitForLeft: Boolean, | ||
| race: Ref[Boolean], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rename this to raceDone?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done ;-)
| winner: Fiber[E0, A], | ||
| loser: Fiber[E1, B], | ||
| race: Ref[Int], | ||
| waitForLeft: Boolean, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without the refactoring proposed above - yes, we need this to have the necessary context information in arbiter if I'm not mistaken. I've renamed it to leftWins though...
|
With these changes if we race two effects with |
5fdc5db to
7a45ecb
Compare
7a45ecb to
d3d1171
Compare
@adamgfraser yes, if the looser wasn't a bad looser that is ;-) - I mean if the looser did not fail. There is a test that checks for this: https://github.com/zio/zio/pull/1594/files#diff-b3f6f229206b73e705ea7e4c6e03784bR23 |
|
@mlangc Thanks! It looks like right now we are testing that one or the other |
|
@adamgfraser thanks a lot for bringing this up! After adding tests for |
We have to call `inheritFiberRefs` on the winner *before* calling the user supplied function `f`, because we want `FiberRef` related updates from `f` to win over updates from the winning fiber.
1da4897 to
8165398
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me! @adamgfraser thanks for pointing out the need for new tests!
* Correctly inherit fiber refs in `ZIO.raceWith` Fixes zio#1515 * Cosmetics * Don't overwrite already inherited fiber refs in `raceWith` We have to call `inheritFiberRefs` on the winner *before* calling the user supplied function `f`, because we want `FiberRef` related updates from `f` to win over updates from the winning fiber.
Fixes #1515