diff --git a/src/main/org/apache/tools/ant/taskdefs/Execute.java b/src/main/org/apache/tools/ant/taskdefs/Execute.java index 499d9581e..155751af5 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Execute.java +++ b/src/main/org/apache/tools/ant/taskdefs/Execute.java @@ -450,27 +450,30 @@ public class Execute { } streamHandler.start(); - // add the process to the list of those to destroy if the VM exits - // - processDestroyer.add(process); + try { + // add the process to the list of those to destroy if the VM exits + // + processDestroyer.add(process); - if (watchdog != null) { - watchdog.start(process); - } - waitFor(process); + if (watchdog != null) { + watchdog.start(process); + } + waitFor(process); - // remove the process to the list of those to destroy if the VM exits - // - processDestroyer.remove(process); + if (watchdog != null) { + watchdog.stop(); + } + streamHandler.stop(); - if (watchdog != null) { - watchdog.stop(); - } - streamHandler.stop(); - if (watchdog != null) { - watchdog.checkException(); + if (watchdog != null) { + watchdog.checkException(); + } + return getExitValue(); + } finally { + // remove the process to the list of those to destroy if the VM exits + // + processDestroyer.remove(process); } - return getExitValue(); } protected void waitFor(Process process) { diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecuteWatchdog.java b/src/main/org/apache/tools/ant/taskdefs/ExecuteWatchdog.java index d429db830..01735d225 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ExecuteWatchdog.java +++ b/src/main/org/apache/tools/ant/taskdefs/ExecuteWatchdog.java @@ -153,7 +153,7 @@ public class ExecuteWatchdog implements TimeoutObserver { // We must check if the process was not stopped // before being here process.exitValue(); - } catch (IllegalThreadStateException itse){ + } catch (IllegalThreadStateException itse) { // the process is not terminated, if this is really // a timeout and not a manual stop then kill it. if (watch){ diff --git a/src/main/org/apache/tools/ant/taskdefs/ProcessDestroyer.java b/src/main/org/apache/tools/ant/taskdefs/ProcessDestroyer.java index a1cb84203..20a1cc16f 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ProcessDestroyer.java +++ b/src/main/org/apache/tools/ant/taskdefs/ProcessDestroyer.java @@ -214,6 +214,7 @@ class ProcessDestroyer implements Runnable { synchronized (processes) { boolean processRemoved = processes.removeElement(process); if (processes.size() == 0) { + processes.notify(); removeShutdownHook(); } return processRemoved; @@ -229,6 +230,13 @@ class ProcessDestroyer implements Runnable { while (e.hasMoreElements()) { ((Process) e.nextElement()).destroy(); } + + try { + // wait for all processes to finish + processes.wait(); + } catch (InterruptedException interrupt) { + // ignore + } } } } diff --git a/src/main/org/apache/tools/ant/util/Watchdog.java b/src/main/org/apache/tools/ant/util/Watchdog.java index 7ec81678f..cc7836679 100644 --- a/src/main/org/apache/tools/ant/util/Watchdog.java +++ b/src/main/org/apache/tools/ant/util/Watchdog.java @@ -114,7 +114,8 @@ public class Watchdog implements Runnable { while (!stopped && until > (now = System.currentTimeMillis())) { try { wait(until - now); - } catch (InterruptedException e) {} + } catch (InterruptedException e) { + } } if (!stopped) { fireTimeoutOccured();