Class NestedMonitorLockoutDetector

java.lang.Object
se.deversity.asynctest.diagnostics.NestedMonitorLockoutDetector

public class NestedMonitorLockoutDetector extends Object
Detects the nested monitor lockout anti-pattern: performing a blocking operation (e.g. Object.wait(), Future.get(), Lock.lock()) while holding a monitor lock on a different object.

Why this causes deadlock

When thread A holds monitor M1 and calls wait() on M2, it releases M2 but keeps M1 held. If the thread that eventually calls notifyAll() on M2 also needs to acquire M1 first, both threads will wait forever — a deadlock that is impossible to detect from a thread dump because neither thread shows as "waiting to acquire" M1.

Even without a true deadlock, holding a coarse-grained monitor while blocking on a fine-grained one severely degrades throughput.

Usage inside @AsyncTest:


 @AsyncTest(threads = 4, includes = DetectorType.NESTED_MONITOR_LOCKOUT)
 void testNestedLock() {
     NestedMonitorLockoutDetector mon = AsyncTestContext.nestedMonitorLockoutDetector();
     synchronized (lockA) {
         mon.recordMonitorAcquired(lockA);
         // --- BUG: blocking on a Future while holding lockA ---
         mon.recordBlockingOperationAttempted("future.get()");
         result = future.get();
         mon.recordMonitorReleased(lockA);
     }
 }
 

A guarded wait on the monitor that is held is not this pattern: wait() releases the monitor it is called on. Record it with recordWaitAttempted(Object), which reports only when another monitor stays held.

  • Constructor Details

    • NestedMonitorLockoutDetector

      public NestedMonitorLockoutDetector()
  • Method Details

    • markInvocationStart

      public void markInvocationStart()
      Ends the round's monitors. A body that throws between recordMonitorAcquired(java.lang.Object) and recordMonitorReleased(java.lang.Object) leaves the monitor recorded, and a pooled platform worker reused in a later round would be reported as holding it (#964). The runner calls this once the previous round's workers have finished.
      Since:
      1.13.1
    • recordMonitorAcquired

      public void recordMonitorAcquired(Object monitor)
      Record that the current thread acquired a monitor (entered a synchronized block).
      Parameters:
      monitor - the object whose monitor was acquired (null-safe)
    • recordMonitorReleased

      public void recordMonitorReleased(Object monitor)
      Record that the current thread released a monitor (exited a synchronized block).
      Parameters:
      monitor - the object whose monitor was released (null-safe)
    • recordBlockingOperationAttempted

      public void recordBlockingOperationAttempted(String operation)
      Record that the current thread is about to perform a blocking operation. If the thread currently holds one or more monitors, a lockout risk is recorded.

      An Object.wait() is the exception, recognised by an operation named like a call to wait( (so "wait()" and "obj.wait(100)", not "await()"). A thread can only wait on a monitor it holds, and waiting releases that one, so a wait is a lockout only when some other monitor stays held: the canonical synchronized (m) { while (!ready) m.wait(); } holds one monitor and is silent. Which monitor is waited on cannot be read from a string, so the recorded monitors other than one are counted; recordWaitAttempted(Object) names it and is exact.

      Parameters:
      operation - human-readable name of the blocking operation (e.g. "future.get()")
    • recordWaitAttempted

      public void recordWaitAttempted(Object waitedOn)
      Record that the current thread is about to call waitedOn.wait().

      wait() releases the monitor of waitedOn and only that one, so this records a lockout risk only when the thread holds some other recorded monitor while it waits.

      Parameters:
      waitedOn - the object whose wait() is called (null-safe)
      Since:
      1.12.3
    • analyze

      Analyze for nested monitor lockout incidents.
      Returns:
      report of all detected incidents