Skip to content

Commit

Permalink
Merge pull request typelevel#4044 from fredfp/fix/3993
Browse files Browse the repository at this point in the history
Use `IOApp#reportFailure` instead of `printStackTrace()` for blocking EC
  • Loading branch information
djspiewak authored May 25, 2024
2 parents 636ab77 + 85d54d3 commit f18a00d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 5 additions & 1 deletion core/jvm/src/main/scala/cats/effect/IOApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,11 @@ trait IOApp {
)

val (blocking, blockDown) =
IORuntime.createDefaultBlockingExecutionContext()
IORuntime.createDefaultBlockingExecutionContext(
threadPrefix = "io-blocking",
reportFailure =
(t: Throwable) => reportFailure(t).unsafeRunAndForgetWithoutCallback()(runtime)
)

IORuntime(
compute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,22 @@ private[unsafe] abstract class IORuntimeCompanionPlatform { this: IORuntime.type
createDefaultComputeThreadPool(self(), threads, threadPrefix)

def createDefaultBlockingExecutionContext(
threadPrefix: String = "io-blocking"): (ExecutionContext, () => Unit) = {
threadPrefix: String = "io-blocking"
): (ExecutionContext, () => Unit) =
createDefaultBlockingExecutionContext(threadPrefix, _.printStackTrace())

private[effect] def createDefaultBlockingExecutionContext(
threadPrefix: String,
reportFailure: Throwable => Unit
): (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

0 comments on commit f18a00d

Please sign in to comment.