Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ private[effect] object IOAppPlatform {
case Left(t) =>
IO(Logger.reportFailure(t)) *>
IO(sys.exit(ExitCode.Error.code))
case Right(0) =>
IO.unit
Copy link
Member

Choose a reason for hiding this comment

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

JS is always tricky. I guess at this point the keep-alive was canceled.

But yes, this is better for JS as well.

case Right(code) =>
IO(sys.exit(code))
}.unsafeRunSync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ private[effect] object IOAppPlatform {
val code = mainFiber(args, timer)(run).flatMap(_.join)
.handleErrorWith(t => IO(Logger.reportFailure(t)) *> IO(ExitCode.Error.code))
.unsafeRunSync()
sys.exit(code)

if (code === 0) {
// Return naturally from main. This allows any non-daemon
// threads to gracefully complete their work, and managed
// environments to execute their own shutdown hooks.
()
}
else sys.exit(code)
}

def mainFiber(args: Array[String], timer: Eval[Timer[IO]])(run: List[String] => IO[ExitCode]): IO[Fiber[IO, Int]] = {
Expand Down
18 changes: 13 additions & 5 deletions core/shared/src/main/scala/cats/effect/IOApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ package effect
import cats.effect.internals.IOAppPlatform

/**
* `App` type that runs a [[cats.effect.IO]] and exits with the
* returned code. If the `IO` raises an error, then the stack trace
* is printed to standard error and the JVM exits with code 1.
* `App` type that runs a [[cats.effect.IO]]. Shutdown occurs after
* the `IO` completes, as follows:
*
* - If completed with `ExitCode.Success`, the main method exits and
* shutdown is handled by the platform.
*
* - If completed with any other [[ExitCode]], `sys.exit` is called
* with the specified code.
*
* - If the `IO` raises an error, the stack trace is printed to
* standard error and `sys.exit(1)` is called.
*
* When a shutdown is requested via a signal, the `IO` is canceled and
* we wait for the `IO` to release any resources. The JVM exits with
* the numeric value of the signal plus 128.
* we wait for the `IO` to release any resources. The process exits
* with the numeric value of the signal plus 128.
*
* {{{
* import cats.effect.IO
Expand Down