Skip to content

Commit fec67ed

Browse files
cloud-fangatorsmile
authored andcommitted
[SPARK-25076][SQL] SQLConf should not be retrieved from a stopped SparkSession
## What changes were proposed in this pull request? When a `SparkSession` is stopped, `SQLConf.get` should use the fallback conf to avoid weird issues like ``` sbt.ForkMain$ForkError: java.lang.IllegalStateException: LiveListenerBus is stopped. at org.apache.spark.scheduler.LiveListenerBus.addToQueue(LiveListenerBus.scala:97) at org.apache.spark.scheduler.LiveListenerBus.addToStatusQueue(LiveListenerBus.scala:80) at org.apache.spark.sql.internal.SharedState.<init>(SharedState.scala:93) at org.apache.spark.sql.SparkSession$$anonfun$sharedState$1.apply(SparkSession.scala:120) at org.apache.spark.sql.SparkSession$$anonfun$sharedState$1.apply(SparkSession.scala:120) at scala.Option.getOrElse(Option.scala:121) ... ``` ## How was this patch tested? a new test suite Closes #22056 from cloud-fan/session. Authored-by: Wenchen Fan <[email protected]> Signed-off-by: Xiao Li <[email protected]>
1 parent bd6db15 commit fec67ed

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

sql/core/src/main/scala/org/apache/spark/sql/SparkSession.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ class SparkSession private(
9292

9393
// If there is no active SparkSession, uses the default SQL conf. Otherwise, use the session's.
9494
SQLConf.setSQLConfGetter(() => {
95-
SparkSession.getActiveSession.map(_.sessionState.conf).getOrElse(SQLConf.getFallbackConf)
95+
SparkSession.getActiveSession.filterNot(_.sparkContext.isStopped).map(_.sessionState.conf)
96+
.getOrElse(SQLConf.getFallbackConf)
9697
})
9798

9899
/**

sql/core/src/test/scala/org/apache/spark/sql/LocalSparkSession.scala

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,14 @@ trait LocalSparkSession extends BeforeAndAfterEach with BeforeAndAfterAll { self
3636

3737
override def afterEach() {
3838
try {
39-
resetSparkContext()
39+
LocalSparkSession.stop(spark)
4040
SparkSession.clearActiveSession()
4141
SparkSession.clearDefaultSession()
42+
spark = null
4243
} finally {
4344
super.afterEach()
4445
}
4546
}
46-
47-
def resetSparkContext(): Unit = {
48-
LocalSparkSession.stop(spark)
49-
spark = null
50-
}
51-
5247
}
5348

5449
object LocalSparkSession {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.internal
19+
20+
import org.apache.spark.SparkFunSuite
21+
import org.apache.spark.sql.{LocalSparkSession, SparkSession}
22+
23+
class SQLConfGetterSuite extends SparkFunSuite with LocalSparkSession {
24+
25+
test("SPARK-25076: SQLConf should not be retrieved from a stopped SparkSession") {
26+
spark = SparkSession.builder().master("local").getOrCreate()
27+
assert(SQLConf.get eq spark.sessionState.conf,
28+
"SQLConf.get should get the conf from the active spark session.")
29+
spark.stop()
30+
assert(SQLConf.get eq SQLConf.getFallbackConf,
31+
"SQLConf.get should not get conf from a stopped spark session.")
32+
}
33+
}

0 commit comments

Comments
 (0)