Skip to content

Fix akka shutdown hanging #13073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import io.opentelemetry.context.Context;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
Expand Down Expand Up @@ -46,8 +44,7 @@ public static class ScheduleAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void enterSchedule(
@Advice.Argument(value = 2, readOnly = false) Runnable runnable) {
Context context = Java8BytecodeBridge.currentContext();
runnable = context.wrap(runnable);
runnable = AkkaSchedulerTaskWrapper.wrap(runnable);
}
}

Expand All @@ -57,8 +54,7 @@ public static class ScheduleOnceAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void enterScheduleOnce(
@Advice.Argument(value = 1, readOnly = false) Runnable runnable) {
Context context = Java8BytecodeBridge.currentContext();
runnable = context.wrap(runnable);
runnable = AkkaSchedulerTaskWrapper.wrap(runnable);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.akkaactor;

import io.opentelemetry.context.Context;

public final class AkkaSchedulerTaskWrapper {
private static final Class<?> RUN_ON_CLOSE_TASK_CLASS = getRunOnCloseTaskClass();

private static Class<?> getRunOnCloseTaskClass() {
try {
return Class.forName("akka.actor.Scheduler$TaskRunOnClose");
} catch (ClassNotFoundException e) {
return null;
}
}

private static boolean isRunOnCloseTask(Runnable runnable) {
return RUN_ON_CLOSE_TASK_CLASS != null && RUN_ON_CLOSE_TASK_CLASS.isInstance(runnable);
}

public static Runnable wrap(Runnable runnable) {
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/13066
// Skip wrapping shutdown tasks. Shutdown process hangs when shutdown tasks are wrapped here.
if (isRunOnCloseTask(runnable)) {
return runnable;
}

Context context = Context.current();
return context.wrap(runnable);
}

private AkkaSchedulerTaskWrapper() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
Expand Down Expand Up @@ -46,7 +45,7 @@ public static class ScheduleAdvice {
@Advice.AssignReturned.ToArguments(@ToArgument(2))
@Advice.OnMethodEnter(suppress = Throwable.class)
public static Runnable enterSchedule(@Advice.Argument(value = 2) Runnable runnable) {
return Java8BytecodeBridge.currentContext().wrap(runnable);
return PekkoSchedulerTaskWrapper.wrap(runnable);
}
}

Expand All @@ -56,7 +55,7 @@ public static class ScheduleOnceAdvice {
@Advice.AssignReturned.ToArguments(@ToArgument(1))
@Advice.OnMethodEnter(suppress = Throwable.class)
public static Runnable enterScheduleOnce(@Advice.Argument(value = 1) Runnable runnable) {
return Java8BytecodeBridge.currentContext().wrap(runnable);
return PekkoSchedulerTaskWrapper.wrap(runnable);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.pekkoactor.v1_0;

import io.opentelemetry.context.Context;
import org.apache.pekko.actor.Scheduler;

public final class PekkoSchedulerTaskWrapper {

public static Runnable wrap(Runnable runnable) {
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/13066
// Skip wrapping shutdown tasks. Shutdown process hangs when shutdown tasks are wrapped here.
if (runnable instanceof Scheduler.TaskRunOnClose) {
return runnable;
}

Context context = Context.current();
return context.wrap(runnable);
}

private PekkoSchedulerTaskWrapper() {}
}
Loading