From 2b4b4f304448602c619ff2fac40909c7d22fe59c Mon Sep 17 00:00:00 2001 From: Dmitry Trofimov Date: Tue, 7 Sep 2010 20:23:02 +0400 Subject: [PATCH] fixed python process termination issues on mac and linux (PY-651) --- .../python/run/PythonCommandLineState.java | 14 ++-- .../python/run/PythonProcessHandler.java | 64 +++++++++++++++++++ .../PythonTestCommandLineStateBase.java | 6 -- 3 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 python/src/com/jetbrains/python/run/PythonProcessHandler.java diff --git a/python/src/com/jetbrains/python/run/PythonCommandLineState.java b/python/src/com/jetbrains/python/run/PythonCommandLineState.java index d0ef99124bfd..14f3df9b26e1 100644 --- a/python/src/com/jetbrains/python/run/PythonCommandLineState.java +++ b/python/src/com/jetbrains/python/run/PythonCommandLineState.java @@ -101,11 +101,7 @@ public abstract class PythonCommandLineState extends CommandLineState { } protected ColoredProcessHandler doCreateProcess(GeneralCommandLine commandLine) throws ExecutionException { - return createProcessHandler(commandLine.createProcess(), commandLine); - } - - protected ColoredProcessHandler createProcessHandler(Process process, GeneralCommandLine commandLine) throws ExecutionException { - return new ColoredProcessHandler(process, commandLine.getCommandLineString()); + return PythonProcessHandler.createProcessHandler(null); } public GeneralCommandLine generateCommandLine() throws ExecutionException { @@ -143,16 +139,22 @@ public abstract class PythonCommandLineState extends CommandLineState { envs = new HashMap(envs); addPredefinedEnvironmentVariables(envs); + addCommonEnvironmentVariables(envs); + + commandLine.setEnvParams(envs); commandLine.setPassParentEnvs(myConfig.isPassParentEnvs()); } + protected void addCommonEnvironmentVariables(Map envs) { + PythonEnvUtil.setPythonUnbuffered(envs); + } + protected void addPredefinedEnvironmentVariables(Map envs) { final PythonSdkFlavor flavor = PythonSdkFlavor.getFlavor(myConfig.getInterpreterPath()); if (flavor != null) { flavor.addPredefinedEnvironmentVariables(envs); } - PythonEnvUtil.setPythonUnbuffered(envs); } protected void setRunnerPath(GeneralCommandLine commandLine) throws ExecutionException { diff --git a/python/src/com/jetbrains/python/run/PythonProcessHandler.java b/python/src/com/jetbrains/python/run/PythonProcessHandler.java new file mode 100644 index 000000000000..355078bcf69f --- /dev/null +++ b/python/src/com/jetbrains/python/run/PythonProcessHandler.java @@ -0,0 +1,64 @@ +package com.jetbrains.python.run; + +import com.intellij.execution.ExecutionException; +import com.intellij.execution.configurations.GeneralCommandLine; +import com.intellij.execution.process.ColoredProcessHandler; +import com.intellij.execution.process.RunnerMediator; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.ui.Messages; +import org.jetbrains.annotations.NotNull; + +/** + * @author traff + */ +public class PythonProcessHandler extends RunnerMediator.CustomDestroyProcessHandler { + + private PythonProcessHandler(@NotNull Process process, @NotNull GeneralCommandLine commandLine, @NotNull String processUid) { + super(process, commandLine, processUid); + } + + @Override + protected void detachProcessImpl() { + ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { + public void run() { + long millis = System.currentTimeMillis(); + while (true) { + try { + getProcess().exitValue(); + return; + } + catch (IllegalThreadStateException e) { + if (System.currentTimeMillis() - millis > 5000L) { + if (Messages.showYesNoDialog("Do you want to terminate the process?", "Process is not responding", null) == 0) { + killProcess(); + return; + } + else { + return; + } + } + } + try { + synchronized (this) { + wait(2000L); + } + } + catch (InterruptedException ignore) { + } + } + } + } + ); + super.detachProcessImpl(); + } + + public static ColoredProcessHandler createProcessHandler(GeneralCommandLine commandLine) + throws ExecutionException { + + String processUid = RunnerMediator.injectUid(commandLine); + + Process p = commandLine.createProcess(); + + return new PythonProcessHandler(p, commandLine, processUid); + } +} diff --git a/python/src/com/jetbrains/python/testing/PythonTestCommandLineStateBase.java b/python/src/com/jetbrains/python/testing/PythonTestCommandLineStateBase.java index 04c7bb8ec49c..5c6c89be7859 100644 --- a/python/src/com/jetbrains/python/testing/PythonTestCommandLineStateBase.java +++ b/python/src/com/jetbrains/python/testing/PythonTestCommandLineStateBase.java @@ -75,12 +75,6 @@ public abstract class PythonTestCommandLineStateBase extends PythonCommandLineSt return cmd; } - @Override - protected void addPredefinedEnvironmentVariables(Map envs) { - super.addPredefinedEnvironmentVariables(envs); - PythonEnvUtil.setPythonUnbuffered(envs); - } - protected Collection buildPythonPath() { Collection pythonPathList = Sets.newLinkedHashSet(); pythonPathList.add(PythonHelpersLocator.getHelpersRoot().getPath());