Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8339584

java.util.concurrent.locks.Condition.await() does not throw InterruptedException even when interrupted

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • tbd
    • 8, 24
    • core-libs
    • None

      The javadoc of Condition.await() states:

      > If the current thread:
      > - has its interrupted status set on entry to this method; or
      > - is interrupted while waiting and interruption of thread suspension is supported,
      > then InterruptedException is thrown and the current thread's interrupted status is cleared. It is not specified, in the first case, whether or not the test for interruption occurs before the lock is released.

      Experiments with a trivial reproducer however show that if there are 2 concurrent threads involved, one which does a Thread.interrupt() on the thread that is waiting (through Condition.await()) and one which does a Condition.signal(), then even if the waiting thread is interrupted first (and then signalled next), the InterruptedException doesn't get thrown from the waiting Condition.await() call. Instead, the Condition.await() call returns successfully with the Thread's interrupt flag set (Thread.currentThread().isInterrupted() returns true after the call returns from Condition.await()).

      A reproducer is attached to this issue, which reproduces the behaviour where the InterruptedException doesn't get thrown. The reproducer reproduces this issue against the current mainline as well as older releases, all the way back to Java 8.

      The reproducer was run on a local macosx M1 instance and you should see the output similar to:

          iteration 1 - interrupted first, then signalled
          Exception in thread "main" java.lang.AssertionError: failed on iteration 1 - Thread has interrupt bit set but no InterruptedException was thrown
              at InterruptTest.runTest(InterruptTest.java:34)
              at InterruptTest.main(InterruptTest.java:10)

            vklang Viktor Klang
            jpai Jaikiran Pai
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: