Remove remaining context class loader mentions
Continuation of
MODIFY
gradle-plugins/src/main/kotlin/io.opentelemetry.instrumentation.muzzle-check.gradle.kts
gradle-plugins/src/main/kotlin/io.opentelemetry.instrumentation.muzzle-check.gradle.kts
@@ -130,11 +130,9 @@ tasks.register("printMuzzleReferences") {
dependsOn(shadowModule)
doLast {
val instrumentationCL = createInstrumentationClassloader()
- withContextClassLoader(instrumentationCL) {
MuzzleGradlePluginUtil.printMuzzleReferences(instrumentationCL)
}
}
-}
val hasRelevantTask = gradle.startParameter.taskNames.any {
// removing leading ':' if present
@@ -293,20 +291,9 @@ fun addMuzzleTask(muzzleDirective: MuzzleDirective, versionArtifact: Artifact?,
doLast {
val instrumentationCL = createInstrumentationClassloader()
val userCL = createClassLoaderForTask(config)
- withContextClassLoader(instrumentationCL) {
MuzzleGradlePluginUtil.assertInstrumentationMuzzled(instrumentationCL, userCL,
muzzleDirective.excludedInstrumentationNames.get(), muzzleDirective.assertPass.get())
}
-
- for (thread in Thread.getAllStackTraces().keys) {
- if (thread.contextClassLoader === instrumentationCL
- || thread.contextClassLoader === userCL) {
- throw GradleException(
- "Task ${taskName} has spawned a thread: ${thread} with class loader ${thread.contextClassLoader}. " +
- "This will prevent GC of dynamic muzzle classes. Aborting muzzle run.")
- }
- }
- }
}
runAfter.configure { finalizedBy(muzzleTask) }
@@ -414,13 +401,3 @@ fun muzzleDirectiveToArtifacts(muzzleDirective: MuzzleDirective, system: Reposit
yieldAll(allVersionArtifacts)
}
-
-fun withContextClassLoader(classLoader: ClassLoader, action: () -> Unit) {
- val currentClassLoader = Thread.currentThread().contextClassLoader
- Thread.currentThread().contextClassLoader = classLoader
- try {
- action()
- } finally {
- Thread.currentThread().contextClassLoader = currentClassLoader
- }
-}

MODIFY
javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/bytebuddy/ExceptionHandlers.java
javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/bytebuddy/ExceptionHandlers.java
@@ -32,7 +32,6 @@ public final class ExceptionHandlers {
@Override
public StackManipulation.Size apply(MethodVisitor mv, Implementation.Context context) {
String name = context.getInstrumentedType().getName();
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// writes the following bytecode:
// try {
@@ -51,11 +50,7 @@ public final class ExceptionHandlers {
// stack: (top) throwable
mv.visitLabel(logStart);
- mv.visitLdcInsn(
- "Failed to handle exception in instrumentation for "
- + name
- + " on "
- + classLoader);
+ mv.visitLdcInsn("Failed to handle exception in instrumentation for " + name);
mv.visitInsn(Opcodes.SWAP); // stack: (top) throwable,string
mv.visitMethodInsn(

MODIFY
muzzle/src/main/java/io/opentelemetry/javaagent/tooling/muzzle/ClassLoaderMatcher.java
muzzle/src/main/java/io/opentelemetry/javaagent/tooling/muzzle/ClassLoaderMatcher.java
@@ -91,7 +91,7 @@ public class ClassLoaderMatcher {
instrumentationModule.instrumentationName(),
allHelperClasses,
helperResourceBuilder.getResources(),
- Thread.currentThread().getContextClassLoader(),
+ ClassLoaderMatcher.class.getClassLoader(),
null)
.transform(null, null, classLoader, null, null);
}

MODIFY
smoke-tests/src/test/java/io/opentelemetry/smoketest/windows/WindowsTestContainerManager.java
smoke-tests/src/test/java/io/opentelemetry/smoketest/windows/WindowsTestContainerManager.java
@@ -20,6 +20,7 @@ import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import io.opentelemetry.smoketest.AbstractTestContainerManager;
import io.opentelemetry.smoketest.ResourceMapping;
import io.opentelemetry.smoketest.TargetWaitStrategy;
+import io.opentelemetry.smoketest.TestContainerManager;
import io.opentelemetry.testing.internal.armeria.client.WebClient;
import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse;
import java.io.ByteArrayInputStream;
@@ -216,7 +217,7 @@ public class WindowsTestContainerManager extends AbstractTestContainerManager {
private void copyResourceToContainer(
String containerId, String resourcePath, String containerPath) throws IOException {
try (InputStream is =
- Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath)) {
+ TestContainerManager.class.getClassLoader().getResourceAsStream(resourcePath)) {
copyFileToContainer(containerId, IOUtils.toByteArray(is), containerPath);
}
}

Remove remaining context class loader mentions (#7419)

Continuation of #7391