8303198: System and Runtime.exit() resilience to logging errors

Reviewed-by: dholmes, jpai, alanb
This commit is contained in:
Roger Riggs 2023-03-03 16:14:45 +00:00
parent 80739e11b5
commit 379f2061aa
2 changed files with 67 additions and 27 deletions

View file

@ -157,37 +157,40 @@ class Shutdown {
* which should pass a nonzero status code.
*/
static void exit(int status) {
System.Logger log = getRuntimeExitLogger(); // Locate the logger without holding the lock;
logRuntimeExit(status); // Log without holding the lock;
synchronized (Shutdown.class) {
/* Synchronize on the class object, causing any other thread
* that attempts to initiate shutdown to stall indefinitely
*/
if (log != null) {
Throwable throwable = new Throwable("Runtime.exit(" + status + ")");
log.log(System.Logger.Level.DEBUG, "Runtime.exit() called with status: " + status,
throwable);
}
beforeHalt();
runHooks();
halt(status);
}
}
/* Locate and return the logger for Shutdown.exit, if it is functional and DEBUG enabled.
* Exceptions should not prevent System.exit; the exception is printed and otherwise ignored.
/* Locate the logger and log the Runtime.exit(status).
* Catch and ignore any and all exceptions.
*/
private static System.Logger getRuntimeExitLogger() {
private static void logRuntimeExit(int status) {
try {
System.Logger log = System.getLogger("java.lang.Runtime");
return (log.isLoggable(System.Logger.Level.DEBUG)) ? log : null;
if (log.isLoggable(System.Logger.Level.DEBUG)) {
Throwable throwable = new Throwable("Runtime.exit(" + status + ")");
log.log(System.Logger.Level.DEBUG, "Runtime.exit() called with status: " + status,
throwable);
}
} catch (Throwable throwable) {
// Exceptions from locating the Logger are printed but do not prevent exit
System.err.println("Runtime.exit() log finder failed with: " + throwable.getMessage());
try {
// Exceptions from the Logger are printed but do not prevent exit
System.err.println("Runtime.exit(" + status + ") logging failed: " +
throwable.getMessage());
} catch (Throwable throwable2) {
// Ignore
}
}
return null;
}
/* Invoked by the JNI DestroyJavaVM procedure when the last non-daemon
* thread has finished. Unlike the exit method, this method does not
* actually halt the VM.