[java] fixes a tread leakage in process proxy

This commit is contained in:
Roman Shevchenko
2016-10-31 11:35:41 +01:00
parent 1735492b7c
commit ed55e36a8d
2 changed files with 14 additions and 7 deletions
@@ -23,6 +23,7 @@ import com.intellij.execution.process.ProcessHandler;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.projectRoots.ex.JavaSdkUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.rt.execution.application.AppMain;
import java.io.File;
@@ -41,7 +42,7 @@ public class ProcessProxyFactoryImpl extends ProcessProxyFactory {
if (moduleName == null || new File(rtJarPath).isFile()) {
try {
ProcessProxyImpl proxy = new ProcessProxyImpl();
ProcessProxyImpl proxy = new ProcessProxyImpl(StringUtil.getShortName(mainClass));
String port = String.valueOf(proxy.getPortNumber());
String binPath = PathManager.getBinPath();
@@ -30,9 +30,11 @@ import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.TimeUnit;
/**
* @author ven
@@ -40,20 +42,21 @@ import java.nio.channels.CompletionHandler;
class ProcessProxyImpl implements ProcessProxy {
static final Key<ProcessProxyImpl> KEY = Key.create("ProcessProxyImpl");
private final AsynchronousServerSocketChannel myChannel;
private final AsynchronousChannelGroup myGroup;
private final int myPort;
private final Object myLock = new Object();
private AsynchronousSocketChannel myConnection;
private int myPid;
ProcessProxyImpl() throws IOException {
myChannel = AsynchronousServerSocketChannel.open()
ProcessProxyImpl(String mainClass) throws IOException {
myGroup = AsynchronousChannelGroup.withFixedThreadPool(1, r -> new Thread(r, "Process Proxy: " + mainClass));
AsynchronousServerSocketChannel channel = AsynchronousServerSocketChannel.open(myGroup)
.bind(new InetSocketAddress("127.0.0.1", 0))
.setOption(StandardSocketOptions.SO_REUSEADDR, true);
myPort = ((InetSocketAddress)myChannel.getLocalAddress()).getPort();
myPort = ((InetSocketAddress)channel.getLocalAddress()).getPort();
myChannel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {
channel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {
@Override
public void completed(AsynchronousSocketChannel channel, Void attachment) {
synchronized (myLock) {
@@ -146,7 +149,10 @@ class ProcessProxyImpl implements ProcessProxy {
}
}
});
execute(myChannel::close);
execute(() -> {
myGroup.shutdownNow();
myGroup.awaitTermination(1, TimeUnit.SECONDS);
});
}
private static void execute(ThrowableRunnable<Exception> block) {