mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-26 22:34:27 +02:00
8303198: System and Runtime.exit() resilience to logging errors
Reviewed-by: dholmes, jpai, alanb
This commit is contained in:
parent
80739e11b5
commit
379f2061aa
2 changed files with 67 additions and 27 deletions
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue