Skip to content
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

[JENKINS-72796] stable context classloader for Computer.threadPoolForRemoting #9012

Merged
merged 2 commits into from
Mar 6, 2024
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
5 changes: 4 additions & 1 deletion core/src/main/java/hudson/model/Computer.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import hudson.slaves.RetentionStrategy;
import hudson.slaves.WorkspaceList;
import hudson.triggers.SafeTimerTask;
import hudson.util.ClassLoaderSanityThreadFactory;
import hudson.util.DaemonThreadFactory;
import hudson.util.EditDistance;
import hudson.util.ExceptionCatchingThreadFactory;
Expand Down Expand Up @@ -1381,7 +1382,9 @@ public String call() throws IOException {
Executors.newCachedThreadPool(
new ExceptionCatchingThreadFactory(
new NamingThreadFactory(
new DaemonThreadFactory(), "Computer.threadPoolForRemoting")))), ACL.SYSTEM2));
new ClassLoaderSanityThreadFactory(new DaemonThreadFactory()),
"Computer.threadPoolForRemoting")))),
ACL.SYSTEM2));

//
//
Expand Down
40 changes: 40 additions & 0 deletions core/src/test/java/hudson/model/ComputerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import hudson.FilePath;
import hudson.security.ACL;
import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import jenkins.model.Jenkins;
import jenkins.util.SetContextClassLoader;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.springframework.security.core.Authentication;
Expand Down Expand Up @@ -45,4 +47,42 @@ public void testThreadPoolForRemotingActsAsSystemUser() throws InterruptedExcept
Future<Authentication> job = Computer.threadPoolForRemoting.submit(Jenkins::getAuthentication2);
assertThat(job.get(), is(ACL.SYSTEM2));
}

@Issue("JENKINS-72796")
@Test
public void testThreadPoolForRemotingContextClassLoaderIsSet() throws Exception {
// as the threadpool is cached, any other tests here pollute this test so we need enough threads to
// avoid any cached.
final int numThreads = 5;

// simulate the first call to Computer.threadPoolForRemoting with a non default classloader
try (var ignored = new SetContextClassLoader(new ClassLoader() {})) {
obtainAndCheckThreadsContextClassloaderAreCorrect(numThreads);
}
// now repeat this as the checking that the pollution of the context classloader is handled
obtainAndCheckThreadsContextClassloaderAreCorrect(numThreads);
}

private static void obtainAndCheckThreadsContextClassloaderAreCorrect(int numThreads) throws Exception {
ArrayList<Future<ClassLoader>> classloaderFuturesList = new ArrayList<>();
// block all calls to getContextClassloader() so we create more threads.
synchronized (WaitAndGetContextClassLoader.class) {
for (int i = 0; i < numThreads; i++) {
classloaderFuturesList.add(Computer.threadPoolForRemoting.submit(WaitAndGetContextClassLoader::getContextClassloader));
}
}
for (Future<ClassLoader> fc : classloaderFuturesList) {
assertThat(fc.get(), is(Jenkins.class.getClassLoader()));
}
}

private static class WaitAndGetContextClassLoader {

public static synchronized ClassLoader getContextClassloader() throws InterruptedException {
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
// intentionally pollute the Threads context classloader
Thread.currentThread().setContextClassLoader(new ClassLoader() {});
return ccl;
}
}
}
Loading