RFR: In rare cases, the unlock() on join() might fail, causing an IllegalMonitorStateException

kabutz github.com+332398+kabutz at openjdk.java.net
Thu Feb 25 11:15:58 UTC 2021


Unlike synchronized/wait, the Condition.await() method does not necessarily reacquire the lock on exit, for example if the thread is stopped or if CTRL+C is caused in jshell. In that case we would enter the finally block without the lock held and unlocking would cause an IllegalMonitorStateException.

For example, run the following code from jshell and then press CTRL+C:

Object monitor = new Object();
for (int i = 0; i < 10_000; i++) {
  Thread.startVirtualThread(() -> {
    synchronized (monitor) {
      try {
        monitor.wait();
      } catch (InterruptedException ignore) {}
    }
  });
}
Thread.startVirtualThread(() -> System.out.println("done")).join();

Output is:

|  Exception java.lang.IllegalMonitorStateException
|        at ReentrantLock$Sync.tryRelease (ReentrantLock.java:175)
|        at AbstractQueuedSynchronizer.release (AbstractQueuedSynchronizer.java:1007)
|        at ReentrantLock.unlock (ReentrantLock.java:494)
|        at VirtualThread.joinNanos (VirtualThread.java:635)
|        at Thread.join (Thread.java:2281)
|        at Thread.join (Thread.java:2366)
|        at (#3:1)

-------------

Commit messages:
 - In rare cases, the unlock() on join() might fail, causing an IllegalMonitorStateException

Changes: https://git.openjdk.java.net/loom/pull/32/files
 Webrev: https://webrevs.openjdk.java.net/?repo=loom&pr=32&range=00
  Stats: 7 lines in 1 file changed: 6 ins; 0 del; 1 mod
  Patch: https://git.openjdk.java.net/loom/pull/32.diff
  Fetch: git fetch https://git.openjdk.java.net/loom pull/32/head:pull/32

PR: https://git.openjdk.java.net/loom/pull/32


More information about the loom-dev mailing list