CHE-3065 Add uncaught exceptions handler in threads being run by ExecutorService (#3174)

6.19.x
Max Shaposhnik 2016-11-28 11:50:50 +02:00 committed by GitHub
parent ac14c9e1ba
commit afadcd24f3
23 changed files with 118 additions and 5 deletions

View File

@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.che.api.core.notification;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.commons.lang.Pair;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
@ -108,6 +109,9 @@ public final class WSocketEventBusClient {
}
if (!cfg.isEmpty()) {
executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("WSocketEventBusClient-%d")
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler
.getInstance())
.setDaemon(true).build());
for (Map.Entry<URI, Set<String>> entry : cfg.entrySet()) {
executor.execute(new ConnectTask(entry.getKey(), entry.getValue()));

View File

@ -15,6 +15,7 @@ import com.google.inject.AbstractModule;
import org.eclipse.che.commons.lang.IoUtil;
import org.eclipse.che.commons.lang.Pair;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -43,6 +44,9 @@ public class FileCleaner {
private static ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder()
.setNameFormat("FileCleaner")
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler
.getInstance())
.setDaemon(true).build());
static {

View File

@ -13,6 +13,7 @@ package org.eclipse.che.everrest;
import com.google.common.base.MoreObjects;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.commons.env.EnvironmentContext;
import org.eclipse.che.commons.subject.Subject;
import org.everrest.core.DependencySupplier;
@ -174,7 +175,11 @@ public class ServerContainerInitializeListener implements ServletContextListener
final EverrestConfiguration everrestConfiguration = getEverrestConfiguration(servletContext);
final String threadNameFormat = "everrest.WSConnection." + servletContext.getServletContextName() + "-%d";
return Executors.newFixedThreadPool(everrestConfiguration.getAsynchronousPoolSize(),
new ThreadFactoryBuilder().setNameFormat(threadNameFormat).setDaemon(true).build());
new ThreadFactoryBuilder().setNameFormat(threadNameFormat)
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setDaemon(true)
.build());
}
protected SecurityContext createSecurityContext(final HandshakeRequest req) {

View File

@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright (c) 2012-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.commons.lang.concurrent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Writes uncaught exceptions in threads being run by {@link java.util.concurrent.ExecutorService} into application log.
*
* @author Max Shaposhnik
*/
public class LoggingUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private static final Logger LOG = LoggerFactory.getLogger(LoggingUncaughtExceptionHandler.class);
private static final LoggingUncaughtExceptionHandler INSTANCE = new LoggingUncaughtExceptionHandler();
@Override
public void uncaughtException(Thread t, Throwable e) {
LOG.error(String.format("Runtime exception caught in thread %s. Message: %s", t.getName(), e.getLocalizedMessage()), e);
}
public static LoggingUncaughtExceptionHandler getInstance() {
return INSTANCE;
}
private LoggingUncaughtExceptionHandler() {
}
}

View File

@ -45,6 +45,10 @@
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-commons-inject</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-commons-lang</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>

View File

@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.che.commons.schedule.executor;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.commons.schedule.Launcher;
import org.eclipse.che.inject.ConfigurationException;
@ -44,7 +45,10 @@ public class ThreadPullLauncher implements Launcher {
@Inject
public ThreadPullLauncher(@Named("schedule.core_pool_size") Integer corePoolSize) {
this.service = new CronThreadPoolExecutor(corePoolSize,
new ThreadFactoryBuilder().setNameFormat("Annotated-scheduler-%d").setDaemon(false)
new ThreadFactoryBuilder().setNameFormat("Annotated-scheduler-%d")
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setDaemon(false)
.build());
}

View File

@ -13,6 +13,7 @@ package org.eclipse.che.plugin.docker.client;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.sun.jna.ptr.LongByReference;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.api.core.util.SystemInfo;
import org.eclipse.che.commons.lang.Size;
import org.slf4j.Logger;
@ -55,6 +56,8 @@ public class CgroupOOMDetector implements DockerOOMDetector {
this.dockerConnector = dockerConnector;
this.oomDetectors = new ConcurrentHashMap<>();
this.executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("CgroupOOMDetector-%d")
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setDaemon(true)
.build());
}

View File

@ -19,6 +19,7 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import org.eclipse.che.api.core.util.FileCleaner;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.commons.lang.TarUtils;
import org.eclipse.che.commons.lang.ws.rs.ExtMediaType;
@ -147,6 +148,8 @@ public class DockerConnector {
this.authResolver = authResolver;
this.apiVersionPathPrefix = dockerApiVersionPathPrefixProvider.get();
executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder()
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setNameFormat("DockerApiConnector-%d")
.setDaemon(true)
.build());

View File

