eclipse-openj9 / openj9

Eclipse OpenJ9: A Java Virtual Machine for OpenJDK that's optimized for small footprint, fast start-up, and high throughput. Builds on Eclipse OMR (https://github.com/eclipse/omr) and combines with the Extensions for OpenJDK for OpenJ9 repo.
Other
3.28k stars 722 forks source link

Investigate VirtualThread wait(), wait(timeout), sleep() behaviour #19039

Open babsingh opened 8 months ago

babsingh commented 8 months ago

In JDK22, a new test, GetThreadStateMountedTest, was added to verify the behaviour of interrupting a virtual thread while it is suspended using JVMTI.

The behaviour between a thread suspended by JVMTI and it receiving an interrupt is not properly defined in the Java doc and JVMTI spec.

The goal of this issue is to verify the below [Thread.interrupt](https://download.java.net/java/early_access/jdk22/docs/api/java.base/java/lang/Thread.html#interrupt()) behaviour for a thread suspended by JVMTI:

If this thread is blocked in an invocation of the [wait()](https://download.java.net/java/early_access/jdk22/docs/api/java.base/java/lang/Object.html#wait()), wait(long), or wait(long, int) methods of the Object class, or of the [join()](https://download.java.net/java/early_access/jdk22/docs/api/java.base/java/lang/Thread.html#join()), join(long), join(long, int), sleep(long), or sleep(long, int) methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

If a thread is suspended while executing wait()/wait(timeout)/sleep() and it receives an interrupt, should the thread throw an InterruptedException after resuming?

After running GetThreadStateMountedTest using the JDK22 RI, it was found that the RI never throws an InterruptedException for waiting(timeout) and sleeping. It intermittently throws an InterruptedException for waiting indefinitely (without a timeout), but most of the time it didn't throw an InterruptedException even for this case. The test is missing asserts to validate InterruptedException because the RI doesn't guarantee to throw an InterruptedException.

Based upon the above RI behaviour, throwing an InterruptedException is neither expected nor guaranteed.

tajila commented 5 months ago

@babsingh any updates on this?

babsingh commented 5 months ago

I reran the test with OpenJ9 and RI to re-evaluate the issue. I ran the test a few times and neither JDK threw an InterruptedException.

OpenJ9 matches the RI in terms of behaviour. This issue shouldn't block the 0.46 release.

The behaviour between a thread suspended by JVMTI and it receiving an interrupt is not properly defined in the Java doc and JVMTI spec.

For clarification on spec, I will contact the spec SMEs on the RI side.

tajila commented 2 months ago

@babsingh Whats the latest on this?

babsingh commented 2 months ago

Whats the latest on this?

@tajila The spec SME didn't respond to our initial email, so I have sent another email to the SME requesting clarification.

In the meantime, OpenJ9 matches the RI in terms of behavior, so this issue shouldn't block the 0.48 release. Moving this issue to the next release.