Commits

Michael Heemskerk committed db74813

STASHDEV-367: Abort processes when one of the input/outputhandlers throws an exception

  • Participants
  • Parent commits 151970d

Comments (0)

Files changed (1)

src/main/java/com/atlassian/utils/process/ExternalProcessImpl.java

         }
     }
 
+    private void handleHandlerError(String handlerName, Throwable t) {
+        if (!isCanceled()) {
+            LOG.debug(handlerName + " encountered an error; aborting process", t);
+            cancel();
+            if (t instanceof ProcessException) {
+                processException = (ProcessException) t;
+            } else {
+                processException = new ProcessException(t);
+            }
+        } else {
+            LOG.debug(handlerName + ": Process canceled; ignoring exception", t);
+        }
+    }
+    
     private void setupIOPumps() {
         // set up threads to feed data to and extract data from the process
         if (handler.hasInput()) {
             inputPump = new LatchedRunnable("StdInHandler " + process) {
 
                 protected void doTask() {
-                    handler.provideInput(process.getOutputStream());
+                    try {
+                        handler.provideInput(process.getOutputStream());
+                    } catch (Throwable t) {
+                        handleHandlerError(name, t);
+                    }
                 }
             };
         }
             protected void doTask() {
                 try {
                     handler.processError(process.getErrorStream());
-                } catch (Throwable e) {
-                    if (!isCancelled()) {
-                        LOG.debug(name + ": Process wasn't canceled; storing exception", e);
-                        processException = new ProcessException(e);
-                    } else {
-                        LOG.debug(name + ": Process canceled; ignoring exception", e);
-                    }
+                } catch (Throwable t) {
+                    handleHandlerError(name, t);
                 }
             }
         };
             protected void doTask() {
                 try {
                     handler.processOutput(process.getInputStream());
-                } catch (Throwable e) {
-                    if (!isCancelled()) {
-                        LOG.debug(name + ": Process wasn't canceled; storing exception", e);
-                        processException = new ProcessException(e);
-                    } else {
-                        LOG.debug(name + ": Process canceled; ignoring exception", e);
-                    }
+                } catch (Throwable t) {
+                    handleHandlerError(name, t);
                 }
             }
         };