Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report non-fatal Runnable failures on MainThread EC #4223

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading