@@ -89,6 +89,9 @@ private[spark] class ApplicationMaster(
8989
9090 @ volatile private var reporterThread : Thread = _
9191 @ volatile private var allocator : YarnAllocator = _
92+
93+ // A flag to check whether user has initialized spark context
94+ @ volatile private var registered = false
9295
9396 private val userClassLoader = {
9497 val classpath = Client .getUserClasspath(sparkConf)
@@ -319,7 +322,7 @@ private[spark] class ApplicationMaster(
319322 */
320323 final def unregister (status : FinalApplicationStatus , diagnostics : String = null ): Unit = {
321324 synchronized {
322- if (! unregistered) {
325+ if (registered && ! unregistered) {
323326 logInfo(s " Unregistering ApplicationMaster with $status" +
324327 Option (diagnostics).map(msg => s " (diag message: $msg) " ).getOrElse(" " ))
325328 unregistered = true
@@ -332,10 +335,15 @@ private[spark] class ApplicationMaster(
332335 synchronized {
333336 if (! finished) {
334337 val inShutdown = ShutdownHookManager .inShutdown()
335- logInfo(s " Final app status: $status, exitCode: $code" +
338+ if (registered) {
339+ exitCode = code
340+ finalStatus = status
341+ } else {
342+ finalStatus = FinalApplicationStatus .FAILED
343+ exitCode = ApplicationMaster .EXIT_SC_NOT_INITED
344+ }
345+ logInfo(s " Final app status: $finalStatus, exitCode: $exitCode" +
336346 Option (msg).map(msg => s " , (reason: $msg) " ).getOrElse(" " ))
337- exitCode = code
338- finalStatus = status
339347 finalMsg = msg
340348 finished = true
341349 if (! inShutdown && Thread .currentThread() != reporterThread && reporterThread != null ) {
@@ -439,12 +447,11 @@ private[spark] class ApplicationMaster(
439447 sc.getConf.get(" spark.driver.port" ),
440448 isClusterMode = true )
441449 registerAM(sc.getConf, rpcEnv, driverRef, sc.ui.map(_.webUrl), securityMgr)
450+ registered = true
442451 } else {
443452 // Sanity check; should never happen in normal operation, since sc should only be null
444453 // if the user app did not create a SparkContext.
445- if (! finished) {
446- throw new IllegalStateException (" SparkContext is null but app is still running!" )
447- }
454+ throw new IllegalStateException (" User did not initialize spark context!" )
448455 }
449456 userClassThread.join()
450457 } catch {
0 commit comments