Skip to content

Commit

Permalink
Use IOApp.reportFailure instead of printStackTrace() where possible - f…
Browse files Browse the repository at this point in the history
…ixes #3993
  • Loading branch information
fredfp committed Mar 19, 2024
1 parent b77600c commit cc64a55
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
4 changes: 3 additions & 1 deletion core/jvm/src/main/scala/cats/effect/IOApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ trait IOApp {
)

val (blocking, blockDown) =
IORuntime.createDefaultBlockingExecutionContext()
IORuntime.createDefaultBlockingExecutionContext(
reportFailure = t => reportFailure(t).unsafeRunAndForgetWithoutCallback()(runtime)
)

IORuntime(
compute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private[unsafe] abstract class IORuntimeCompanionPlatform { this: IORuntime.type
threadPrefix,
blockerThreadPrefix,
60.seconds,
_.printStackTrace(),
t => self.compute.reportFailure(t),
false
)

Expand All @@ -186,15 +186,17 @@ private[unsafe] abstract class IORuntimeCompanionPlatform { this: IORuntime.type
createDefaultComputeThreadPool(self(), threads, threadPrefix)

def createDefaultBlockingExecutionContext(
threadPrefix: String = "io-blocking"): (ExecutionContext, () => Unit) = {
threadPrefix: String = "io-blocking",
reportFailure: Throwable => Unit = _.printStackTrace()
): (ExecutionContext, () => Unit) = {
val threadCount = new AtomicInteger(0)
val executor = Executors.newCachedThreadPool { (r: Runnable) =>
val t = new Thread(r)
t.setName(s"${threadPrefix}-${threadCount.getAndIncrement()}")
t.setDaemon(true)
t
}
(ExecutionContext.fromExecutor(executor), { () => executor.shutdown() })
(ExecutionContext.fromExecutor(executor, reportFailure), { () => executor.shutdown() })
}

def createDefaultScheduler(threadPrefix: String = "io-scheduler"): (Scheduler, () => Unit) = {
Expand Down
6 changes: 3 additions & 3 deletions core/shared/src/main/scala/cats/effect/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ sealed abstract class IO[+A] private () extends IOPlatform[A] {
cb(Left(new CancellationException("The fiber was canceled"))),
t => {
if (!NonFatal(t)) {
t.printStackTrace()
runtime.compute.reportFailure(t)
}
cb(Left(t))
},
Expand All @@ -977,7 +977,7 @@ sealed abstract class IO[+A] private () extends IOPlatform[A] {
cb(Outcome.canceled),
t => {
if (!NonFatal(t)) {
t.printStackTrace()
runtime.compute.reportFailure(t)
}
cb(Outcome.errored(t))
},
Expand All @@ -1002,7 +1002,7 @@ sealed abstract class IO[+A] private () extends IOPlatform[A] {
if (NonFatal(t)) {
if (runtime.config.reportUnhandledFiberErrors)
runtime.compute.reportFailure(t)
} else { t.printStackTrace() }
} else { runtime.compute.reportFailure(t) }
},
_ => ())
()
Expand Down
12 changes: 5 additions & 7 deletions std/shared/src/main/scala/cats/effect/std/Dispatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import cats.effect.kernel.{
Sync
}
import cats.effect.kernel.syntax.all._
import cats.effect.std.Dispatcher.parasiticEC
import cats.syntax.all._

import scala.annotation.tailrec
Expand Down Expand Up @@ -88,22 +87,21 @@ trait Dispatcher[F[_]] extends DispatcherPlatform[F] {
case _ => ()
}(parasiticEC)

protected def reportFailure(t: Throwable): Unit =
t.printStackTrace()
protected def reportFailure(t: Throwable): Unit

// package-private because it's just an internal utility which supports specific implementations
// anyone who needs this type of thing should use unsafeToFuture and then onComplete
private[std] def unsafeRunAsync[A](fa: F[A])(cb: Either[Throwable, A] => Unit): Unit =
unsafeToFuture(fa).onComplete(t => cb(t.toEither))(parasiticEC)
}

object Dispatcher {

private val parasiticEC: ExecutionContext = new ExecutionContext {
def execute(runnable: Runnable) = runnable.run()

def reportFailure(t: Throwable) = t.printStackTrace()
def reportFailure(t: Throwable) = Dispatcher.this.reportFailure(t)
}
}

object Dispatcher {

private[this] val Cpus: Int = Runtime.getRuntime().availableProcessors()

Expand Down

0 comments on commit cc64a55

Please sign in to comment.