Bug 546755 - Race condition: foreground ant run in separate JVM can
block forever

The termination can happen after the initial terminated check and before
registering the listener. In this case the delegate miss the terminate
and waits until launch is canceled.

Change-Id: I633d396fb305f6defa6dd8bc759785e871a6a42c
Signed-off-by: Paul Pazderski <paul-eclipse@ppazderski.de>
diff --git a/ant/org.eclipse.ant.launching/META-INF/MANIFEST.MF b/ant/org.eclipse.ant.launching/META-INF/MANIFEST.MF
index 6a58697..97ffed2 100644
--- a/ant/org.eclipse.ant.launching/META-INF/MANIFEST.MF
+++ b/ant/org.eclipse.ant.launching/META-INF/MANIFEST.MF
@@ -3,7 +3,7 @@
 Bundle-Localization: plugin
 Bundle-Name: %pluginName
 Bundle-SymbolicName: org.eclipse.ant.launching;singleton:=true
-Bundle-Version: 1.2.400.qualifier
+Bundle-Version: 1.2.500.qualifier
 Bundle-Activator: org.eclipse.ant.internal.launching.AntLaunching
 Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.5.0,4.0.0)",
  org.eclipse.debug.core;bundle-version="[3.12.0,4.0.0)",
diff --git a/ant/org.eclipse.ant.launching/pom.xml b/ant/org.eclipse.ant.launching/pom.xml
index a6b09a0..20a5069 100644
--- a/ant/org.eclipse.ant.launching/pom.xml
+++ b/ant/org.eclipse.ant.launching/pom.xml
@@ -19,7 +19,7 @@
   </parent>
   <groupId>org.eclipse.ant</groupId>
   <artifactId>org.eclipse.ant.launching</artifactId>
-  <version>1.2.400-SNAPSHOT</version>
+  <version>1.2.500-SNAPSHOT</version>
   <packaging>eclipse-plugin</packaging>
   <properties>
     <defaultSigning-excludeInnerJars>true</defaultSigning-excludeInnerJars>
diff --git a/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/AntLaunchDelegate.java b/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/AntLaunchDelegate.java
index f33a35b..96d20f9 100644
--- a/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/AntLaunchDelegate.java
+++ b/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/AntLaunchDelegate.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2017 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -25,6 +25,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.tools.ant.ProjectHelper;
 import org.eclipse.ant.core.AntCorePlugin;
@@ -594,8 +595,7 @@
 				refresher.startBackgroundRefresh();
 			}
 		} else {
-			final boolean[] terminated = new boolean[1];
-			terminated[0] = launch.isTerminated();
+			final AtomicBoolean terminated = new AtomicBoolean(false);
 			IDebugEventSetListener listener = new IDebugEventSetListener() {
 				@Override
 				public void handleDebugEvents(DebugEvent[] events) {
@@ -603,7 +603,7 @@
 						DebugEvent event = events[i];
 						for (int j = 0, numProcesses = processes.length; j < numProcesses; j++) {
 							if (event.getSource() == processes[j] && event.getKind() == DebugEvent.TERMINATE) {
-								terminated[0] = true;
+								terminated.set(true);
 								break;
 							}
 						}
@@ -611,8 +611,9 @@
 				}
 			};
 			DebugPlugin.getDefault().addDebugEventListener(listener);
+			terminated.compareAndSet(false, launch.isTerminated());
 			monitor.subTask(AntLaunchConfigurationMessages.AntLaunchDelegate_28);
-			while (!monitor.isCanceled() && !terminated[0]) {
+			while (!monitor.isCanceled() && !terminated.get()) {
 				try {
 					Thread.sleep(50);
 				}