Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@lyubortk

Description

@lyubortk

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions