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

Dr Heinz M. Kabutz heinz at javaspecialists.eu
Thu Feb 25 11:26:37 UTC 2021


The code in parkOnCarrierThread() might also need to be considered if 
someone is crazy enough to call Thread.stop() on a carrier thread. That 
would mean that the unlock would fail and then the state of the virtual 
thread would remain pinned. I didn't change that code as I'm not sure 
whether that is something that would happen "in the wild". However, 
join() would be called by an outside thread and so there it can happen 
that we abruptly terminate the await().

Regards

Heinz
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java™ Specialists' Newsletter" - www.javaspecialists.eu
Java Champion - www.javachampions.org
JavaOne Rock Star Speaker
Tel: +30 69 75 595 262
Skype: kabutz

On 2021/02/25 13:15, kabutz wrote:
> 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