diff --git a/core/src/main/scala/org/apache/spark/deploy/SparkSubmit.scala b/core/src/main/scala/org/apache/spark/deploy/SparkSubmit.scala index d7443951e7f9..c117435547e8 100644 --- a/core/src/main/scala/org/apache/spark/deploy/SparkSubmit.scala +++ b/core/src/main/scala/org/apache/spark/deploy/SparkSubmit.scala @@ -1005,17 +1005,20 @@ private[spark] class SparkSubmit extends Logging { e } + var exitCode = 0 + try { app.start(childArgs.toArray, sparkConf) } catch { case t: Throwable => + exitCode = -1 throw findCause(t) } finally { - if (args.master.startsWith("k8s") && !isShell(args.primaryResource) && - !isSqlShell(args.mainClass) && !isThriftServer(args.mainClass) && - !isConnectServer(args.mainClass)) { + if ((args.master.startsWith("k8s") || args.master.startsWith("yarn")) && + !isShell(args.primaryResource) && !isSqlShell(args.mainClass) && + !isThriftServer(args.mainClass) && !isConnectServer(args.mainClass)) { try { - SparkContext.getActive.foreach(_.stop()) + SparkContext.getActive.foreach(_.stop(exitCode)) } catch { case e: Throwable => logError(s"Failed to close SparkContext: $e") }