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

Handle Differences In Ancestor Values When Joining Fiber Refs #8334

Merged
merged 4 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 33 additions & 0 deletions core-tests/shared/src/test/scala/zio/FiberRefSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,39 @@ object FiberRefSpec extends ZIOBaseSpec {
}
value <- promise.await
} yield assertTrue(value)
},
adamgfraser marked this conversation as resolved.
Show resolved Hide resolved
test("child fiber inherits changes made by parent after fork when joining parent") {
def parent(
fiberRef: FiberRef[Set[String]],
promise: Promise[Nothing, Fiber.Runtime[Any, Any]]
): ZIO[Any, Nothing, Set[String]] =
for {
parent <- promise.await
promise <- Promise.make[Nothing, Fiber.Runtime[Any, Any]]
_ <- fiberRef.update(_ + "parent before")
child <- child(fiberRef, promise).fork
_ <- fiberRef.update(_ + "parent after")
_ <- promise.succeed(parent)
value <- child.join
} yield value

def child(
fiberRef: FiberRef[Set[String]],
promise: Promise[Nothing, Fiber.Runtime[Any, Any]]
): ZIO[Any, Nothing, Set[String]] =
for {
parent <- promise.await
_ <- parent.inheritAll
value <- fiberRef.get
} yield value

for {
fiberRef <- FiberRef.makeSet(Set.empty[String])
promise <- Promise.make[Nothing, Fiber.Runtime[Any, Any]]
parent <- parent(fiberRef, promise).fork
_ <- promise.succeed(parent)
value <- parent.join
} yield assertTrue(value == Set("parent before", "parent after"))
}
) @@ TestAspect.fromLayer(Runtime.enableCurrentFiber) @@ TestAspect.sequential
}
Expand Down
39 changes: 20 additions & 19 deletions core/shared/src/main/scala/zio/FiberRefs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,39 +102,40 @@ final class FiberRefs private (
childStack: List[(FiberId.Runtime, Any)],
childDepth: Int,
childModified: Boolean = false
): (Any, Boolean) =
): (Any, Any, Boolean) =
(parentStack, childStack) match {
case ((parentFiberId, _) :: parentAncestors, (childFiberId, childValue) :: childAncestors) =>
case (
(parentFiberId, parentValue) :: parentAncestors,
(childFiberId, childValue) :: childAncestors
) =>
if (parentFiberId == childFiberId)
(childValue, childModified)
(parentValue, childValue, childModified)
else if (childDepth > parentDepth)
findAncestor(parentStack, parentDepth, childAncestors, childDepth - 1, true)
else if (childDepth < parentDepth)
findAncestor(parentAncestors, parentDepth - 1, childStack, childDepth, childModified)
else
findAncestor(parentAncestors, parentDepth - 1, childAncestors, childDepth - 1, true)
case _ =>
(ref.initial, true)
(ref.initial, ref.initial, true)
}

val (ancestor, wasModified) = findAncestor(parentStack, parentDepth, childStack, childDepth)
val (parentAncestor, childAncestor, wasModified) =
findAncestor(parentStack, parentDepth, childStack, childDepth)

if (!wasModified) parentFiberRefs
val patch = if (wasModified) ref.diff(childAncestor, childValue) else ref.diff(parentAncestor, childValue)
adamgfraser marked this conversation as resolved.
Show resolved Hide resolved

val oldValue = parentStack.head._2
val newValue = ref.join(oldValue, ref.patch(patch)(oldValue))

if (oldValue == newValue) parentFiberRefs
else {
val patch = ref.diff(ancestor, childValue)

val oldValue = parentStack.head._2
val newValue = ref.join(oldValue, ref.patch(patch)(oldValue))

if (oldValue == newValue) parentFiberRefs
else {
val (newStack, newDepth) = parentStack match {
case (parentFiberId, _) :: tail =>
if (parentFiberId == fiberId) (::((parentFiberId, newValue), tail), parentDepth)
else (::((fiberId, newValue), parentStack), parentDepth + 1)
}
parentFiberRefs.updated(ref, (newStack, newDepth))
val (newStack, newDepth) = parentStack match {
case (parentFiberId, _) :: tail =>
if (parentFiberId == fiberId) (::((parentFiberId, newValue), tail), parentDepth)
else (::((fiberId, newValue), parentStack), parentDepth + 1)
}
parentFiberRefs.updated(ref, (newStack, newDepth))
}
}
}
Expand Down
Loading