@ -15,6 +15,7 @@ import com.google.common.cache.CacheBuilder;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.api.machine.server.event.InstanceStateEvent;
import org.eclipse.che.commons.lang.Pair;
import org.eclipse.che.plugin.docker.client.DockerConnector;
@ -74,6 +75,8 @@ public class DockerInstanceStopDetector {
.build();
this.executorService = Executors.newSingleThreadExecutor(
new ThreadFactoryBuilder().setNameFormat("DockerInstanceStopDetector-%d")
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setDaemon(true)
.build());
}

View File

@ -21,6 +21,7 @@ import org.eclipse.che.api.core.model.machine.MachineStatus;
import org.eclipse.che.api.core.model.machine.ServerConf;
import org.eclipse.che.api.core.util.FileCleaner;
import org.eclipse.che.api.core.util.LineConsumer;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.api.core.util.SystemInfo;
import org.eclipse.che.api.environment.server.MachineInstanceProvider;
import org.eclipse.che.api.environment.server.model.CheServiceImpl;
@ -235,6 +236,8 @@ public class MachineProviderImpl implements MachineInstanceProvider {
// TODO single point of failure in case of highly loaded system
executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("MachineLogsStreamer-%d")
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setDaemon(true)
.build());
}

View File

@ -18,6 +18,7 @@ import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.core.model.workspace.Workspace;
import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.che.api.core.notification.EventSubscriber;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.api.workspace.server.WorkspaceFilesCleaner;
import org.eclipse.che.api.workspace.server.event.WorkspaceRemovedEvent;
import org.eclipse.che.commons.lang.concurrent.ThreadLocalPropagateContext;
@ -49,6 +50,8 @@ public class RemoveWorkspaceFilesAfterRemoveWorkspaceEventSubscriber implements
this.workspaceFilesCleaner = workspaceFilesCleaner;
this.eventService = eventService;
executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("RemoveWorkspaceFilesAfterRemoveWorkspaceEventSubscriber-%d")
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setDaemon(true)
.build());
}

View File

