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

zio.App does not exit if fatal error occurs #4810

Closed
lyubortk opened this issue Mar 17, 2021 · 2 comments · Fixed by #4812
Closed

zio.App does not exit if fatal error occurs #4810

lyubortk opened this issue Mar 17, 2021 · 2 comments · Fixed by #4812
Assignees
Labels
bug Something isn't working

Comments

@lyubortk
Copy link

The following program never stops:

import zio.{ZIO, URIO, ExitCode}

object Main extends zio.App {
  def program: ZIO[zio.ZEnv, Throwable, Any] =
    // make sure that shutdown hook is registered
    zio.clock.sleep(java.time.Duration.ofSeconds(2)) *> ZIO.effect(throw new OutOfMemoryError())

  override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = program.exitCode
} 

I suppose that JVM just waits for that shutdown hook to finish its work (but it never happens):

_ <- IO.effectTotal(java.lang.Runtime.getRuntime.addShutdownHook(new Thread {
override def run() = {
val _ = unsafeRunSync(fiber.interrupt)
}
}))

Could it be that the fiber in which a fatal error was thrown just cannot be interrupted? If so then it explains why the shutdown hook blocks indefinitely.


P.S.
For some reason it looks like the code finishes normally when run with sbt (sbt 1.4.9 prints that the program has completed and then just freezes). However, if the compiled jars are invoked directly with java then the program never stops just as I described.

ZIO 1.0.5
openjdk 11.0.10

@jdegoes
Copy link
Member

jdegoes commented Mar 17, 2021

The desired behavior of this program is that System.exit is invoked with a dump of the out of memory error.

@jdegoes jdegoes added the bug Something isn't working label Mar 17, 2021
@adamgfraser
Copy link
Contributor

It looks like we suspend indefinitely on attempting to interrupt the fiber in the shut down hook. One possibility is to fork the interruption so we don't wait for it. That results in printing the out of memory error and exiting. Looking into why we can't interrupt there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants