Skip to content

Commit

Permalink
Remove remaining context class loader mentions (#7419)
Browse files Browse the repository at this point in the history
Continuation of #7391
  • Loading branch information
Mateusz Rzeszutek committed Dec 14, 2022
1 parent 5efa640 commit 59f00df
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ tasks.register("printMuzzleReferences") {
dependsOn(shadowModule)
doLast {
val instrumentationCL = createInstrumentationClassloader()
withContextClassLoader(instrumentationCL) {
MuzzleGradlePluginUtil.printMuzzleReferences(instrumentationCL)
}
MuzzleGradlePluginUtil.printMuzzleReferences(instrumentationCL)
}
}

Expand Down Expand Up @@ -293,19 +291,8 @@ 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.")
}
}
MuzzleGradlePluginUtil.assertInstrumentationMuzzled(instrumentationCL, userCL,
muzzleDirective.excludedInstrumentationNames.get(), muzzleDirective.assertPass.get())
}
}

Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public boolean isValid() {
@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 {
Expand All @@ -51,11 +50,7 @@ public StackManipulation.Size apply(MethodVisitor mv, Implementation.Context con

// 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private static List<Mismatch> checkHelperInjection(
instrumentationModule.instrumentationName(),
allHelperClasses,
helperResourceBuilder.getResources(),
Thread.currentThread().getContextClassLoader(),
ClassLoaderMatcher.class.getClassLoader(),
null)
.transform(null, null, classLoader, null, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -216,7 +217,7 @@ private boolean imageExists(String imageName) {
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);
}
}
Expand Down

0 comments on commit 59f00df

Please sign in to comment.