Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix [JENKINS-49206] by ensuring Timer threads get standard classloader (
#3272) * Fix [JENKINS-49206] by ensuring Timer threads get standard classloader * Refactor to better fit in with existing ThreadFactor utils * Fix javadocs
- Loading branch information
1 parent
6cefcf1
commit 2a6fc65
Showing
3 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
core/src/main/java/hudson/util/ClassLoaderSanityThreadFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package hudson.util; | ||
|
||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Explicitly sets the {@link Thread#contextClassLoader} for threads it creates to its own classloader. | ||
* This avoids issues where threads are lazily created (ex by invoking {@link java.util.concurrent.ScheduledExecutorService#schedule(Runnable, long, TimeUnit)}) | ||
* in a context where they would receive a customized {@link Thread#contextClassLoader} that was never meant to be used. | ||
* | ||
* Commonly this is a problem for Groovy use, where this may result in memory leaks. | ||
* @see <a href="https://issues.jenkins-ci.org/browse/JENKINS-49206">JENKINS-49206</a> | ||
*/ | ||
public class ClassLoaderSanityThreadFactory implements ThreadFactory { | ||
private final ThreadFactory delegate; | ||
|
||
public ClassLoaderSanityThreadFactory(ThreadFactory delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override public Thread newThread(Runnable r) { | ||
Thread t = delegate.newThread(r); | ||
t.setContextClassLoader(ClassLoaderSanityThreadFactory.class.getClassLoader()); | ||
return t; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters