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

Fix propagation of interruption from CIO to ZIO #696

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import zio.interop.catz.*
import zio.test.*
import zio.*

import java.util.concurrent.CompletableFuture
import java.util.concurrent.atomic.AtomicBoolean

object CatsInteropSpec extends CatsRunnableSpec {
def spec: Spec[Any, Throwable] = suite("Cats interop")(
test("cats fiber wrapped in Resource can be canceled") {
Expand Down Expand Up @@ -203,6 +206,25 @@ object CatsInteropSpec extends CatsRunnableSpec {
!exception.get.getMessage.contains("Boxed Exception") &&
exception.get.getMessage.contains("The fiber was canceled")
)
}
},
test("CIO propagates interruption to ZIO") {
ZIO.succeedBlocking {
val latch = new CompletableFuture[Unit]()
val ref = new AtomicBoolean(false)
val zioF: CIO[Nothing] =
(ZIO.yieldNow.as(latch.complete(())) *> ZIO.never)
.onInterrupt(ZIO.succeed(ref.set(true)))
.toEffect[CIO]

val value = zioF.start
.productL(CIO.fromCompletableFuture(CIO(latch)))
.flatMap { (fib: cats.effect.FiberIO[Nothing]) =>
fib.cancel *> CIO(ref.get())
}
.unsafeRunSync()

assertTrue(value)
}
} @@ TestAspect.nonFlaky(1000)
)
}
24 changes: 15 additions & 9 deletions zio-interop-cats/shared/src/main/scala/zio/interop/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,27 @@ package object interop {
@inline def toEffect[F[_], R, A](rio: RIO[R, A])(implicit R: Runtime[R], F: Async[F], trace: Trace): F[A] =
F.defer {
val interrupted = new AtomicBoolean(true)
F.async[Exit[Throwable, A]] { cb =>
Unsafe.unsafe { implicit unsafe =>
val fiber = R.unsafe.fork {
F.asyncCheckAttempt[Exit[Throwable, A]] { cb =>
F.delay {
implicit val unsafe: Unsafe = Unsafe.unsafe

val out = R.unsafe.runOrFork {
signalOnNoExternalInterrupt {
rio
}(ZIO.succeed(interrupted.set(false)))
}
fiber.unsafe
.addObserver(exit => cb(Right(exit)))
val cancelerEffect = F.delay {
val _ = fiber.interrupt
out match {

Choose a reason for hiding this comment

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

Cats syntax is imported so you could use out.leftMap

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about it but I decided against it as it's going to increase allocations from the Function1 and the closures of local vals captured within the function. I think we can afford a bit of "uglier code" to improve performance, wdut?

case Left(fiber) =>
val completeCb = (exit: Exit[Throwable, A]) => cb(Right(exit))
fiber.unsafe.addObserver(completeCb)
Left(Some(F.async_ { cb =>
fiber.unsafe.addObserver(_ => cb(Right(())))
fiber.unsafe.removeObserver(completeCb)
fiber.tellInterrupt(Cause.interrupt(fiber.id))
}))
case Right(v) => Right(v) // No need to invoke the callback, sync resumption will take place
}
F.pure(Some(cancelerEffect))
}

}.flatMap { exit =>
toOutcomeThrowableOtherFiber(interrupted.get())(F.pure(_: A), exit) match {
case Outcome.Succeeded(fa) =>
Expand Down