From 4b73c7a1ba7cabfc99df7065f90d220b035a4aaf Mon Sep 17 00:00:00 2001 From: Daniel Urban Date: Tue, 29 Nov 2022 00:59:33 +0100 Subject: [PATCH] Fix #3283: make Deferred#complete uncancelable --- .../main/scala/cats/effect/kernel/Deferred.scala | 2 +- .../scala/cats/effect/kernel/DeferredSpec.scala | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/kernel/shared/src/main/scala/cats/effect/kernel/Deferred.scala b/kernel/shared/src/main/scala/cats/effect/kernel/Deferred.scala index bf015fd676..4357ef3755 100644 --- a/kernel/shared/src/main/scala/cats/effect/kernel/Deferred.scala +++ b/kernel/shared/src/main/scala/cats/effect/kernel/Deferred.scala @@ -186,7 +186,7 @@ object Deferred { } } - F.defer(loop()) + F.uncancelable(_ => F.defer(loop())) } } diff --git a/tests/shared/src/test/scala/cats/effect/kernel/DeferredSpec.scala b/tests/shared/src/test/scala/cats/effect/kernel/DeferredSpec.scala index 581b479e02..2f220a1472 100644 --- a/tests/shared/src/test/scala/cats/effect/kernel/DeferredSpec.scala +++ b/tests/shared/src/test/scala/cats/effect/kernel/DeferredSpec.scala @@ -147,5 +147,20 @@ class DeferredSpec extends BaseSpec { outer => } } + "issue #3283: complete must be uncancelable" in real { + + import cats.syntax.all._ + + for { + d <- Deferred[IO, Int] + attemptCompletion = { (n: Int) => d.complete(n).void } + res <- List( + IO.race(attemptCompletion(1), attemptCompletion(2)).void, + d.get.void + ).parSequence + r <- IO { (res == List((), ())) must beTrue } + } yield r + } + } }