cats-effect
cats-effect copied to clipboard
Dispatcher.unsafeToFuture reporting errors
CE3/Dispatcher/Future/Exceptions.
I have some IOs and convert them Futures (Enforced by Play) using Dispatcher.unsafeToFuture. In certain cases I raise errors using IO.raiseError to be handled in the unsafe region. The issue is that I see unwanted stacktraces in the console for these potentially expected errors printed by CE runtime(WorkStealingThreadPool) as they are rightfully considered as unhandled errors.
The following example may produce the case where an exception will be printed
package example
import cats.effect.{ IO, IOApp }
import cats.effect.std.{ Dispatcher, Queue }
import cats.effect.unsafe.implicits.global
import scala.concurrent.duration._
import scala.concurrent.{ Await, ExecutionContext, Future }
import scala.util.control.NoStackTrace
object MainDispatcher {
val (dispatcher, dispatcherShutdown) = Dispatcher[IO].allocated.unsafeRunSync()
def main(args: Array[String]): Unit = {
case class SomeError(message: String) extends NoStackTrace
val m: Future[Unit] =
dispatcher
.unsafeToFuture(IO.raiseError(SomeError("nice")))
.recover {
case SomeError(message) =>
println(message)
()
}(ExecutionContext.global)
Await.result(m, 10.seconds)
}
}
Thanks for the report. What is your version of Cats Effect?
Thanks for looking at this, it is 3.3.14.
As a workaround you can do something like this:
dispatcher
.unsafeToFuture(myIO.attempt)
.flatMap {
case Left(ex) => Future.failed(ex)
case Right(result) => Future.successful(result)
}
This is most likely due to https://github.com/typelevel/cats-effect/pull/2868. I'm not sure if this should count as an unhandled error, since it is being reported to the Future and thus should be handled there. It seems like we should be able to fix this.
I'd like to investigate this one, cc @armanbilge :D
I think this was fixed in https://github.com/typelevel/cats-effect/pull/3487 :)