mridulm commented on code in PR #52373:
URL: https://github.com/apache/spark/pull/52373#discussion_r2605272732


##########
core/src/test/scala/org/apache/spark/util/UninterruptibleThreadSuite.scala:
##########
@@ -28,50 +28,69 @@ import org.apache.spark.SparkFunSuite
 
 class UninterruptibleThreadSuite extends SparkFunSuite {
 
-  /** Sleep millis and return true if it's interrupted */
+  /* Sleep millis and return true if it's interrupted */
   private def sleep(millis: Long): Boolean = {
     try {
       Thread.sleep(millis)
       false
     } catch {
-      case _: InterruptedException =>
+      case e: InterruptedException =>
+        log.error("Thread interrupted during sleep", e)
+        true
+    }
+  }
+
+  /* Await latch and return true if it's interrupted */
+  private def await(
+      latch: CountDownLatch,
+      timeout: Long = 10,
+      timeUnit: TimeUnit = TimeUnit.SECONDS): Boolean = {
+    try {
+      if (!latch.await(timeout, timeUnit)) {
+        log.error("timeout while waiting for the latch")
+        fail("timeout while waiting for the latch")
+      }
+      false
+    } catch {
+      case e: InterruptedException =>
+        log.error("Thread interrupted during await", e)
         true
     }
   }
 
   test("interrupt when runUninterruptibly is running") {
     val enterRunUninterruptibly = new CountDownLatch(1)
+    val interruptLatch = new CountDownLatch(1)
     @volatile var hasInterruptedException = false
     @volatile var interruptStatusBeforeExit = false
-    val t = new UninterruptibleThread("test") {
+    val t = new UninterruptibleThread("runUninterruptibly") {
       override def run(): Unit = {
         runUninterruptibly {
           enterRunUninterruptibly.countDown()
-          hasInterruptedException = sleep(1000)
+          hasInterruptedException = await(interruptLatch)
         }
         interruptStatusBeforeExit = Thread.interrupted()
       }
     }
     t.start()
-    assert(enterRunUninterruptibly.await(10, TimeUnit.SECONDS), "await 
timeout")
+    assert(!await(enterRunUninterruptibly), "await interrupted")
     t.interrupt()
+    interruptLatch.countDown()

Review Comment:
   The race condition is not in the thread calling interrpt - but in the thread 
which is getting interrupted.



##########
core/src/test/scala/org/apache/spark/util/UninterruptibleThreadSuite.scala:
##########
@@ -28,50 +28,69 @@ import org.apache.spark.SparkFunSuite
 
 class UninterruptibleThreadSuite extends SparkFunSuite {
 
-  /** Sleep millis and return true if it's interrupted */
+  /* Sleep millis and return true if it's interrupted */
   private def sleep(millis: Long): Boolean = {
     try {
       Thread.sleep(millis)
       false
     } catch {
-      case _: InterruptedException =>
+      case e: InterruptedException =>
+        log.error("Thread interrupted during sleep", e)
+        true
+    }
+  }
+
+  /* Await latch and return true if it's interrupted */
+  private def await(
+      latch: CountDownLatch,
+      timeout: Long = 10,
+      timeUnit: TimeUnit = TimeUnit.SECONDS): Boolean = {
+    try {
+      if (!latch.await(timeout, timeUnit)) {
+        log.error("timeout while waiting for the latch")
+        fail("timeout while waiting for the latch")
+      }
+      false
+    } catch {
+      case e: InterruptedException =>
+        log.error("Thread interrupted during await", e)
         true
     }
   }
 
   test("interrupt when runUninterruptibly is running") {
     val enterRunUninterruptibly = new CountDownLatch(1)
+    val interruptLatch = new CountDownLatch(1)
     @volatile var hasInterruptedException = false
     @volatile var interruptStatusBeforeExit = false
-    val t = new UninterruptibleThread("test") {
+    val t = new UninterruptibleThread("runUninterruptibly") {
       override def run(): Unit = {
         runUninterruptibly {
           enterRunUninterruptibly.countDown()
-          hasInterruptedException = sleep(1000)
+          hasInterruptedException = await(interruptLatch)
         }
         interruptStatusBeforeExit = Thread.interrupted()
       }
     }
     t.start()
-    assert(enterRunUninterruptibly.await(10, TimeUnit.SECONDS), "await 
timeout")
+    assert(!await(enterRunUninterruptibly), "await interrupted")
     t.interrupt()
+    interruptLatch.countDown()

Review Comment:
   The race condition is not in the thread calling interrupt - but in the 
thread which is getting interrupted.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to