System.exit() is evil, pls no
See original GitHub issueSo 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:
- Created 8 years ago
- Reactions:1
- Comments:13 (8 by maintainers)
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…reset
closes all log appenders and, as side-effect, flushes all underlying streams so, finally, I was able to see my error messagehttps://github.com/HubSpot/dropwizard-guice/pull/110