Author: ggregory Date: Mon Oct 22 19:14:08 2012 New Revision: 1401016 URL: http://svn.apache.org/viewvc?rev=1401016&view=rev Log: [EXEC-68] Watchdog kills process immediately if timeout is too large.
Modified: commons/proper/exec/trunk/src/changes/changes.xml commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/Watchdog.java commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java Modified: commons/proper/exec/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/changes/changes.xml?rev=1401016&r1=1401015&r2=1401016&view=diff ============================================================================== --- commons/proper/exec/trunk/src/changes/changes.xml (original) +++ commons/proper/exec/trunk/src/changes/changes.xml Mon Oct 22 19:14:08 2012 @@ -24,6 +24,9 @@ </properties> <body> <release version="1.1.1-SNAPSHOT" date="TBA" description="Bugfixing Release"> + <action issue="EXEC-68" dev="ggregory" type="fix" date="2012-10-22" due-to="Joel McCance"> + Watchdog kills process immediately if timeout is too large. + </action> <action issue="EXEC-57" dev="sgoeschl" type="fix" date="2011-10-10" due-to="Nickolay Martinov"> Applied the patch from Nickolay Martinov but the timeout disguises the fact that the process might be still runnung - therefore added a sanity check in Modified: commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/Watchdog.java URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/Watchdog.java?rev=1401016&r1=1401015&r2=1401016&view=diff ============================================================================== --- commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/Watchdog.java (original) +++ commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/Watchdog.java Mon Oct 22 19:14:08 2012 @@ -69,25 +69,25 @@ public class Watchdog implements Runnabl } public void run() { - final long until = System.currentTimeMillis() + timeout; - boolean isWaiting; - synchronized (this) { - long now = System.currentTimeMillis(); - isWaiting = until > now; - while (!stopped && isWaiting) { - try { - wait(until - now); - } catch (InterruptedException e) { - } - now = System.currentTimeMillis(); - isWaiting = until > now; - } - } + final long startTime = System.currentTimeMillis(); + boolean isWaiting; + synchronized (this) { + long timeLeft = timeout - (System.currentTimeMillis() - startTime); + isWaiting = timeLeft > 0; + while (!stopped && isWaiting) { + try { + wait(timeLeft); + } catch (InterruptedException e) { + } + timeLeft = timeout - (System.currentTimeMillis() - startTime); + isWaiting = timeLeft > 0; + } + } - // notify the listeners outside of the synchronized block (see EXEC-60) - if (!isWaiting) { - fireTimeoutOccured(); - } - } + // notify the listeners outside of the synchronized block (see EXEC-60) + if (!isWaiting) { + fireTimeoutOccured(); + } + } } Modified: commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java?rev=1401016&r1=1401015&r2=1401016&view=diff ============================================================================== --- commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java (original) +++ commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java Mon Oct 22 19:14:08 2012 @@ -327,6 +327,31 @@ public class DefaultExecutorTest extends } /** + * [EXEC-68] Synchronously starts a short script with a Watchdog attached with an extremely large timeout. Checks + * to see if the script terminated naturally or if it was killed by the Watchdog. Fail if killed by Watchdog. + * + * @throws Exception + * the test failed + */ + public void testExecuteWatchdogVeryLongTimeout() throws Exception { + long timeout = Long.MAX_VALUE; + + CommandLine cl = new CommandLine(testScript); + DefaultExecutor executor = new DefaultExecutor(); + executor.setWorkingDirectory(new File(".")); + ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); + executor.setWatchdog(watchdog); + + try { + executor.execute(cl); + } catch (ExecuteException e) { + assertFalse("Process should exit normally, not be killed by watchdog", watchdog.killedProcess()); + // If the Watchdog did not kill it, something else went wrong. + throw e; + } + } + + /** * Try to start an non-existing application which should result * in an exception. *