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

Immediately surface fatal errors in IO.raiseError #3811

Merged
merged 8 commits into from
Nov 23, 2023
8 changes: 7 additions & 1 deletion core/shared/src/main/scala/cats/effect/IOFiber.scala
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,11 @@ private final class IOFiber[A](

case 1 =>
val cur = cur0.asInstanceOf[Error]
runLoop(failed(cur.t, 0), nextCancelation, nextAutoCede)
val ex = cur.t
if (!NonFatal(ex))
onFatalFailure(ex)

runLoop(failed(ex, 0), nextCancelation, nextAutoCede)

case 2 =>
val cur = cur0.asInstanceOf[Delay[Any]]
Expand Down Expand Up @@ -434,6 +438,8 @@ private final class IOFiber[A](
case 1 =>
val error = ioa.asInstanceOf[Error]
val t = error.t
if (!NonFatal(t))
onFatalFailure(t)
Comment on lines 448 to +450
Copy link
Member

@armanbilge armanbilge Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops, looks like the convention was val t = error.t and not val ex = error.t. Not a big deal :)

// We need to augment the exception here because it doesn't get
// forwarded to the `failed` path.
Tracing.augmentThrowable(runtime.enhancedExceptions, t, tracingEvents)
Expand Down
2 changes: 2 additions & 0 deletions tests/js/src/main/scala/catseffect/examplesplatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ package examples {
register(Arguments)
register(NonFatalError)
register(FatalError)
register(RaiseFatalErrorAttempt)
register(RaiseFatalErrorHandle)
registerRaw(FatalErrorRaw)
register(Canceled)
registerLazy("catseffect.examples.GlobalRacingInit", GlobalRacingInit)
Expand Down
14 changes: 14 additions & 0 deletions tests/jvm/src/test/scala/cats/effect/IOAppSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ class IOAppSpec extends Specification {
h.stderr() must contain("Boom!")
}

"exit on raising a fatal error with attempt" in {
val h = platform(RaiseFatalErrorAttempt, List.empty)
h.awaitStatus() mustEqual 1
h.stderr() must contain("Boom!")
h.stdout() must not(contain("sadness"))
}

"exit on raising a fatal error with handleError" in {
val h = platform(RaiseFatalErrorHandle, List.empty)
h.awaitStatus() mustEqual 1
h.stderr() must contain("Boom!")
h.stdout() must not(contain("sadness"))
}

"warn on global runtime collision" in {
val h = platform(GlobalRacingInit, List.empty)
h.awaitStatus() mustEqual 0
Expand Down
18 changes: 18 additions & 0 deletions tests/shared/src/main/scala/catseffect/examples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ package examples {
}
}

object RaiseFatalErrorAttempt extends IOApp {
def run(args: List[String]): IO[ExitCode] = {
IO.raiseError[Unit](new OutOfMemoryError("Boom!"))
.attempt
.flatMap(_ => IO.println("sadness"))
.as(ExitCode.Success)
}
}

object RaiseFatalErrorHandle extends IOApp {
def run(args: List[String]): IO[ExitCode] = {
IO.raiseError[Unit](new OutOfMemoryError("Boom!"))
.handleError(_ => ())
.flatMap(_ => IO.println("sadness"))
.as(ExitCode.Success)
}
}

object Canceled extends IOApp {
def run(args: List[String]): IO[ExitCode] =
IO.canceled.as(ExitCode.Success)
Expand Down
Loading