On Wed, 10 Dec 2025 06:00:47 GMT, Serguei Spitsyn <[email protected]> wrote:
> This fixes the test which is unreliable and fails intermittently from time to
> time.
> The test was failing in the method `checkReentrantLock()` when comparing the
> expected state with result from `vt.getState()`. The issue is that the call
> to `ThreadListStackTracesTest.reentrantLock.lock()` in a
> `ReentrantLockTestTask` can reach waiting state on some class loading but not
> on the ReentrantLock. Please see the first comment for test sources details.
> The fix is to add a big enough sleep before call to the checkStates().
>
> Testing:
> - submitted about thousands of mach5 runs of the `ThreadListStackTracesTest`
> test runs before and after the fix
> - TBD: submit mach5 tiers 1-3 to be completely safe
The most relevant test fragments are below:
private static void checkReentrantLock() throws InterruptedException {
final Thread.State expState = Thread.State.WAITING;
reentrantLock.lock();
String name = "ReentrantLockTestTask";
TestTask task = new ReentrantLockTestTask();
Thread vt = Thread.ofVirtual().name(name).start(task);
task.ensureReady(vt, expState);
checkStates(vt, expState); <== intermittently fails inside this call !!!
}
private static void checkStates(Thread vt, Thread.State expState) {
int singleState = getStateSingle(vt);
int multiState = getStateMultiple(vt, Thread.currentThread());
int jvmtiExpState = (expState == Thread.State.WAITING) ?
JVMTI_THREAD_STATE_WAITING :
JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER;
System.out.printf("State: expected: %s single: %x multi: %x\n",
vt.getState(), singleState, multiState); <== printed
state values !!!
if (vt.getState() != expState) { <== this comparison failed!!!
failed("Java thread state is wrong"); <== Exception is thrown
here!!!
}
. . .
class ReentrantLockTestTask extends TestTask {
public void run() {
log("grabbing reentrantLock");
threadReady = true;
ThreadListStackTracesTest.reentrantLock.lock(); <== May have waiting
state in some class loading!
log("grabbed reentrantLock");
}
}
-------------
PR Comment: https://git.openjdk.org/jdk/pull/28734#issuecomment-3635563778