diff --git a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/V2WriteRequireDistributionTest.scala b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/V2WriteRequireDistributionTest.scala index 69cbfa97bebf..2c7d1788e134 100644 --- a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/V2WriteRequireDistributionTest.scala +++ b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/V2WriteRequireDistributionTest.scala @@ -24,7 +24,7 @@ import org.apache.paimon.spark.catalog.WithPaimonCatalog import org.apache.paimon.spark.commands.FixedBucketExpression import org.apache.paimon.utils.ExceptionUtils -import org.apache.spark.SparkConf +import org.apache.spark.{SparkConf, TaskContext, TaskKilledException} import org.apache.spark.scheduler.{SparkListener, SparkListenerStageSubmitted, SparkListenerTaskStart} import org.apache.spark.sql.{Row, SparkSession} import org.apache.spark.sql.connector.catalog.{Identifier, TableCapability, TableCatalog} @@ -526,9 +526,21 @@ class V2WriteRequireDistributionTest extends PaimonSparkTestBase with AdaptiveSp session.udf.register( "ctas_interrupt_input", (id: Long) => { - Thread.sleep(30000) + // Before Spark 4.0 (SPARK-44818), a kill that lands after the executor's kill check + // but before Task.run records the task thread only marks the TaskContext: the thread + // is never interrupted and the task keeps running. Poll the mark as well, otherwise + // the cancelled task keeps a local[2] slot and can starve the later cases. + val context = TaskContext.get() + val deadline = System.currentTimeMillis() + 30000 + while (System.currentTimeMillis() < deadline) { + if (context.isInterrupted()) { + throw new TaskKilledException("ctas_interrupt_input was cancelled") + } + Thread.sleep(20) + } id.toInt - }) + } + ) val driver = new Thread("interrupted-ctas-driver") { override def run(): Unit = { SparkSession.setActiveSession(session)