You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@apache.org on 2004/05/27 09:06:28 UTC
cvs commit: ant/src/main/org/apache/tools/ant/taskdefs ProcessDestroyer.java
bodewig 2004/05/27 00:06:27
Modified: src/main/org/apache/tools/ant/taskdefs ProcessDestroyer.java
Log:
Don't try to add or remove shutdown hooks while the hook is running. Remove a processes.wait that won't ever return as far as I can tell.
Revision Changes Path
1.17 +35 -27 ant/src/main/org/apache/tools/ant/taskdefs/ProcessDestroyer.java
Index: ProcessDestroyer.java
===================================================================
RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/ProcessDestroyer.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- ProcessDestroyer.java 9 Mar 2004 16:48:06 -0000 1.16
+++ ProcessDestroyer.java 27 May 2004 07:06:27 -0000 1.17
@@ -38,6 +38,9 @@
// whether or not this ProcessDestroyer has been registered as a
// shutdown hook
private boolean added = false;
+ // whether or not this ProcessDestroyer is currently running as
+ // shutdown hook
+ private boolean running = false;
private class ProcessDestroyerImpl extends Thread {
private boolean shouldDestroy = true;
@@ -89,7 +92,7 @@
* uses reflection to ensure pre-JDK 1.3 compatibility.
*/
private void addShutdownHook() {
- if (addShutdownHookMethod != null) {
+ if (addShutdownHookMethod != null && !running) {
destroyProcessThread = new ProcessDestroyerImpl();
Object[] args = {destroyProcessThread};
try {
@@ -98,7 +101,13 @@
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
- e.printStackTrace();
+ Throwable t = e.getCause();
+ if (t != null && t.getClass() == IllegalStateException.class) {
+ // shutdown already is in progress
+ running = true;
+ } else {
+ e.printStackTrace();
+ }
}
}
}
@@ -108,7 +117,7 @@
* uses reflection to ensure pre-JDK 1.3 compatibility
*/
private void removeShutdownHook() {
- if (removeShutdownHookMethod != null && destroyProcessThread != null) {
+ if (removeShutdownHookMethod != null && added && !running) {
Object[] args = {destroyProcessThread};
try {
Boolean removed =
@@ -118,25 +127,31 @@
if (!removed.booleanValue()) {
System.err.println("Could not remove shutdown hook");
}
- // start the hook thread, a unstarted thread may not be
- // eligible for garbage collection
- // Cf.: http://developer.java.sun.com/developer/bugParade/bugs/4533087.html
- destroyProcessThread.setShouldDestroy(false);
- destroyProcessThread.start();
- // this should return quickly, since Process.destroy()
- try {
- destroyProcessThread.join(20000);
- } catch (InterruptedException ie) {
- // the thread didn't die in time
- // it should not kill any processes unexpectedly
- }
- destroyProcessThread = null;
- added = false;
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
- e.printStackTrace();
+ Throwable t = e.getCause();
+ if (t != null && t.getClass() == IllegalStateException.class) {
+ // shutdown already is in progress
+ running = true;
+ } else {
+ e.printStackTrace();
+ }
}
+ // start the hook thread, a unstarted thread may not be
+ // eligible for garbage collection
+ // Cf.: http://developer.java.sun.com/developer/bugParade/bugs/4533087.html
+ destroyProcessThread.setShouldDestroy(false);
+ destroyProcessThread.start();
+ // this should return quickly, since it basically is a NO-OP.
+ try {
+ destroyProcessThread.join(20000);
+ } catch (InterruptedException ie) {
+ // the thread didn't die in time
+ // it should not kill any processes unexpectedly
+ }
+ destroyProcessThread = null;
+ added = false;
}
}
@@ -179,8 +194,7 @@
public boolean remove(Process process) {
synchronized (processes) {
boolean processRemoved = processes.removeElement(process);
- if (processes.size() == 0) {
- processes.notifyAll();
+ if (processRemoved && processes.size() == 0) {
removeShutdownHook();
}
return processRemoved;
@@ -192,16 +206,10 @@
*/
public void run() {
synchronized (processes) {
+ running = true;
Enumeration e = processes.elements();
while (e.hasMoreElements()) {
((Process) e.nextElement()).destroy();
- }
-
- try {
- // wait for all processes to finish
- processes.wait();
- } catch (InterruptedException interrupt) {
- // ignore
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org
Re: cvs commit: ant/src/main/org/apache/tools/ant/taskdefs
ProcessDestroyer.java
Posted by Stefan Bodewig <bo...@apache.org>.
On Thu, 27 May 2004, Peter Reilly <pe...@corvil.com> wrote:
> This fixes bug 24181 correct?
I still don't think that it really does much (other than suppressing
the stack-trace). I plan to wait for the next Gump run (just to be
sure), then merge it over to the 1.6 branch and publish a
self-compiled ant.jar to give people a chance to test it.
We really need to get the nightly builds going again.
> - at least it fixes my nofin example.
If so, this is probably due to the removed processes.wait. The
shutdown hook was waiting for the last process to be removed frok its
internal list. I don't know why it did so and don't know why it
assumed that this would ever happen either.
Stefan
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org
Re: cvs commit: ant/src/main/org/apache/tools/ant/taskdefs ProcessDestroyer.java
Posted by Peter Reilly <pe...@corvil.com>.
bodewig@apache.org wrote:
>bodewig 2004/05/27 00:06:27
>
> Modified: src/main/org/apache/tools/ant/taskdefs ProcessDestroyer.java
> Log:
> Don't try to add or remove shutdown hooks while the hook is running. Remove a processes.wait that won't ever return as far as I can tell.
>
>
>
Brilliant,
This fixes bug 24181 correct?
- at least it fixes my nofin example.
Peter
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org