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

inherit FiberRef's when a failed fiber joins #8731

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core-tests/shared/src/test/scala/zio/ZIOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2883,6 +2883,19 @@ object ZIOSpec extends ZIOBaseSpec {
r <- f.join
} yield assert(r)(equalTo(42))
},
test("propagates FiberRef value even if the effect failed") {
for {
ref <- FiberRef.make[Set[String]](Set("parent"))
workflow = ref.update(_ + "child") *> ZIO.sleep(2.seconds) *> ZIO.fail(new RuntimeException("Boom!"))
f <- workflow.fork
_ <- TestClock.adjust(2.seconds)
res <- f.join.flip
set <- ref.get
} yield {
assert(res)(isSubtype[RuntimeException](anything)) &&
assert(set)(hasSameElements(Set("parent", "child")))
}
},
test("deep fork/join identity") {
val n = 20
assertZIO(concurrentFib(n))(equalTo(fib(n)))
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/Fiber.scala
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ abstract class Fiber[+E, +A] { self =>
* `IO[E, A]`
*/
final def join(implicit trace: Trace): IO[E, A] =
await.unexit <* inheritAll
(await <* inheritAll).unexit

/**
* Maps over the value the Fiber computes.
Expand Down
11 changes: 6 additions & 5 deletions core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1272,9 +1272,10 @@ sealed trait ZIO[-R, +E, +A]

/**
* Returns an effect that races this effect with the specified effect,
* returning the first successful `A` from the faster side. If one effect
* succeeds, the other will be interrupted. If neither succeeds, then the
* effect will fail with some error.
* returning the first successful `A` from the faster side and propagates its
* all [[FiberRef]] values. If one effect succeeds, the other will be
* interrupted. If neither succeeds, then the effect will fail with some
* error, and no [[FiberRef]] values will be inherited.
*
* WARNING: The raced effect will safely interrupt the "loser", but will not
* resume until the loser has been cleanly terminated. If early return is
Expand All @@ -1287,12 +1288,12 @@ sealed trait ZIO[-R, +E, +A]
(self.raceWith(that))(
(exit, right) =>
exit.foldExitZIO[Any, E1, A1](
cause => right.join.mapErrorCause(cause && _),
cause => right.await.unexit.mapErrorCause(cause && _) <* right.inheritAll,
a => right.interruptAs(parentFiberId).as(a)
),
(exit, left) =>
exit.foldExitZIO[Any, E1, A1](
cause => left.join.mapErrorCause(_ && cause),
cause => left.await.unexit.mapErrorCause(_ && cause) <* left.inheritAll,
a => left.interruptAs(parentFiberId).as(a)
)
)
Expand Down
Loading