I've been bit a few times by a java process that wouldn't cooperate and exit cleanly (it would be buried in some low-level libraries out of my control). I am now testing a sort of watchdog that implements a hard stop for the process at some pre-established time, ScheduledStop
. That watchdog is a singleton class that runs an independent thread that will kill the whole process if the scheduled stop time arrives. Normally, all threads should return nicely before that hard-stop time and the program exits gracefully. If necessary however, the process kills itself, file locks are released etc. All of this runs on Linux.
I seem to remember that even System.exit(0)
is not fool-proof (I think if some shutdown hooks are getting stuck, the process may stay alive), so I have concocted something along the line of:
int pid = MyUtil.getPID();
Runtime.getRuntime().exec(new String[]{"kill", "-9", String.valueOf(pid)});
Now, I'd like to test it with some really un-cooperative threads, and possibly some shutdown hooks that, on purpose for the test, are not doing well.
The itinial NastyThread below is not all that nasty... It ignores InterruptionException
, but doesn't prevent System.exit(0)
. How can I put my VM into a state that even exit()
doesn't terminate?
Another question is, although the watchdog thread is in theory independent, what are the conditions where other threads would completely preempt it, thus foiling the scheduled stop?
If necessary, I could launch a separate process (e.g. a simple perl script) that kills the parent (the java process) at some specified time.
/**
* A Runnable that runs forever and ignores InterruptedException.
*/
private static class NastyThread implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("Received and ignoring "+e);
System.out.flush();
}
System.out.println(ScheduledStop.getInstance().toString());
}
}
}