From 2af7d092bbe168d642ce49bb27abbe2310c547f8 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Thu, 27 Oct 2016 10:36:47 +0200 Subject: [PATCH] [java] explicit destroying of process proxy instances --- .../impl/DefaultJavaProgramRunner.java | 24 +++++++++++++++---- .../execution/runners/ProcessProxyImpl.java | 21 ++++++++-------- .../execution/runners/ProcessProxy.java | 11 +++++---- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/java/execution/impl/src/com/intellij/execution/impl/DefaultJavaProgramRunner.java b/java/execution/impl/src/com/intellij/execution/impl/DefaultJavaProgramRunner.java index 2a237cc9ca0c..8d1c4cbf6963 100644 --- a/java/execution/impl/src/com/intellij/execution/impl/DefaultJavaProgramRunner.java +++ b/java/execution/impl/src/com/intellij/execution/impl/DefaultJavaProgramRunner.java @@ -72,7 +72,7 @@ public class DefaultJavaProgramRunner extends JavaPatchableProgramRunner { } @Override - protected RunContentDescriptor doExecute(@NotNull final RunProfileState state, @NotNull final ExecutionEnvironment env) throws ExecutionException { + protected RunContentDescriptor doExecute(@NotNull RunProfileState state, @NotNull ExecutionEnvironment env) throws ExecutionException { FileDocumentManager.getInstance().saveAllDocuments(); ExecutionResult executionResult; @@ -80,11 +80,25 @@ public class DefaultJavaProgramRunner extends JavaPatchableProgramRunner { if (state instanceof JavaCommandLine) { final JavaParameters parameters = ((JavaCommandLine)state).getJavaParameters(); patch(parameters, env.getRunnerSettings(), env.getRunProfile(), true); - final ProcessProxy proxy = ProcessProxyFactory.getInstance().createCommandLineProxy((JavaCommandLine)state); + + ProcessProxy proxy = ProcessProxyFactory.getInstance().createCommandLineProxy((JavaCommandLine)state); executionResult = state.execute(env.getExecutor(), this); - if (proxy != null && executionResult != null) { - proxy.attach(executionResult.getProcessHandler()); + if (proxy != null) { + ProcessHandler handler = executionResult != null ? executionResult.getProcessHandler() : null; + if (handler != null) { + proxy.attach(handler); + handler.addProcessListener(new ProcessAdapter() { + @Override + public void processTerminated(ProcessEvent event) { + proxy.destroy(); + } + }); + } + else { + proxy.destroy(); + } } + if (state instanceof JavaCommandLineState && !((JavaCommandLineState)state).shouldAddJavaProgramRunnerActions()) { shouldAddDefaultActions = false; } @@ -248,4 +262,4 @@ public class DefaultJavaProgramRunner extends JavaPatchableProgramRunner { public static ProgramRunner getInstance() { return RunnerRegistry.getInstance().findRunnerById(DEFAULT_JAVA_RUNNER_ID); } -} +} \ No newline at end of file diff --git a/java/execution/impl/src/com/intellij/execution/runners/ProcessProxyImpl.java b/java/execution/impl/src/com/intellij/execution/runners/ProcessProxyImpl.java index 66877b118e45..ee798b92465b 100644 --- a/java/execution/impl/src/com/intellij/execution/runners/ProcessProxyImpl.java +++ b/java/execution/impl/src/com/intellij/execution/runners/ProcessProxyImpl.java @@ -17,6 +17,7 @@ package com.intellij.execution.runners; import com.intellij.execution.process.ProcessHandler; import com.intellij.openapi.util.Key; +import org.jetbrains.annotations.NotNull; import java.io.BufferedWriter; import java.io.IOException; @@ -73,17 +74,7 @@ class ProcessProxyImpl implements ProcessProxy { } @Override - @SuppressWarnings("FinalizeDeclaration") - protected synchronized void finalize() throws Throwable { - if (myWriter != null) { - myWriter.close(); - } - ourUsedSockets[myPortNumber - SOCKET_NUMBER_START] = false; - super.finalize(); - } - - @Override - public void attach(final ProcessHandler processHandler) { + public void attach(@NotNull ProcessHandler processHandler) { processHandler.putUserData(KEY, this); } @@ -113,4 +104,12 @@ class ProcessProxyImpl implements ProcessProxy { public void sendStop() { writeLine("STOP"); } + + @Override + public synchronized void destroy() { + if (myWriter != null) { + myWriter.close(); + } + ourUsedSockets[myPortNumber - SOCKET_NUMBER_START] = false; + } } \ No newline at end of file diff --git a/platform/lang-api/src/com/intellij/execution/runners/ProcessProxy.java b/platform/lang-api/src/com/intellij/execution/runners/ProcessProxy.java index b16ece341609..e74223375084 100644 --- a/platform/lang-api/src/com/intellij/execution/runners/ProcessProxy.java +++ b/platform/lang-api/src/com/intellij/execution/runners/ProcessProxy.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,13 +16,16 @@ package com.intellij.execution.runners; import com.intellij.execution.process.ProcessHandler; +import org.jetbrains.annotations.NotNull; public interface ProcessProxy { int getPortNumber(); - void attach(ProcessHandler processHandler); + void attach(@NotNull ProcessHandler processHandler); - void sendBreak (); + void sendBreak(); - void sendStop (); + void sendStop(); + + default void destroy() { } } \ No newline at end of file