Skip to content

[SPARK-12049] [CORE] User JVM shutdown hook can cause deadlock at shutdown #10042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions core/src/main/scala/org/apache/spark/util/ShutdownHookManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private[spark] object ShutdownHookManager extends Logging {
private [util] class SparkShutdownHookManager {

private val hooks = new PriorityQueue[SparkShutdownHook]()
private var shuttingDown = false
@volatile private var shuttingDown = false

/**
* Install a hook to run at shutdown and run all registered hooks in order. Hadoop 1.x does not
Expand All @@ -232,28 +232,27 @@ private [util] class SparkShutdownHookManager {
}
}

def runAll(): Unit = synchronized {
def runAll(): Unit = {
shuttingDown = true
while (!hooks.isEmpty()) {
Try(Utils.logUncaughtExceptions(hooks.poll().run()))
var nextHook: SparkShutdownHook = null
while ({ nextHook = hooks.synchronized { hooks.poll() }; nextHook != null }) {
Try(Utils.logUncaughtExceptions(nextHook.run()))
}
}

def add(priority: Int, hook: () => Unit): AnyRef = synchronized {
checkState()
val hookRef = new SparkShutdownHook(priority, hook)
hooks.add(hookRef)
hookRef
}

def remove(ref: AnyRef): Boolean = synchronized {
hooks.remove(ref)
def add(priority: Int, hook: () => Unit): AnyRef = {
hooks.synchronized {
if (shuttingDown) {
throw new IllegalStateException("Shutdown hooks cannot be modified during shutdown.")
}
val hookRef = new SparkShutdownHook(priority, hook)
hooks.add(hookRef)
hookRef
}
}

private def checkState(): Unit = {
if (shuttingDown) {
throw new IllegalStateException("Shutdown hooks cannot be modified during shutdown.")
}
def remove(ref: AnyRef): Boolean = {
hooks.synchronized { hooks.remove(ref) }
}

}
Expand Down