Skip to content

[SPARK-11655] [core] Fix deadlock in handling of launcher stop(). #9633

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 1 commit into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import java.net.{InetAddress, Socket}

import org.apache.spark.SPARK_VERSION
import org.apache.spark.launcher.LauncherProtocol._
import org.apache.spark.util.ThreadUtils
import org.apache.spark.util.{ThreadUtils, Utils}

/**
* A class that can be used to talk to a launcher server. Users should extend this class to
Expand Down Expand Up @@ -88,12 +88,20 @@ private[spark] abstract class LauncherBackend {
*/
protected def onDisconnected() : Unit = { }

private def fireStopRequest(): Unit = {
val thread = LauncherBackend.threadFactory.newThread(new Runnable() {
override def run(): Unit = Utils.tryLogNonFatalError {
onStopRequest()
}
})
thread.start()
}

private class BackendConnection(s: Socket) extends LauncherConnection(s) {

override protected def handle(m: Message): Unit = m match {
case _: Stop =>
onStopRequest()
fireStopRequest()

case _ =>
throw new IllegalArgumentException(s"Unexpected message type: ${m.getClass().getName()}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,19 @@ private[spark] class SparkDeploySchedulerBackend(
}

private def stop(finalState: SparkAppHandle.State): Unit = synchronized {
stopping = true
try {
stopping = true

launcherBackend.setState(finalState)
launcherBackend.close()
super.stop()
client.stop()

super.stop()
client.stop()

val callback = shutdownCallback
if (callback != null) {
callback(this)
val callback = shutdownCallback
if (callback != null) {
callback(this)
}
} finally {
launcherBackend.setState(finalState)
launcherBackend.close()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.spark.launcher;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadFactory;
Expand Down Expand Up @@ -102,8 +103,20 @@ public synchronized void kill() {
disconnect();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was initially worried that this needs to be in a try block but it doesn't look like disconnect() is capable of throwing any exceptions.

}
if (childProc != null) {
childProc.destroy();
childProc = null;
try {
childProc.exitValue();
} catch (IllegalThreadStateException e) {
// Child is still alive. Try to use Java 8's "destroyForcibly()" if available,
// fall back to the old API if it's not there.
try {
Method destroy = childProc.getClass().getMethod("destroyForcibly");
destroy.invoke(childProc);
} catch (Exception inner) {
childProc.destroy();
}
} finally {
childProc = null;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public boolean isFinal() {
* Tries to kill the underlying application. Implies {@link #disconnect()}. This will not send
* a {@link #stop()} message to the application, so it's recommended that users first try to
* stop the application cleanly and only resort to this method if that fails.
* <p>
* Note that if the application is running as a child process, this method fail to kill the
* process when using Java 7. This may happen if, for example, the application is deadlocked.
*/
void kill();

Expand Down