Skip to content

Commit 02f1c9d

Browse files
committed
Handle exceptions in externally submitted sleeps
1 parent 1908673 commit 02f1c9d

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

core/jvm/src/main/scala/cats/effect/unsafe/WorkStealingThreadPool.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import cats.effect.tracing.TracingConstants
3636
import scala.collection.mutable
3737
import scala.concurrent.ExecutionContextExecutor
3838
import scala.concurrent.duration.{Duration, FiniteDuration}
39+
import scala.util.control.NonFatal
3940

4041
import java.time.Instant
4142
import java.time.temporal.ChronoField
@@ -662,7 +663,14 @@ private[effect] final class WorkStealingThreadPool[P](
662663

663664
override def sleep(delay: FiniteDuration, task: Runnable): Runnable = {
664665
val cb = new AtomicBoolean with (Right[Nothing, Unit] => Unit) { // run at most once
665-
def apply(ru: Right[Nothing, Unit]) = if (compareAndSet(false, true)) task.run()
666+
def apply(ru: Right[Nothing, Unit]) = if (compareAndSet(false, true)) {
667+
try {
668+
task.run()
669+
} catch {
670+
case ex if NonFatal(ex) =>
671+
reportFailure(ex)
672+
}
673+
}
666674
}
667675

668676
val cancel = sleepInternal(delay, cb)

0 commit comments

Comments
 (0)