Skip to content

Commit

Permalink
Issue #5859 Make safe creation of threads a utility class.
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Bartel <janb@webtide.com>
  • Loading branch information
janbartel committed Jan 20, 2021
1 parent 7f270a0 commit 4a93a00
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 27 deletions.
Expand Up @@ -687,19 +687,15 @@ private boolean addCounts(int deltaThreads, int deltaIdle)
@Override
public Thread newThread(Runnable runnable)
{
return (AccessController.doPrivileged(new PrivilegedAction<Thread>()
return ThreadCreator.create(() ->
{
@Override
public Thread run()
{
Thread thread = new Thread(_threadGroup, runnable);
thread.setDaemon(isDaemon());
thread.setPriority(getThreadsPriority());
thread.setName(_name + "-" + thread.getId());
thread.setContextClassLoader(this.getClass().getClassLoader());
return thread;
}
}));
Thread thread = new Thread(_threadGroup, runnable);
thread.setDaemon(isDaemon());
thread.setPriority(getThreadsPriority());
thread.setName(_name + "-" + thread.getId());
thread.setContextClassLoader(this.getClass().getClassLoader());
return thread;
});
}

protected void removeThread(Thread thread)
Expand Down
Expand Up @@ -38,15 +38,10 @@
public class ShutdownThread extends Thread
{
private static final Logger LOG = Log.getLogger(ShutdownThread.class);
private static final ShutdownThread _thread = AccessController.doPrivileged(new PrivilegedAction<ShutdownThread>()
{
@Override
public ShutdownThread run()
{
return new ShutdownThread();
}

});
private static final ShutdownThread _thread = ThreadCreator.create(() ->
{
return new ShutdownThread();
});

private boolean _hooked;
private final List<LifeCycle> _lifeCycles = new CopyOnWriteArrayList<LifeCycle>();
Expand Down
@@ -0,0 +1,53 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//

package org.eclipse.jetty.util.thread;

import java.security.AccessController;
import java.security.PrivilegedAction;

/**
* ThreadCreator
*
* Convenience class to ensure that a new Thread is created
* inside a privileged block. This prevents the Thread constructor
* from pinning the caller's context classloader. This happens
* when the Thread constructor takes a snapshot of the current
* calling context - which contains ProtectionDomains that may
* reference the context classloader - and remembers it for the
* lifetime of the Thread.
*/
class ThreadCreator
{
interface Factory<T extends Thread>
{
T newThread();
}

static <T extends Thread> T create(Factory<T> maker)
{
return AccessController.doPrivileged(new PrivilegedAction<T>()
{
@Override
public T run()
{
return maker.newThread();
}
});
}
}
Expand Up @@ -840,10 +840,6 @@ public void testDump() throws Exception
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
Expand All @@ -859,8 +855,6 @@ public void testContextClassLoader() throws Exception

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

t.start();
}
}

Expand Down

0 comments on commit 4a93a00

Please sign in to comment.