@ -13,6 +13,7 @@ package org.eclipse.che.plugin.maven.server.core;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.eclipse.che.api.core.util.ListLineConsumer;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.everrest.websockets.WSConnectionContext;
import org.everrest.websockets.message.ChannelBroadcastMessage;
import org.slf4j.Logger;
@ -40,6 +41,7 @@ public class BufferOutputFixedRateSender extends ListLineConsumer {
public BufferOutputFixedRateSender(String channel, long delay) {
this.channel = channel;
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(BufferOutputFixedRateSender.class.getSimpleName() + "-%d")
.setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance())
.setDaemon(true)
.build();

View File

@ -13,6 +13,7 @@ package org.eclipse.che.plugin.maven.server.core;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.inject.Singleton;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -33,7 +34,9 @@ public class MavenExecutorService {
private final ExecutorService service;
public MavenExecutorService() {
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Maven Executor - %d").build();
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Maven Executor - %d")
.setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance())
.build();
service = Executors.newFixedThreadPool(1, threadFactory);
}

View File

@ -12,6 +12,7 @@ package org.eclipse.che.plugin.nodejsdbg.server;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.commons.lang.IoUtil;
import org.eclipse.che.plugin.nodejsdbg.server.exception.NodeJsDebuggerException;
import org.eclipse.che.plugin.nodejsdbg.server.exception.NodeJsDebuggerTerminatedException;
@ -70,6 +71,8 @@ public class NodeJsDebugProcess implements NodeJsProcessObservable {
processWriter = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
executor = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder().setNameFormat("nodejs-debugger-%d")
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setDaemon(true)
.build());
executor.scheduleWithFixedDelay(new OutputReader(), 0, 100, TimeUnit.MILLISECONDS);

View File

@ -22,6 +22,7 @@ import org.eclipse.che.api.core.model.project.SourceStorage;
import org.eclipse.che.api.core.model.project.type.ProjectType;
import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.che.api.core.util.LineConsumerFactory;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.api.project.server.handlers.CreateProjectHandler;
import org.eclipse.che.api.project.server.handlers.ProjectHandlerRegistry;
import org.eclipse.che.api.project.server.importer.ProjectImportOutputWSLineConsumer;
@ -106,6 +107,8 @@ public final class ProjectManager {
executor = Executors.newFixedThreadPool(1 + Runtime.getRuntime().availableProcessors(),
new ThreadFactoryBuilder().setNameFormat("ProjectService-IndexingThread-")
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setDaemon(true).build());
}

View File

@ -13,6 +13,7 @@ package org.eclipse.che.api.project.server.importer;
import org.eclipse.che.api.core.util.LineConsumer;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.everrest.core.impl.provider.json.JsonUtils;
import org.everrest.websockets.WSConnectionContext;
import org.everrest.websockets.message.ChannelBroadcastMessage;
@ -44,7 +45,10 @@ public class ProjectImportOutputWSLineConsumer implements LineConsumer {
this.workspaceId = workspaceId;
lineToSendQueue = new ArrayBlockingQueue<>(1024);
executor = Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder().setNameFormat(ProjectImportOutputWSLineConsumer.class.getSimpleName()+"-%d").setDaemon(true).build());
new ThreadFactoryBuilder().setNameFormat(ProjectImportOutputWSLineConsumer.class.getSimpleName() + "-%d")
.setDaemon(true)
.setUncaughtExceptionHandler(LoggingUncaughtExceptionHandler.getInstance())
.build());
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {

View File

@ -12,6 +12,7 @@ package org.eclipse.che.api.vfs.impl.file;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.api.project.shared.dto.event.FileWatcherEventType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -85,7 +86,11 @@ public class FileTreeWatcher {
this.excludePatterns = newArrayList(excludePatterns);
this.fileWatcherNotificationHandler = fileWatcherNotificationHandler;
ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("FileTreeWatcher-%d").build();
ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true)
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setNameFormat("FileTreeWatcher-%d")
.build();
executor = Executors.newSingleThreadExecutor(threadFactory);
running = new AtomicBoolean();
watchedDirectories = newHashMap();

View File

@ -13,6 +13,7 @@ package org.eclipse.che.api.vfs.impl.file.event;
import com.google.common.annotations.Beta;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.slf4j.Logger;
import javax.annotation.PostConstruct;
@ -41,6 +42,8 @@ abstract class VfsEventService {
VfsEventService() {
final String threadName = getClass().getSimpleName().concat("Thread-%d");
final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(threadName)
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setDaemon(TRUE)
.build();

View File

@ -13,6 +13,7 @@ package org.eclipse.che.api.vfs.search.impl;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.api.vfs.VirtualFileFilter;
import org.eclipse.che.api.vfs.VirtualFileFilters;
import org.eclipse.che.api.vfs.VirtualFileSystem;
@ -42,6 +43,8 @@ public abstract class AbstractLuceneSearcherProvider implements SearcherProvider
this.excludeFileIndexFilters = mergeFileIndexFilters(excludeFileIndexFilters);
executor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder()
.setDaemon(true)
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setNameFormat("LuceneSearcherInitThread")
.build());
}

View File

@ -18,6 +18,7 @@ import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.core.model.machine.Command;
import org.eclipse.che.api.core.util.AbstractLineConsumer;
import org.eclipse.che.api.core.util.LineConsumer;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.api.machine.server.exception.MachineException;
import org.eclipse.che.api.machine.server.model.impl.CommandImpl;
import org.eclipse.che.api.machine.server.spi.Instance;
@ -47,6 +48,8 @@ public abstract class AbstractAgentLauncher implements AgentLauncher {
private static final Logger LOG = LoggerFactory.getLogger(AbstractAgentLauncher.class);
private static final ExecutorService executor =
Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("AgentLauncher-%d")
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setDaemon(true)
.build());

View File

@ -22,6 +22,7 @@ import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.che.api.core.util.CompositeLineConsumer;
import org.eclipse.che.api.core.util.FileLineConsumer;
import org.eclipse.che.api.core.util.LineConsumer;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.api.core.util.WebsocketLineConsumer;
import org.eclipse.che.api.machine.server.exception.MachineException;
import org.eclipse.che.api.machine.server.spi.Instance;
@ -75,6 +76,8 @@ public class MachineProcessManager {
this.environmentEngine = environmentEngine;
executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("MachineProcessManager-%d")
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setDaemon(false)
.build());
}

View File

@ -26,6 +26,7 @@ import org.eclipse.che.api.core.model.workspace.Workspace;
import org.eclipse.che.api.core.model.workspace.WorkspaceConfig;
import org.eclipse.che.api.core.model.workspace.WorkspaceStatus;
import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.api.environment.server.exception.EnvironmentException;
import org.eclipse.che.api.machine.server.exception.SnapshotException;
import org.eclipse.che.api.machine.server.exception.SourceNotFoundException;
@ -122,6 +123,8 @@ public class WorkspaceManager {
this.snapshotDao = snapshotDao;
executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("WorkspaceManager-%d")
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setDaemon(true)
.build());
}

View File

@ -34,6 +34,7 @@ import org.eclipse.che.api.core.model.workspace.WorkspaceRuntime;
import org.eclipse.che.api.core.model.workspace.WorkspaceStatus;
import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.che.api.core.util.AbstractMessageConsumer;
import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
import org.eclipse.che.api.core.util.MessageConsumer;
import org.eclipse.che.api.core.util.WebsocketMessageConsumer;
import org.eclipse.che.api.environment.server.CheEnvironmentEngine;
@ -126,6 +127,8 @@ public class WorkspaceRuntimes {
this.stripedLocks = new StripedLocks(16);
executor = Executors.newFixedThreadPool(2 * Runtime.getRuntime().availableProcessors(),
new ThreadFactoryBuilder().setNameFormat("WorkspaceRuntimes-%d")
.setUncaughtExceptionHandler(
LoggingUncaughtExceptionHandler.getInstance())
.setDaemon(false)
.build());
}