Skip to content

Commit d223f79

Browse files
committed
fix(virtual-thread): Add support for early VirtualThread afterTerminate method
Support both afterDone() and afterTerminate() cleanup methods to handle different JDK 21 virtual thread implementations. Both methods cancel the help continuation and release the context scope when the virtual thread completes.
1 parent c521b50 commit d223f79

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

dd-java-agent/instrumentation/java/java-lang/java-lang-21.0/src/main/java/datadog/trace/instrumentation/java/lang/jdk21/VirtualThreadInstrumentation.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
* <li>{@code unmount()}: swaps the carrier thread's original context back, saving the virtual
4646
* thread's (possibly modified) context for the next mount.
4747
* <li>Steps 2-3 repeat on each park/unpark cycle, potentially on different carrier threads.
48-
* <li>{@code afterDone()}: cancels the help continuation, releasing the context scope to be
49-
* closed.
48+
* <li>{@code afterDone()} / {@code afterTerminate()} for early VirtualThread support: cancels the
49+
* help continuation, releasing the context scope to be closed.
5050
* </ol>
5151
*
5252
* <p>Instrumenting the internal {@code VirtualThread.runContinuation()} method does not work as the
@@ -111,6 +111,9 @@ public void methodAdvice(MethodTransformer transformer) {
111111
transformer.applyAdvice(
112112
isMethod().and(named("afterDone")).and(takesArguments(boolean.class)),
113113
getClass().getName() + "$AfterDone");
114+
transformer.applyAdvice(
115+
isMethod().and(named("afterTerminate")).and(takesArguments(boolean.class, boolean.class)),
116+
getClass().getName() + "$AfterDone");
114117
}
115118

116119
public static final class Construct {
@@ -153,7 +156,7 @@ public static void onUnmount(@Advice.This Object virtualThread) {
153156

154157
public static final class AfterDone {
155158
@OnMethodEnter(suppress = Throwable.class)
156-
public static void onTerminate(@Advice.This Object virtualThread) {
159+
public static void onDone(@Advice.This Object virtualThread) {
157160
ContextStore<Object, VirtualThreadState> store =
158161
InstrumentationContext.get(VIRTUAL_THREAD_CLASS_NAME, VIRTUAL_THREAD_STATE_CLASS_NAME);
159162
VirtualThreadState state = store.remove(virtualThread);

0 commit comments

Comments
 (0)