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

Attempt to detect host environments (like unforked sbt) #2719

Merged
merged 2 commits into from
Jan 3, 2022
Merged
Changes from 1 commit
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
26 changes: 20 additions & 6 deletions core/jvm/src/main/scala/cats/effect/IOApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ trait IOApp {
def run(args: List[String]): IO[ExitCode]

final def main(args: Array[String]): Unit = {
// checked in openjdk 8-17; this attempts to detect when we're running under artificial environments, like sbt
Copy link

Choose a reason for hiding this comment

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

Does this comment mean that this has been tested on those Java versions? I'm on Java 23, and Thread.currentThread().getId() returns 1 - though that .getId() got deprecated in Java 19, because:

This method is not final and may be overridden to return a value that is not the thread ID. Use threadId() instead

val isForked = Thread.currentThread().getName() == "main"
djspiewak marked this conversation as resolved.
Show resolved Hide resolved

if (runtime == null) {
import unsafe.IORuntime

Expand Down Expand Up @@ -329,15 +332,26 @@ trait IOApp {
new NonDaemonThreadLogger().start()
else
()
} else {
} else if (isForked) {
System.exit(result.code)
}
case _: CancellationException =>
// Do not report cancelation exceptions but still exit with an error code.
System.exit(1)

case e: CancellationException =>
if (isForked)
// Do not report cancelation exceptions but still exit with an error code.
System.exit(1)
else
// if we're unforked, the only way to report cancelation is to throw
throw e

case NonFatal(t) =>
t.printStackTrace()
System.exit(1)
if (isForked) {
t.printStackTrace()
System.exit(1)
} else {
throw t
}

case t: Throwable =>
t.printStackTrace()
rt.halt(1)
Expand Down