Class NestedMonitorLockoutDetector
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 callswait() 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.
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionanalyze()Analyze for nested monitor lockout incidents.voidEnds the round's monitors.voidrecordBlockingOperationAttempted(String operation) Record that the current thread is about to perform a blocking operation.voidrecordMonitorAcquired(Object monitor) Record that the current thread acquired a monitor (entered asynchronizedblock).voidrecordMonitorReleased(Object monitor) Record that the current thread released a monitor (exited asynchronizedblock).voidrecordWaitAttempted(Object waitedOn) Record that the current thread is about to callwaitedOn.wait().
-
Constructor Details
-
NestedMonitorLockoutDetector
public NestedMonitorLockoutDetector()
-
-
Method Details
-
markInvocationStart
public void markInvocationStart()Ends the round's monitors. A body that throws betweenrecordMonitorAcquired(java.lang.Object)andrecordMonitorReleased(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
Record that the current thread acquired a monitor (entered asynchronizedblock).- Parameters:
monitor- the object whose monitor was acquired (null-safe)
-
recordMonitorReleased
Record that the current thread released a monitor (exited asynchronizedblock).- Parameters:
monitor- the object whose monitor was released (null-safe)
-
recordBlockingOperationAttempted
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 towait((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 canonicalsynchronized (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
Record that the current thread is about to callwaitedOn.wait().wait()releases the monitor ofwaitedOnand 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 whosewait()is called (null-safe)- Since:
- 1.12.3
-
analyze
Analyze for nested monitor lockout incidents.- Returns:
- report of all detected incidents
-