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

Jetty 9.4.x 5859 classloader leak queuedthreadpool #5894

Merged
Merged
Expand Up @@ -689,6 +689,7 @@ public Thread newThread(Runnable runnable)
thread.setDaemon(isDaemon());
thread.setPriority(getThreadsPriority());
thread.setName(_name + "-" + thread.getId());
thread.setContextClassLoader(this.getClass().getClassLoader());
return thread;
}

Expand Down
Expand Up @@ -19,6 +19,8 @@
package org.eclipse.jetty.util.thread;

import java.io.Closeable;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -27,6 +29,7 @@
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.log.StacklessLogging;
import org.eclipse.jetty.util.thread.ThreadPool.SizedThreadPool;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -833,6 +836,34 @@ public void testDump() throws Exception
assertThat(count(dump, "QueuedThreadPoolTest.lambda$testDump$"), is(1));
}

@Test
public void testContextClassLoader() throws Exception
{
QueuedThreadPool tp = new QueuedThreadPool();
tp.setMinThreads(1);
tp.setMaxThreads(3);
tp.setIdleTimeout(1000);
tp.setThreadsPriority(Thread.NORM_PRIORITY - 1);
try (StacklessLogging stackless = new StacklessLogging(QueuedThreadPool.class))
{
//change the current thread's classloader to something else
Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[] {}));

//create a new thread
Thread t = tp.newThread(() ->
{
//the executing thread should be still set to the classloader of the QueuedThreadPool,
//not that of the thread that created this thread.
assertThat(Thread.currentThread().getContextClassLoader(), Matchers.equalTo(QueuedThreadPool.class.getClassLoader()));
});

//new thread should be set to the classloader of the QueuedThreadPool
assertThat(t.getContextClassLoader(), Matchers.equalTo(QueuedThreadPool.class.getClassLoader()));

t.start();
}
}
janbartel marked this conversation as resolved.
Show resolved Hide resolved

private int count(String s, String p)
{
int c = 0;
Expand Down