From b4ee6b6b70d4c9ce5e752623281a5d2378d9d6a0 Mon Sep 17 00:00:00 2001 From: Gengchen Tuo Date: Thu, 11 Jul 2024 15:35:46 -0400 Subject: [PATCH] Destroy virtual thread data after VirtualThreadEnd callback Currently destroyThreadData() is hooked onto the VirtualThreadEnd event which is called before the event callback. In JDWP, the agent uses TLS to associate nodes in the running thread list. Clearing TLS before the event callback makes the agent lose track of the node thus failed to remove existed virtual threads from the list. This commit removes jvmtiHookVirtualThreadDestroy() and calls destroyThreadData() directly inside jvmtiHookVirtualThreadEnd(). Fixes: https://github.com/ibmruntimes/Semeru-Runtimes/issues/80 https://github.com/eclipse-openj9/openj9/issues/19759 Signed-off-by: Gengchen Tuo --- runtime/j9vm/javanextvmi.cpp | 1 + runtime/jvmti/jvmtiHook.c | 26 +------------------------- 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/runtime/j9vm/javanextvmi.cpp b/runtime/j9vm/javanextvmi.cpp index 50e7808dde1..fbc82499161 100644 --- a/runtime/j9vm/javanextvmi.cpp +++ b/runtime/j9vm/javanextvmi.cpp @@ -722,6 +722,7 @@ JVM_VirtualThreadEnd(JNIEnv *env, jobject vthread) vmFuncs->internalEnterVMFromJNI(currentThread); TRIGGER_J9HOOK_VM_VIRTUAL_THREAD_END(vm->hookInterface, currentThread); + setContinuationStateToLastUnmount((J9VMThread *)env, vthread); virtualThreadUnmountBegin(env, vthread); diff --git a/runtime/jvmti/jvmtiHook.c b/runtime/jvmti/jvmtiHook.c index fa52875bcb1..515ec972a96 100644 --- a/runtime/jvmti/jvmtiHook.c +++ b/runtime/jvmti/jvmtiHook.c @@ -147,7 +147,6 @@ static UDATA methodExists(J9Class * methodClass, U_8 * nameData, UDATA nameLengt #if JAVA_SPEC_VERSION >= 19 static void jvmtiHookVirtualThreadStarted (J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData); static void jvmtiHookVirtualThreadEnd (J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData); -static void jvmtiHookVirtualThreadDestroy (J9HookInterface **hook, UDATA eventNum, void *eventData, void *userData); static void jvmtiHookVirtualThreadMount (J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData); static void jvmtiHookVirtualThreadUnmount (J9HookInterface** hook, UDATA eventNum, void* eventData, void* userData); #endif /* JAVA_SPEC_VERSION >= 19 */ @@ -412,25 +411,11 @@ jvmtiHookVirtualThreadEnd(J9HookInterface **hook, UDATA eventNum, void *eventDat } } - TRACE_JVMTI_EVENT_RETURN(jvmtiHookVirtualThreadEnd); -} - -static void -jvmtiHookVirtualThreadDestroy(J9HookInterface **hook, UDATA eventNum, void *eventData, void *userData) -{ - J9VirtualThreadEndEvent *data = eventData; - J9JVMTIEnv *j9env = userData; - J9VMThread *currentThread = data->currentThread; - - Trc_JVMTI_jvmtiHookVirtualThreadDestroy_Entry(); - - /* The final thread in the system is destroyed very late, JVMTI could be shut down by then. */ - if (NULL != currentThread->javaVM->jvmtiData) { destroyThreadData(j9env, currentThread); } - TRACE_JVMTI_EVENT_RETURN(jvmtiHookVirtualThreadDestroy); + TRACE_JVMTI_EVENT_RETURN(jvmtiHookVirtualThreadEnd); } /** @@ -967,9 +952,6 @@ unhookAllEvents(J9JVMTIEnv * j9env) } hookUnregister(vmHook, J9HOOK_VM_THREAD_DESTROY, jvmtiHookThreadDestroy, NULL, j9env); -#if JAVA_SPEC_VERSION >= 19 - hookUnregister(vmHook, J9HOOK_VM_VIRTUAL_THREAD_END, jvmtiHookVirtualThreadDestroy, NULL, j9env); -#endif /* JAVA_SPEC_VERSION >= 19 */ hookUnregister(vmHook, J9HOOK_VM_POP_FRAMES_INTERRUPT, jvmtiHookPopFramesInterrupt, NULL, j9env); hookUnregister(gcOmrHook, J9HOOK_MM_OMR_GLOBAL_GC_END, jvmtiHookGCEnd, NULL, j9env); @@ -986,12 +968,6 @@ hookRequiredEvents(J9JVMTIEnv * j9env) return 1; } -#if JAVA_SPEC_VERSION >= 19 - if (hookRegister(vmHook, J9HOOK_VM_VIRTUAL_THREAD_END, jvmtiHookVirtualThreadDestroy, OMR_GET_CALLSITE(), j9env)) { - return 1; - } -#endif /* JAVA_SPEC_VERSION >= 19 */ - return 0; }