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

Skip to content

ConcurrentLaws.acquireIsNotCancelable unreasonable assumption about Fiber.cancel #375

@alexandru

Description

@alexandru

As seen in zio/zio#267

We have this law:

  def acquireIsNotCancelable[A](a1: A, a2: A) = {
    val lh =
      for {
        mVar  <- MVar[F].of(a1)
        latch <- Deferred.uncancelable[F, Unit]
        task   = F.bracket(latch.complete(()) *> mVar.put(a2))(_ => F.never[A])(_ => F.unit)
        fiber <- F.start(task)
        _     <- latch.get
        _     <- fiber.cancel
        _     <- contextShift.shift
        _     <- mVar.take
        out   <- mVar.take
      } yield out

    lh <-> F.pure(a2)
  }

This law introduces the assumption that fiber.cancel doesn't block. In cats.effect.IO's implementation there aren't at that point any finalizers registered so cats.effect.IO doesn't block on anything. But ZIO blocks for the completion of that fiber.

Which means that we need a fix that forks the cancel operation. E.g. we could use F.start(fiber.cancel) instead. Maybe even with an extra contextShift.shift just to be sure that it is indeed concurrent.


Published hash version with the fix for testing purposes:

1.0.0-1182d8c

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions