diff --git a/core/src/test/scala/org/apache/spark/LocalRootDirsTest.scala b/core/src/test/scala/org/apache/spark/LocalRootDirsTest.scala index 3a813f4d8b53c..a7968b6f2b022 100644 --- a/core/src/test/scala/org/apache/spark/LocalRootDirsTest.scala +++ b/core/src/test/scala/org/apache/spark/LocalRootDirsTest.scala @@ -20,7 +20,7 @@ package org.apache.spark import java.io.File import java.util.UUID -import org.apache.spark.util.Utils +import org.apache.spark.util.{ShutdownHookManager, Utils} trait LocalRootDirsTest extends SparkFunSuite with LocalSparkContext { @@ -46,7 +46,13 @@ trait LocalRootDirsTest extends SparkFunSuite with LocalSparkContext { override def afterEach(): Unit = { try { - Utils.deleteRecursively(tempDir) + // SPARK-51030: Only perform manual cleanup of the `tempDir` if it has + // not been registered for cleanup via a shutdown hook, to avoid potential + // IOException due to race conditions during multithreaded cleanup. + if (!ShutdownHookManager.hasShutdownDeleteDir(tempDir) && + !ShutdownHookManager.hasRootAsShutdownDeleteDir(tempDir)) { + Utils.deleteRecursively(tempDir) + } Utils.clearLocalRootDirs() } finally { super.afterEach()