Skip to content

Commit

Permalink
Destroy virtual thread data after VirtualThreadEnd callback
Browse files Browse the repository at this point in the history
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:
ibmruntimes/Semeru-Runtimes#80
eclipse-openj9#19759

Signed-off-by: Gengchen Tuo <[email protected]>
  • Loading branch information
Gengchen Tuo authored and thallium committed Jul 12, 2024
1 parent c895f84 commit b4ee6b6
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 25 deletions.
1 change: 1 addition & 0 deletions runtime/j9vm/javanextvmi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
26 changes: 1 addition & 25 deletions runtime/jvmti/jvmtiHook.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down

0 comments on commit b4ee6b6

Please sign in to comment.