question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System.exit() is evil, pls no

See original GitHub issue

So I just discovered this using another dropwizard-guice library, but figured I should put it back here since it was caused by code based on this library.

So this line: https://github.com/HubSpot/dropwizard-guice/blob/master/src/main/java/com/hubspot/dropwizard/guice/GuiceBundle.java#L110

It’s meant to terminate the server if initialization fails, which does make sense; but I discovered that, depending on what logging library is used, System.exit() terminates so forcefully, that it even kills the task that would print out that super-useful error message on the previous line. As such, my app started spitting out exit code 1’s with no error message, no stacktrace; this was pretty terrible to debug. (Fun fact: If you put a breakpoint on the exit line, it actually gives the logger a chance to print, and you can see an error message again.)

In general, I’ve discovered that System.exit() is absolutely horrifying, in part because it’s one of the few things that can stop threads mid-try-block, with no catch or finally - a dubious honor shared with (1) daemon threads, and (2) outright crash/segfault of the JVM.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
Vandalkocommented, Jun 13, 2016

I was wondering for about an hour, why my application silently exits… It looks like stderr logs not flushed immediately so I had to help them do so…

// Inside Application class.
static {
    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        LoggingUtil.getLoggerContext().reset();
      }
    });
}

reset closes all log appenders and, as side-effect, flushes all underlying streams so, finally, I was able to see my error message

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java - Is it bad if a method invokes System.exit? - Stack Overflow
When you call System.exit() directly. You are unable to use this code inside a unit test or an application server.
Read more >
System.exit() : r/java - Reddit
I have been reviewing a few apps lately that are using System.exit() in exception scenarios, such as the app not being deployed properly....
Read more >
CWE-382: J2EE Bad Practices: Use of System.exit() - MITRE
Implementation, A call to System.exit() is probably part of leftover debug code or code imported from a non-J2EE application.
Read more >
Python static code analysis | bad-practice: "SystemExit" should ...
SystemExit is raised when sys.exit() is called. This exception is expected to propagate up until the application stops. It is ok to catch...
Read more >
Testing System.exit() with JUnit5 - Todd Ginsberg
An Extension to JUnit 5 for testing System.exit(), and how it works ... exits the JVM while it is under test is definitely...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found