From 9a2c6d11d49219ccfeaa5ebf899c4aea64cc7aea Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Sun, 7 Oct 2007 17:44:53 +0000
Subject: [PATCH] add: method ProcessResult.destroyProcess() in order to cope
 with an IOException under Linux when spawning processes with high frequency

SVN: 2026
---
 .../utilities/ProcessExecutionHelper.java     | 35 ++++++++++++++-----
 1 file changed, 27 insertions(+), 8 deletions(-)

diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/ProcessExecutionHelper.java b/common/source/java/ch/systemsx/cisd/common/utilities/ProcessExecutionHelper.java
index 49df956358a..2d530d84508 100644
--- a/common/source/java/ch/systemsx/cisd/common/utilities/ProcessExecutionHelper.java
+++ b/common/source/java/ch/systemsx/cisd/common/utilities/ProcessExecutionHelper.java
@@ -93,6 +93,23 @@ public class ProcessExecutionHelper
             this.outputLines = null;
         }
 
+        /**
+         * Calls the {@link Process#destroy()} method which explicitly closes some file handles.
+         * <p>
+         * <i>Note that one must not call {#link {@link #getProcessOutput()} for the first time after this method has
+         * been called.</i>
+         * <p>
+         * Whether it is necessary to call this method depends on the JRE. For some JREs there occur {@link IOException}s
+         * with code 24 ("Too many open files") when running processes with high frequency without calling this method.
+         */
+        public void destroyProcess()
+        {
+            if (processOrNull != null)
+            {
+                processOrNull.destroy();
+            }
+        }
+
         /**
          * Returns the command line that belongs to this process.
          */
@@ -213,7 +230,7 @@ public class ProcessExecutionHelper
      */
     public static boolean runAndLog(List<String> commandLine, Logger operationLog, Logger machineLog)
     {
-        return new ProcessExecutionHelper(operationLog, machineLog).run(commandLine, 0L);
+        return new ProcessExecutionHelper(operationLog, machineLog).runAndLog(commandLine, 0L);
     }
 
     /**
@@ -226,7 +243,7 @@ public class ProcessExecutionHelper
      */
     public static ProcessResult run(List<String> commandLine, Logger operationLog, Logger machineLog)
     {
-        return new ProcessExecutionHelper(operationLog, machineLog).tryRun(commandLine, 0L);
+        return new ProcessExecutionHelper(operationLog, machineLog).run(commandLine, 0L);
     }
 
     /**
@@ -239,9 +256,10 @@ public class ProcessExecutionHelper
      * @param machineLog The {@link Logger} to use for all message on the lower (machine) level.
      * @return <code>true</code>, if the process did complete successfully, <code>false</code> otherwise.
      */
-    public static boolean runAndLog(List<String> cmd, long millisToWaitForCompletion, Logger operationLog, Logger machineLog)
+    public static boolean runAndLog(List<String> cmd, long millisToWaitForCompletion, Logger operationLog,
+            Logger machineLog)
     {
-        return new ProcessExecutionHelper(operationLog, machineLog).run(cmd, millisToWaitForCompletion);
+        return new ProcessExecutionHelper(operationLog, machineLog).runAndLog(cmd, millisToWaitForCompletion);
     }
 
     /**
@@ -257,7 +275,7 @@ public class ProcessExecutionHelper
     public static ProcessResult run(List<String> cmd, long millisToWaitForCompletion, Logger operationLog,
             Logger machineLog)
     {
-        return new ProcessExecutionHelper(operationLog, machineLog).tryRun(cmd, millisToWaitForCompletion);
+        return new ProcessExecutionHelper(operationLog, machineLog).run(cmd, millisToWaitForCompletion);
     }
 
     /**
@@ -314,7 +332,7 @@ public class ProcessExecutionHelper
         this.machineLog = machineLog;
     }
 
-    private ProcessResult tryRun(List<String> commandLine, long millisoWaitForCompletion)
+    private ProcessResult run(List<String> commandLine, long millisoWaitForCompletion)
     {
         final ProcessBuilder processBuilder = new ProcessBuilder(commandLine);
         processBuilder.redirectErrorStream(true);
@@ -373,10 +391,11 @@ public class ProcessExecutionHelper
         return watchDogOrNull;
     }
 
-    private boolean run(List<String> cmd, long millisToWaitForCompletion)
+    private boolean runAndLog(List<String> cmd, long millisToWaitForCompletion)
     {
-        final ProcessResult result = tryRun(cmd, millisToWaitForCompletion);
+        final ProcessResult result = run(cmd, millisToWaitForCompletion);
         result.log();
+        result.destroyProcess();
         return result.isOK();
     }
 
-- 
GitLab