kill process tree on pooled thread (IDEA-141457)
authorSergey Simonchik <sergey.simonchik@jetbrains.com>
Tue, 16 Jun 2015 22:02:43 +0000 (01:02 +0300)
committerSergey Simonchik <sergey.simonchik@jetbrains.com>
Tue, 16 Jun 2015 22:02:43 +0000 (01:02 +0300)
platform/platform-api/src/com/intellij/execution/process/OSProcessHandler.java

index bcfa033b21b9e07513f5920abd7b7937b8cf7d23..4ce8a9acd7436b0c395c5580a6901933190752fd 100644 (file)
@@ -95,19 +95,47 @@ public class OSProcessHandler extends BaseOSProcessHandler {
   }
 
   /**
   }
 
   /**
-   * Kill the whole process tree.
+   * Kills the whole process tree asynchronously.
+   * As a potentially time-consuming operation, it's executed asynchronously on a pooled thread.
    *
    * @param process Process
    *
    * @param process Process
-   * @return True if process tree has been successfully killed.
    */
    */
-  protected boolean killProcessTree(@NotNull Process process) {
+  protected void killProcessTree(@NotNull final Process process) {
+    if (ApplicationManager.getApplication().isUnitTestMode()) {
+      killProcessTreeSync(process);
+    }
+    else {
+      executeOnPooledThread(new Runnable() {
+        @Override
+        public void run() {
+          killProcessTreeSync(process);
+        }
+      });
+    }
+  }
+
+  private void killProcessTreeSync(@NotNull Process process) {
     LOG.debug("killing process tree");
     final boolean destroyed = OSProcessManager.getInstance().killProcessTree(process);
     if (!destroyed) {
     LOG.debug("killing process tree");
     final boolean destroyed = OSProcessManager.getInstance().killProcessTree(process);
     if (!destroyed) {
-      LOG.warn("Cannot kill process tree. Trying to destroy process using Java API. Cmdline:\n" + myCommandLine);
-      process.destroy();
+      if (isTerminated(process)) {
+        LOG.warn("Process has been already terminated: " + myCommandLine);
+      }
+      else {
+        LOG.warn("Cannot kill process tree. Trying to destroy process using Java API. Cmdline:\n" + myCommandLine);
+        process.destroy();
+      }
+    }
+  }
+
+  private static boolean isTerminated(@NotNull Process process) {
+    try {
+      process.exitValue();
+      return true;
+    }
+    catch (IllegalThreadStateException e) {
+      return false;
     }
     }
-    return destroyed;
   }
 
   /**
   }
 
   /**