Skip to content

Commit e801e51

Browse files
author
Serguei Spitsyn
committed
8306324: StopThread results in thread being marked as interrupted, leading to unexpected InterruptedException
Reviewed-by: pchilanomate, alanb
1 parent 7ea08d3 commit e801e51

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

src/hotspot/share/prims/jvm.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,15 +2880,16 @@ JVM_ENTRY(void, JVM_SleepNanos(JNIEnv* env, jclass threadClass, jlong nanos))
28802880
} else {
28812881
ThreadState old_state = thread->osthread()->get_state();
28822882
thread->osthread()->set_state(SLEEPING);
2883-
if (!thread->sleep_nanos(nanos)) { // interrupted
2883+
if (!thread->sleep_nanos(nanos)) { // interrupted or async exception was installed
28842884
// An asynchronous exception could have been thrown on
28852885
// us while we were sleeping. We do not overwrite those.
28862886
if (!HAS_PENDING_EXCEPTION) {
28872887
HOTSPOT_THREAD_SLEEP_END(1);
2888-
2889-
// TODO-FIXME: THROW_MSG returns which means we will not call set_state()
2890-
// to properly restore the thread state. That's likely wrong.
2891-
THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
2888+
if (!thread->has_async_exception_condition()) {
2889+
// TODO-FIXME: THROW_MSG returns which means we will not call set_state()
2890+
// to properly restore the thread state. That's likely wrong.
2891+
THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
2892+
}
28922893
}
28932894
}
28942895
thread->osthread()->set_state(old_state);

src/hotspot/share/runtime/javaThread.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,6 @@ void JavaThread::install_async_exception(AsyncExceptionHandshakeClosure* aehc) {
11461146
oop vt_oop = vthread();
11471147
if (vt_oop == nullptr || !vt_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
11481148
// Interrupt thread so it will wake up from a potential wait()/sleep()/park()
1149-
java_lang_Thread::set_interrupted(threadObj(), true);
11501149
this->interrupt();
11511150
}
11521151
}
@@ -2098,7 +2097,7 @@ bool JavaThread::sleep(jlong millis) {
20982097

20992098
// java.lang.Thread.sleep support
21002099
// Returns true if sleep time elapsed as expected, and false
2101-
// if the thread was interrupted.
2100+
// if the thread was interrupted or async exception was installed.
21022101
bool JavaThread::sleep_nanos(jlong nanos) {
21032102
assert(this == Thread::current(), "thread consistency check");
21042103
assert(nanos >= 0, "nanos are in range");
@@ -2118,6 +2117,9 @@ bool JavaThread::sleep_nanos(jlong nanos) {
21182117
jlong nanos_remaining = nanos;
21192118

21202119
for (;;) {
2120+
if (has_async_exception_condition()) {
2121+
return false;
2122+
}
21212123
// interruption has precedence over timing out
21222124
if (this->is_interrupted(true)) {
21232125
return false;

test/hotspot/jtreg/serviceability/jvmti/vthread/StopThreadTest/StopThreadTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ public void run() {
211211
log("TestTask.run: caught expected AssertionError from method A()");
212212
seenExceptionFromA = true;
213213
}
214-
Thread.interrupted();
215214
if (!seenExceptionFromA && !preemptableVirtualThread()) {
216215
StopThreadTest.setFailed("TestTask.run: expected AssertionError from method A()");
217216
}
@@ -224,7 +223,6 @@ public void run() {
224223
log("TestTask.run: caught expected AssertionError from method B()");
225224
seenExceptionFromB = true;
226225
}
227-
Thread.interrupted();
228226
if (!seenExceptionFromB) {
229227
StopThreadTest.setFailed("TestTask.run: expected AssertionError from method B()");
230228
}
@@ -237,7 +235,6 @@ public void run() {
237235
log("TestTask.run: caught expected AssertionError from method C()");
238236
seenExceptionFromC = true;
239237
}
240-
Thread.interrupted();
241238
if (!seenExceptionFromC) {
242239
StopThreadTest.setFailed("TestTask.run: expected AssertionError from method C()");
243240
}

0 commit comments

Comments
 (0)