From 9a2829dab0de344d279d2d03def0798ae0fe8e6c Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Mon, 26 Oct 2015 19:45:05 +0300 Subject: [PATCH] execute long-running builtin server in a separate thread to make it distinguishable in a stacktrace --- .../src/org/jetbrains/io/BuiltInServer.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/platform/platform-impl/src/org/jetbrains/io/BuiltInServer.java b/platform/platform-impl/src/org/jetbrains/io/BuiltInServer.java index f32df2f3f1f4..88044639fa57 100644 --- a/platform/platform-impl/src/org/jetbrains/io/BuiltInServer.java +++ b/platform/platform-impl/src/org/jetbrains/io/BuiltInServer.java @@ -18,6 +18,7 @@ package org.jetbrains.io; import com.intellij.openapi.Disposable; import com.intellij.openapi.diagnostic.Logger; import com.intellij.util.ArrayUtil; +import com.intellij.util.ConcurrencyUtil; import com.intellij.util.ExceptionUtil; import com.intellij.util.NotNullProducer; import com.intellij.util.net.NetUtils; @@ -26,10 +27,11 @@ import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.ide.PooledThreadExecutor; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class BuiltInServer implements Disposable { // Some antiviral software detect viruses by the fact of accessing these ports so we should not touch them to appear innocent. @@ -75,7 +77,11 @@ public class BuiltInServer implements Disposable { int portsCount, boolean tryAnyPort, @Nullable NotNullProducer handler) throws Exception { - return start(new NioEventLoopGroup(workerCount, PooledThreadExecutor.INSTANCE), true, firstPort, portsCount, tryAnyPort, handler); + return start(new NioEventLoopGroup(workerCount, createExecutor()), true, firstPort, portsCount, tryAnyPort, handler); + } + + private static ExecutorService createExecutor() { + return Executors.newCachedThreadPool(ConcurrencyUtil.newNamedThreadFactory("Netty Builtin Server")); } @NotNull @@ -94,11 +100,11 @@ public class BuiltInServer implements Disposable { static void configureChildHandler(@NotNull ServerBootstrap bootstrap, @NotNull final ChannelRegistrar channelRegistrar, - final @Nullable NotNullProducer channelHandler) { + @Nullable final NotNullProducer channelHandler) { final PortUnificationServerHandler portUnificationServerHandler = channelHandler == null ? new PortUnificationServerHandler() : null; bootstrap.childHandler(new ChannelInitializer() { @Override - protected void initChannel(Channel channel) throws Exception { + protected void initChannel(@NotNull Channel channel) throws Exception { channel.pipeline().addLast(channelRegistrar, channelHandler == null ? portUnificationServerHandler : channelHandler.produce()); } }); @@ -123,7 +129,7 @@ public class BuiltInServer implements Disposable { channelRegistrar.add(future.channel()); return port; } - else if (!tryAnyPort && i == (portsCount - 1)) { + else if (!tryAnyPort && i == portsCount - 1) { ExceptionUtil.rethrowAll(future.cause()); } } @@ -134,9 +140,7 @@ public class BuiltInServer implements Disposable { channelRegistrar.add(future.channel()); return ((InetSocketAddress)future.channel().localAddress()).getPort(); } - else { - ExceptionUtil.rethrowAll(future.cause()); - } + ExceptionUtil.rethrowAll(future.cause()); return -1; // unreachable }