Wait() is called while two monitors are held, so the outer one stays locked while waiting

tx0/java:wait-holding-two-locksrecommendedwarningbug

What it finds

wait() releases only the monitor of the object it is called on. Inside a second synchronized, the outer monitor stays locked while the thread waits, and a thread that needs it to send the notification blocks: a deadlock, which CERT's rule LCK09-J describes for blocking operations while holding a lock.

Reported is a call of wait inside two or more synchronized blocks or a synchronized method and a block, in the same body.

From the rule's source, wait_holding_two_locks.rs.

Example from the rule's tests
class Queue {  private final Object a = new Object();  private final Object b = new Object();   void take() throws InterruptedException {    synchronized (a) {      synchronized (b) {        b.wait();tx0 reports: the other stays locked      }    }    synchronized (a) {      a.wait();    }  }   synchronized void poll() throws InterruptedException {    synchronized (b) {      b.wait();tx0 reports: the other stays locked    }  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:wait-holding-two-locks" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:wait-holding-two-locks] reason="..."

tx0 explain tx0/java:wait-holding-two-locks shows the same in the terminal. Found a false positive? Report it.