Skip to content

Commit

Permalink
Merge pull request #4223 from armanbilge/fix/mainthread-runnable-failure
Browse files Browse the repository at this point in the history
Report non-fatal `Runnable` failures on `MainThread` EC
  • Loading branch information
djspiewak authored Jan 2, 2025
2 parents a49a028 + ddbb603 commit d1369b7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
7 changes: 1 addition & 6 deletions core/jvm/src/main/scala/cats/effect/IOApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -568,12 +568,7 @@ trait IOApp {
r.run()
} catch {
case t if NonFatal(t) =>
if (isForked) {
t.printStackTrace()
System.exit(1)
} else {
throw t
}
IOApp.this.reportFailure(t).unsafeRunAndForgetWithoutCallback()(runtime)

case t: Throwable =>
t.printStackTrace()
Expand Down
5 changes: 5 additions & 0 deletions ioapp-tests/src/test/scala/IOAppSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ class IOAppSpec extends Specification {
h.awaitStatus() mustEqual 0
}

"use configurable reportFailure for runnables on MainThread" in {
val h = platform("MainThreadReportFailureRunnable", List.empty)
h.awaitStatus() mustEqual 0
}

"warn on blocked threads" in {
val h = platform("BlockedThreads", List.empty)
h.awaitStatus()
Expand Down
13 changes: 13 additions & 0 deletions tests/jvm/src/main/scala/catseffect/examplesplatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ package examples {

}

object MainThreadReportFailureRunnable extends IOApp {

val exitCode = new AtomicReference[ExitCode](ExitCode.Error)

override def reportFailure(err: Throwable): IO[Unit] =
IO(exitCode.set(ExitCode.Success))

def run(args: List[String]): IO[ExitCode] =
IO(MainThread.execute(() => throw new Exception)) *>
IO.sleep(1.second) *> IO(exitCode.get)

}

object BlockedThreads extends IOApp.Simple {

override protected def blockedThreadDetectionEnabled = true
Expand Down

0 comments on commit d1369b7

Please sign in to comment.