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 cancelable to guarantee cancelation #4118

Merged
Merged
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 @@ -264,7 +264,9 @@ trait GenSpawn[F[_], E] extends MonadCancel[F, E] with Unique[F] {
def cancelable[A](fa: F[A], fin: F[Unit]): F[A] =
uncancelable { poll =>
start(fa) flatMap { fiber =>
poll(fiber.join).onCancel(fin *> fiber.cancel).flatMap(_.embed(poll(canceled *> never)))
poll(fiber.join)
.onCancel(fin.guarantee(fiber.cancel))
.flatMap(_.embed(poll(canceled *> never)))
}
}

Expand Down
23 changes: 23 additions & 0 deletions tests/shared/src/test/scala/cats/effect/IOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,29 @@ class IOSpec extends BaseSpec with Discipline with IOPlatformSpecification {
} must completeAs(())
}

"cancelable waits for termination" in ticked { implicit ticker =>
def test(fin: IO[Unit]) = {
val go = IO.never.uncancelable.cancelable(fin)
go.start.flatMap(IO.sleep(1.second) *> _.cancel)
}

test(IO.unit) must nonTerminate
test(IO.raiseError(new Exception)) must nonTerminate
test(IO.canceled) must nonTerminate
}

"cancelable cancels task" in ticked { implicit ticker =>
def test(fin: IO[Unit]) =
IO.deferred[Unit].flatMap { latch =>
val go = IO.never[Unit].onCancel(latch.complete(()).void).cancelable(fin)
go.start.flatMap(IO.sleep(1.second) *> _.cancel) *> latch.get
}

test(IO.unit) must completeAs(())
test(IO.raiseError(new Exception)) must completeAs(())
test(IO.canceled) must completeAs(())
}

"only unmask within current fiber" in ticked { implicit ticker =>
var passed = false
val test = IO uncancelable { poll =>
Expand Down
Loading