Michael Heemskerk avatar Michael Heemskerk committed db74813

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

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);
                 }
             }
         };
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.