diff --git a/python/pyspark/sql/connect/client/core.py b/python/pyspark/sql/connect/client/core.py index 1bd9725a9a7f0..461b68bb48d59 100644 --- a/python/pyspark/sql/connect/client/core.py +++ b/python/pyspark/sql/connect/client/core.py @@ -313,7 +313,7 @@ class DefaultChannelBuilder(ChannelBuilder): @staticmethod def default_port() -> int: - if "SPARK_TESTING" in os.environ and not is_remote_only(): + if "SPARK_LOCAL_REMOTE" in os.environ and not is_remote_only(): from pyspark.sql.session import SparkSession as PySparkSession # In the case when Spark Connect uses the local mode, it starts the regular Spark diff --git a/python/pyspark/sql/connect/session.py b/python/pyspark/sql/connect/session.py index c01c1e42a3185..fee9f9fdc88f9 100644 --- a/python/pyspark/sql/connect/session.py +++ b/python/pyspark/sql/connect/session.py @@ -1045,6 +1045,8 @@ def _start_connect_server(master: str, opts: Dict[str, Any]) -> None: overwrite_conf = opts overwrite_conf["spark.master"] = master overwrite_conf["spark.local.connect"] = "1" + # Always use an ephemeral port for local connect server + overwrite_conf["spark.connect.grpc.binding.port"] = "0" os.environ["SPARK_LOCAL_CONNECT"] = "1" # Configurations to be set if unset. @@ -1054,11 +1056,6 @@ def _start_connect_server(master: str, opts: Dict[str, Any]) -> None: "spark.sql.artifact.isolation.alwaysApplyClassloader": "true", } - if "SPARK_TESTING" in os.environ: - # For testing, we use 0 to use an ephemeral port to allow parallel testing. - # See also SPARK-42272. - overwrite_conf["spark.connect.grpc.binding.port"] = "0" - origin_remote = os.environ.get("SPARK_REMOTE", None) try: if origin_remote is not None: