From 5f8c7f5e5d3d9f371d785d67004eb852b506e032 Mon Sep 17 00:00:00 2001 From: Luca Milanesio Date: Wed, 29 Mar 2017 08:41:13 +0100 Subject: [PATCH] Shutdown SSH executors upon SshDaemon stop When Gerrit SSH Daemon is stopped, there is no value in keeping its executors threads alive as it would only consume precious resources we do need during our Integration Tests suite. Apache SSHD does not manage the shutdown of the internal executors by himself, so we need to close them manually. Change-Id: I09a62759769bbb222abd4a3ea60be8b8c5571ac9 --- .../com/google/gerrit/sshd/SshDaemon.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshDaemon.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshDaemon.java index f573c1d843..45f68e8caf 100644 --- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshDaemon.java +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshDaemon.java @@ -60,6 +60,7 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Set; +import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicInteger; import org.apache.mina.transport.socket.SocketSessionConfig; import org.apache.sshd.common.BaseBuilder; @@ -72,7 +73,9 @@ import org.apache.sshd.common.file.FileSystemFactory; import org.apache.sshd.common.forward.DefaultTcpipForwarderFactory; import org.apache.sshd.common.future.CloseFuture; import org.apache.sshd.common.future.SshFutureListener; +import org.apache.sshd.common.io.AbstractIoServiceFactory; import org.apache.sshd.common.io.IoAcceptor; +import org.apache.sshd.common.io.IoServiceFactory; import org.apache.sshd.common.io.IoServiceFactoryFactory; import org.apache.sshd.common.io.IoSession; import org.apache.sshd.common.io.mina.MinaServiceFactoryFactory; @@ -349,6 +352,7 @@ public class SshDaemon extends SshServer implements SshInfo, LifecycleListener { if (daemonAcceptor != null) { try { daemonAcceptor.close(true).await(); + shutdownExecutors(); sshDaemonLog.info("Stopped Gerrit SSHD"); } catch (IOException e) { sshDaemonLog.warn("Exception caught while closing", e); @@ -358,6 +362,25 @@ public class SshDaemon extends SshServer implements SshInfo, LifecycleListener { } } + private void shutdownExecutors() { + if (executor != null) { + executor.shutdownNow(); + } + + IoServiceFactory serviceFactory = getIoServiceFactory(); + if (serviceFactory instanceof AbstractIoServiceFactory) { + shutdownServiceFactoryExecutor((AbstractIoServiceFactory) serviceFactory); + } + } + + private void shutdownServiceFactoryExecutor(AbstractIoServiceFactory ioServiceFactory) { + ioServiceFactory.close(true); + ExecutorService serviceFactoryExecutor = ioServiceFactory.getExecutorService(); + if (serviceFactoryExecutor != null && serviceFactoryExecutor != executor) { + serviceFactoryExecutor.shutdownNow(); + } + } + @Override protected void checkConfig() { super.checkConfig();