@@ -26,27 +26,34 @@ import org.apache.spark.internal.Logging
2626 */
2727private [spark] object SparkUncaughtExceptionHandler
2828 extends Thread .UncaughtExceptionHandler with Logging {
29+ private [this ] var exitOnException = true
2930
30- override def uncaughtException (thread : Thread , exception : Throwable ) {
31- try {
32- // Make it explicit that uncaught exceptions are thrown when container is shutting down.
33- // It will help users when they analyze the executor logs
34- val inShutdownMsg = if (ShutdownHookManager .inShutdown()) " [Container in shutdown] " else " "
35- val errMsg = " Uncaught exception in thread "
36- logError(inShutdownMsg + errMsg + thread, exception)
31+ def apply (exitOnException : Boolean ): Thread .UncaughtExceptionHandler = {
32+ this .exitOnException = exitOnException
33+ this
34+ }
3735
38- // We may have been called from a shutdown hook. If so, we must not call System.exit().
39- // (If we do, we will deadlock.)
40- if (! ShutdownHookManager .inShutdown()) {
36+ override def uncaughtException (thread : Thread , exception : Throwable ) {
37+ // Make it explicit that uncaught exceptions are thrown when process is shutting down.
38+ // It will help users when they analyze the executor logs
39+ val errMsg = " Uncaught exception in thread " + thread
40+ if (ShutdownHookManager .inShutdown()) {
41+ logError(" [Process in shutdown] " + errMsg, exception)
42+ } else if (exception.isInstanceOf [Error ] ||
43+ (! exception.isInstanceOf [Error ] && exitOnException)) {
44+ try {
45+ logError(errMsg + " . Shutting down now.." , exception)
4146 if (exception.isInstanceOf [OutOfMemoryError ]) {
4247 System .exit(SparkExitCode .OOM )
4348 } else {
4449 System .exit(SparkExitCode .UNCAUGHT_EXCEPTION )
4550 }
51+ } catch {
52+ case oom : OutOfMemoryError => Runtime .getRuntime.halt(SparkExitCode .OOM )
53+ case t : Throwable => Runtime .getRuntime.halt(SparkExitCode .UNCAUGHT_EXCEPTION_TWICE )
4654 }
47- } catch {
48- case oom : OutOfMemoryError => Runtime .getRuntime.halt(SparkExitCode .OOM )
49- case t : Throwable => Runtime .getRuntime.halt(SparkExitCode .UNCAUGHT_EXCEPTION_TWICE )
55+ } else {
56+ logError(errMsg, exception)
5057 }
5158 }
5259
0 commit comments