Skip to content

Commit

Permalink
Allow application to exit on normal completion (#8811)
Browse files Browse the repository at this point in the history
* Avoid blocking the app on normal completion

* Fulfill the promise also for catastrophic failures

* Revert "Fulfill the promise also for catastrophic failures"

This reverts commit 8735579.
  • Loading branch information
kyri-petrou committed May 2, 2024
1 parent f4042b6 commit 4afafd0
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions core/jvm/src/main/scala/zio/ZIOAppPlatformSpecific.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ private[zio] trait ZIOAppPlatformSpecific { self: ZIOApp =>
runtime.unsafe.run {
ZIO.uninterruptibleMask { restore =>
for {
fiberId <- ZIO.fiberId
p <- Promise.make[Nothing, Set[FiberId.Runtime]]
interrupt = interruptRootFibers(p)
fiber <- restore(workflow)
.foldCauseZIO(
cause => interrupt *> exit(ExitCode.failure) *> ZIO.refailCause(cause),
_ => interrupt *> exit(ExitCode.success)
)
.fork
fiberId <- ZIO.fiberId
p <- Promise.make[Nothing, Set[FiberId.Runtime]]
fiber <- restore(workflow).onExit { exit0 =>
val exitCode = if (exit0.isSuccess) ExitCode.success else ExitCode.failure
val interrupt = interruptRootFibers(p)
// If we're shutting down due to an external signal, the shutdown hook will fulfill the promise
// Otherwise it means we're shutting down due to normal completion and we need to fulfill the promise
ZIO.unless(shuttingDown.get())(p.succeed(Set(fiberId))) *> interrupt *> exit(exitCode)
}.fork
_ <-
ZIO.succeed(Platform.addShutdownHook { () =>
if (!shuttingDown.getAndSet(true)) {
Expand Down

0 comments on commit 4afafd0

Please sign in to comment.