Skip to content

[SPARK-23971] [BACKPORT-2.3] Should not leak Spark sessions across test suites #21197

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
9 changes: 7 additions & 2 deletions mllib/src/test/java/org/apache/spark/SharedSparkSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ public void setUp() throws IOException {

@After
public void tearDown() {
spark.stop();
spark = null;
try {
spark.stop();
spark = null;
} finally {
SparkSession.clearDefaultSession();
SparkSession.clearActiveSession();
}
}
}
23 changes: 21 additions & 2 deletions sql/core/src/main/scala/org/apache/spark/sql/SparkSession.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import org.apache.spark.sql.sources.BaseRelation
import org.apache.spark.sql.streaming._
import org.apache.spark.sql.types.{DataType, StructType}
import org.apache.spark.sql.util.ExecutionListenerManager
import org.apache.spark.util.Utils
import org.apache.spark.util.{CallSite, Utils}


/**
Expand Down Expand Up @@ -81,6 +81,9 @@ class SparkSession private(
@transient private[sql] val extensions: SparkSessionExtensions)
extends Serializable with Closeable with Logging { self =>

// The call site where this SparkSession was constructed.
private val creationSite: CallSite = Utils.getCallSite()

private[sql] def this(sc: SparkContext) {
this(sc, None, None, new SparkSessionExtensions)
}
Expand Down Expand Up @@ -763,7 +766,7 @@ class SparkSession private(


@InterfaceStability.Stable
object SparkSession {
object SparkSession extends Logging {

/**
* Builder for [[SparkSession]].
Expand Down Expand Up @@ -1079,4 +1082,20 @@ object SparkSession {
}
}

private[spark] def cleanupAnyExistingSession(): Unit = {
val session = getActiveSession.orElse(getDefaultSession)
if (session.isDefined) {
logWarning(
s"""An existing Spark session exists as the active or default session.
|This probably means another suite leaked it. Attempting to stop it before continuing.
|This existing Spark session was created at:
|
|${session.get.creationSite.longForm}
|
""".stripMargin)
session.get.stop()
SparkSession.clearActiveSession()
SparkSession.clearDefaultSession()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class SessionStateSuite extends SparkFunSuite {
if (activeSession != null) {
activeSession.stop()
activeSession = null
SparkSession.clearActiveSession()
SparkSession.clearDefaultSession()
}
super.afterAll()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ trait SharedSparkSession
protected implicit def sqlContext: SQLContext = _spark.sqlContext

protected def createSparkSession: TestSparkSession = {
SparkSession.cleanupAnyExistingSession()
new TestSparkSession(sparkConf)
}

Expand Down Expand Up @@ -92,11 +93,22 @@ trait SharedSparkSession
* Stop the underlying [[org.apache.spark.SparkContext]], if any.
*/
protected override def afterAll(): Unit = {
super.afterAll()
if (_spark != null) {
_spark.sessionState.catalog.reset()
_spark.stop()
_spark = null
try {
super.afterAll()
} finally {
try {
if (_spark != null) {
try {
_spark.sessionState.catalog.reset()
} finally {
_spark.stop()
_spark = null
}
}
} finally {
SparkSession.clearActiveSession()
SparkSession.clearDefaultSession()
}
}
}

Expand Down