Move around a bit the logic of process exec in RunUtil

Avoid spamming "Cancelling process". Only call cancel
if it's an exception or a timeout. All other cases the
process will terminate naturally.

Bug: 123529934
Test: unit tests
Bug: 119259967
Change-Id: I2583805ba21dc33643f4082ea751b1e7411d17e7
Merged-In: I2583805ba21dc33643f4082ea751b1e7411d17e7
diff --git a/src/com/android/tradefed/command/CommandInterrupter.java b/src/com/android/tradefed/command/CommandInterrupter.java
index f01c55c..030705b 100644
--- a/src/com/android/tradefed/command/CommandInterrupter.java
+++ b/src/com/android/tradefed/command/CommandInterrupter.java
@@ -118,7 +118,7 @@
      * Interrupts the current thread if it should be interrupted. Threads are encouraged to
      * periodically call this method in order to throw the right {@link RunInterruptedException}.
      */
-    public void checkInterrupted() {
+    public void checkInterrupted() throws RunInterruptedException {
         Thread thread = Thread.currentThread();
         if (isInterruptible()) {
             String message = mInterruptMessage.remove(thread);
diff --git a/src/com/android/tradefed/util/RunUtil.java b/src/com/android/tradefed/util/RunUtil.java
index b1bd220..78483f8 100644
--- a/src/com/android/tradefed/util/RunUtil.java
+++ b/src/com/android/tradefed/util/RunUtil.java
@@ -326,7 +326,7 @@
                 CLog.d("Running command without timeout.");
             }
         }
-
+        CommandStatus status = CommandStatus.TIMED_OUT;
         try {
             runThread.start();
             long startTime = System.currentTimeMillis();
@@ -351,20 +351,20 @@
                 mInterrupter.checkInterrupted();
             } while ((timeout == 0L || (System.currentTimeMillis() - startTime) < timeout)
                     && runThread.isAlive());
+        } catch (RunInterruptedException e) {
+            runThread.cancel();
+            throw e;
+        } finally {
             // Snapshot the status when out of the run loop because thread may terminate and return
             // a false FAILED instead of TIMED_OUT.
-            CommandStatus status = runThread.getStatus();
+            status = runThread.getStatus();
             if (CommandStatus.TIMED_OUT.equals(status) || CommandStatus.EXCEPTION.equals(status)) {
                 CLog.i("runTimed: Calling interrupt, status is %s", status);
                 runThread.cancel();
             }
-            mInterrupter.checkInterrupted();
-            return status;
-
-        } finally {
-            // always ensure the execution is cancelled
-            runThread.cancel();
         }
+        mInterrupter.checkInterrupted();
+        return status;
     }
 
     /**
@@ -674,12 +674,15 @@
 
         @Override
         public void cancel() {
+            if (mCancelled) {
+                return;
+            }
             mCancelled = true;
             synchronized (mLock) {
                 if (mProcess == null || !mProcess.isAlive()) {
                     return;
                 }
-                CLog.i("Cancelling the process execution");
+                CLog.d("Cancelling the process execution");
                 mProcess.destroy();
                 try {
                     // Only allow to continue if the Stdout has been read