Skip to content
Snippets Groups Projects
Commit 092c631f authored by Charles Reiss's avatar Charles Reiss
Browse files

Pull detection of being in a shutdown hook into utility function.

parent d0588bd6
No related branches found
No related tags found
No related merge requests found
...@@ -454,4 +454,25 @@ private object Utils extends Logging { ...@@ -454,4 +454,25 @@ private object Utils extends Logging {
def clone[T](value: T, serializer: SerializerInstance): T = { def clone[T](value: T, serializer: SerializerInstance): T = {
serializer.deserialize[T](serializer.serialize(value)) serializer.deserialize[T](serializer.serialize(value))
} }
/**
* Detect whether this thread might be executing a shutdown hook. Will always return true if
* the current thread is a running a shutdown hook but may spuriously return true otherwise (e.g.
* if System.exit was just called by a concurrent thread).
*
* Currently, this detects whether the JVM is shutting down by Runtime#addShutdownHook throwing
* an IllegalStateException.
*/
def inShutdown(): Boolean = {
try {
val hook = new Thread {
override def run() {}
}
Runtime.getRuntime.addShutdownHook(hook)
Runtime.getRuntime.removeShutdownHook(hook)
} catch {
case ise: IllegalStateException => return true
}
return false
}
} }
...@@ -52,20 +52,8 @@ private[spark] class Executor extends Logging { ...@@ -52,20 +52,8 @@ private[spark] class Executor extends Logging {
logError("Uncaught exception in thread " + thread, exception) logError("Uncaught exception in thread " + thread, exception)
// We may have been called from a shutdown hook. If so, we must not call System.exit(). // We may have been called from a shutdown hook. If so, we must not call System.exit().
// (If we do, we will deadlock.) Runtime#addShutdownHook should fail if we are shutting // (If we do, we will deadlock.)
// down, which would either occur if we were called from a shutdown hook or if if (!Utils.inShutdown()) {
// a System.exit() occured concurrently.
var shuttingDown = false
try {
val hook = new Thread {
override def run() {}
}
Runtime.getRuntime.addShutdownHook(hook)
Runtime.getRuntime.removeShutdownHook(hook)
} catch {
case ise: IllegalStateException => shuttingDown = true
}
if (!shuttingDown) {
if (exception.isInstanceOf[OutOfMemoryError]) { if (exception.isInstanceOf[OutOfMemoryError]) {
System.exit(ExecutorExitCode.OOM) System.exit(ExecutorExitCode.OOM)
} else { } else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment