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

Skip to content

Conversation

@mlangc
Copy link
Contributor

@mlangc mlangc commented Sep 6, 2019

Fixes #1515


inheritFiberRefs = inherit.get.flatMap {
case None => ZIO.unit
case Some(true) => left2.inheritFiberRefs
Copy link
Member

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.

Copy link
Contributor Author

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]],
Copy link
Member

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.

Copy link
Contributor Author

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],
Copy link
Member

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?

Copy link
Contributor Author

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this?

Copy link
Contributor Author

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

@adamgfraser
Copy link
Contributor

With these changes if we race two effects with raceWith and then join the loser as in zipPar, would the loser FiberRef be inherited?

@mlangc mlangc force-pushed the 1515-fiber-ref-not-being-inherited branch from 5fdc5db to 7a45ecb Compare September 11, 2019 18:27
@mlangc mlangc force-pushed the 1515-fiber-ref-not-being-inherited branch from 7a45ecb to d3d1171 Compare September 11, 2019 18:32
@mlangc
Copy link
Contributor Author

mlangc commented Sep 11, 2019

With these changes if we race two effects with raceWith and then join the loser as in zipPar, would the loser FiberRef be inherited?

@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

@adamgfraser
Copy link
Contributor

@mlangc Thanks! It looks like right now we are testing that one or the other FiberRef gets inherited. Could we add a test where we ZipPar with two successful fibers, one of which is slower, and show that it is the loser FiberRef that gets inherited?

@mlangc
Copy link
Contributor Author

mlangc commented Sep 11, 2019

@adamgfraser thanks a lot for bringing this up! After adding tests for zipPar (see https://github.com/zio/zio/pull/1594/files#diff-b3f6f229206b73e705ea7e4c6e03784bR25) I found out that the implementation needed another tweak.

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.
@mlangc mlangc force-pushed the 1515-fiber-ref-not-being-inherited branch from 1da4897 to 8165398 Compare September 11, 2019 19:58
Copy link
Member

@jdegoes jdegoes left a 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!

@jdegoes jdegoes merged commit f15e9cf into zio:master Sep 12, 2019
@adamgfraser adamgfraser mentioned this pull request Sep 19, 2019
fsvehla pushed a commit to fsvehla/zio that referenced this pull request Sep 21, 2019
* 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.
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.

FiberRef Not Being Inherited by Parent After Race

3 participants