@@ -58,12 +58,11 @@ import org.apache.spark.util._
5858 * cluster, and can be used to create RDDs, accumulators and broadcast variables on that cluster.
5959 *
6060 * Only one SparkContext may be active per JVM. You must `stop()` the active SparkContext before
61- * creating a new one. This limitation will eventually be removed; see SPARK-2243 for more details.
61+ * creating a new one. This limitation may eventually be removed; see SPARK-2243 for more details.
6262 *
6363 * @param config a Spark Config object describing the application configuration. Any settings in
6464 * this config overrides the default configs as well as system properties.
6565 */
66-
6766class SparkContext (config : SparkConf ) extends SparkStatusAPI with Logging {
6867
6968 // The call site where this SparkContext was constructed.
@@ -73,9 +72,9 @@ class SparkContext(config: SparkConf) extends SparkStatusAPI with Logging {
7372 private val allowMultipleContexts : Boolean =
7473 config.getBoolean(" spark.driver.allowMultipleContexts" , false )
7574
76-
7775 // In order to prevent multiple SparkContexts from being active at the same time, mark this
78- // context as having started construction
76+ // context as having started construction.
77+ // NOTE: this must be placed at the beginning of the SparkContext constructor.
7978 SparkContext .markPartiallyConstructed(this , allowMultipleContexts)
8079
8180 // This is used only by YARN for now, but should be relevant to other cluster types (Mesos,
@@ -1429,7 +1428,8 @@ class SparkContext(config: SparkConf) extends SparkStatusAPI with Logging {
14291428 }
14301429
14311430 // In order to prevent multiple SparkContexts from being active at the same time, mark this
1432- // context as having finished construction
1431+ // context as having finished construction.
1432+ // NOTE: this must be placed at the end of the SparkContext constructor.
14331433 SparkContext .setActiveContext(this , allowMultipleContexts)
14341434}
14351435
@@ -1473,9 +1473,14 @@ object SparkContext extends Logging {
14731473 SPARK_CONTEXT_CONSTRUCTOR_LOCK .synchronized {
14741474 contextBeingConstructed.foreach { otherContext =>
14751475 if (otherContext ne sc) { // checks for reference equality
1476+ // Since otherContext might point to a partially-constructed context, guard against
1477+ // its creationSite field being null:
1478+ val otherContextCreationSite =
1479+ Option (otherContext.creationSite).map(_.longForm).getOrElse(" unknown location" )
14761480 val warnMsg = " Another SparkContext is being constructed (or threw an exception in its" +
14771481 " constructor). This may indicate an error, since only one SparkContext may be" +
1478- " running in this JVM (see SPARK-2243)."
1482+ " running in this JVM (see SPARK-2243)." +
1483+ s " The other SparkContext was created at: \n $otherContextCreationSite"
14791484 logWarning(warnMsg)
14801485 }
14811486
0 commit comments