diff --git a/core/src/main/java/hudson/model/Computer.java b/core/src/main/java/hudson/model/Computer.java index a187028f73..83c0568f66 100644 --- a/core/src/main/java/hudson/model/Computer.java +++ b/core/src/main/java/hudson/model/Computer.java @@ -16,6 +16,7 @@ import hudson.tasks.Publisher; import hudson.util.DaemonThreadFactory; import hudson.util.RemotingDiagnostics; import hudson.util.RunList; +import hudson.util.ExceptionCatchingThreadFactory; import java.io.IOException; import java.io.PrintWriter; @@ -386,7 +387,7 @@ public abstract class Computer extends AbstractModelObject implements AccessCont return RemotingDiagnostics.getThreadDump(getChannel()); } - public static final ExecutorService threadPoolForRemoting = Executors.newCachedThreadPool(new DaemonThreadFactory()); + public static final ExecutorService threadPoolForRemoting = Executors.newCachedThreadPool(new ExceptionCatchingThreadFactory(new DaemonThreadFactory())); // // diff --git a/core/src/main/java/hudson/util/CompressedFile.java b/core/src/main/java/hudson/util/CompressedFile.java index 178e988e59..40c1117bd4 100644 --- a/core/src/main/java/hudson/util/CompressedFile.java +++ b/core/src/main/java/hudson/util/CompressedFile.java @@ -135,7 +135,8 @@ public class CompressedFile { * this should be a fairly low-priority task. */ private static final ExecutorService compressionThread = new ThreadPoolExecutor( - 0, 1, 5L, TimeUnit.SECONDS, new LinkedBlockingQueue(), new DaemonThreadFactory()); + 0, 1, 5L, TimeUnit.SECONDS, new LinkedBlockingQueue(), + new ExceptionCatchingThreadFactory(new DaemonThreadFactory())); private static final Logger LOGGER = Logger.getLogger(CompressedFile.class.getName()); } diff --git a/core/src/main/java/hudson/util/ExceptionCatchingThreadFactory.java b/core/src/main/java/hudson/util/ExceptionCatchingThreadFactory.java new file mode 100644 index 0000000000..fc216ddc41 --- /dev/null +++ b/core/src/main/java/hudson/util/ExceptionCatchingThreadFactory.java @@ -0,0 +1,37 @@ +package hudson.util; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.Executors; +import java.util.logging.Logger; +import java.util.logging.Level; + +/** + * {@link ThreadFactory} that creates a thread, which in turn displays a stack trace + * when it terminates unexpectedly. + * + * @author Kohsuke Kawaguchi + * @since 1.226 + */ +public class ExceptionCatchingThreadFactory implements ThreadFactory, Thread.UncaughtExceptionHandler { + private final ThreadFactory core; + + public ExceptionCatchingThreadFactory() { + this(Executors.defaultThreadFactory()); + } + + public ExceptionCatchingThreadFactory(ThreadFactory core) { + this.core = core; + } + + public Thread newThread(Runnable r) { + Thread t = core.newThread(r); + t.setUncaughtExceptionHandler(this); + return t; + } + + public void uncaughtException(Thread t, Throwable e) { + LOGGER.log(Level.WARNING, "Thread "+t.getName()+" terminated unexpectedly",e); + } + + private static final Logger LOGGER = Logger.getLogger(ExceptionCatchingThreadFactory.class.getName()); +}