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

Handle Interruption After Fatal Error In FiberContext #4812

Merged
merged 4 commits into from
Mar 17, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions core/jvm/src/main/scala/zio/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package zio

import zio.internal.FiberContext

/**
* The entry point for a purely-functional application on the JVM.
*
Expand Down Expand Up @@ -55,9 +57,18 @@ trait App extends BootstrapRuntime {
for {
fiber <- run(args0.toList).fork
_ <- IO.effectTotal(java.lang.Runtime.getRuntime.addShutdownHook(new Thread {
override def run() = {
val _ = unsafeRunSync(fiber.interrupt)
}
override def run() =
if (FiberContext.fatal.get) {
println(
"**** WARNING ***\n" +
"Catastrophic JVM error encountered. " +
"Application not safely interrupted. " +
"Resources may be leaked. " +
"Check the logs for more details and consider overriding `Platform.reportFatal` to capture context."
)
} else {
val _ = unsafeRunSync(fiber.interrupt)
}
}))
result <- fiber.join
_ <- fiber.interrupt
Expand Down
17 changes: 10 additions & 7 deletions core/shared/src/main/scala/zio/internal/FiberContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -650,13 +650,14 @@ private[zio] final class FiberContext[E, A](
// either a bug in the interpreter or a bug in the user's code. Let the
// fiber die but attempt finalization & report errors.
case t: Throwable =>
curZio =
if (platform.fatal(t)) platform.reportFatal(t)
else {
setInterrupting(true)
curZio = if (platform.fatal(t)) {
fatal.set(true)
platform.reportFatal(t)
} else {
setInterrupting(true)

ZIO.die(t)
}
ZIO.die(t)
}
}
}
} finally Fiber._currentFiber.remove()
Expand Down Expand Up @@ -1066,7 +1067,6 @@ private[zio] final class FiberContext[E, A](
// For improved fairness, we resume in order of submission:
observers.reverse.foreach(k => k(result))
}

}
private[zio] object FiberContext {
sealed abstract class FiberState[+E, +A] extends Serializable with Product {
Expand Down Expand Up @@ -1103,4 +1103,7 @@ private[zio] object FiberContext {

private val noop: Option[Any => Unit] =
Some(_ => ())

val fatal: AtomicBoolean =
new AtomicBoolean(false)
}