I'm trying to add support for signals (especially for Ctrl+C).
My Tool is written in Java and I would like to perform cleanup when Ctrl+C
is caught.
My main file is Application and there is the following peace of code:
if (ArgDefinitions.getInstance().hasOption(ArgNames.EXECUTE)) {
performShutdownHooks();
preformRun();
}
The Application parses the user's options and runs the proper method. So when the user uses the execute
option and clicks Ctrl+C
, I would like the program to stop and clean the area.
I added the performShutdownHooks
method in order to handle the signal and it looks as follows:
private void performShutdownHooks() {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
performCleanup();
}
});
}
It does what I want - if I run the tool and kill it while it runs, it does the cleanup (it runs a special command in the background). The problem comes up when I don't stop it. I get the following exception:
Exception in thread "Thread-2" java.lang.IllegalStateException: Job manager has been shut down.
at org.eclipse.core.internal.jobs.JobManager.schedule(JobManager.java:1104)
at org.eclipse.core.internal.jobs.InternalJob.schedule(InternalJob.java:427)
at org.eclipse.core.runtime.jobs.Job.schedule(Job.java:436)
at glichautil.CommandExecutor.runCommandInBackground(CommandExecutor.java:134)
at glicha.core.Application.performCleanup(Application.java:869)
at glicha.core.Application.access$0(Application.java:838)
at glicha.core.Application$1.run(Application.java:985)
I think that the performShutdownHooks
runs at the end, even though I didn't try to kill it.
Maybe something else kills it but it shouldn't because if I'll comment out the method call performShutdownHooks()
, it works regular (without throwing any exceptions).
That makes me believe that, for some reason, the method performShutdownHooks
is being run even though I didn't press Ctrl+C
.
Is there something I need to add to the performShutdownHooks
method in order to solve it? Maybe I missed something about addShutdownHook
?
If this is the right explanation of the issue, then I think that if I could somehow unmake it run at the end, then it would solve the problem.
It could be also possible that some other threads that being used in the code are being kill and for some reason execute that method.
EDIT: I think that I have manged to understand why it works like this. Before entering my ShutdownHooks
method, it prints:
Job found still running after platform shutdown.
Jobs should be canceled by the plugin that scheduled them during shutdown: glicha.testmanager.HandleParallelJobs.
I guess, this what invokes ShutdownHooks
. The problem is, I don't have an idea how to solve it. I read from the docs:
The Java virtual machine shuts down in response to two kinds of events:
The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or
The virtual machine is terminated in response to a user interrupt, such as typing Ctrl+C, or a system-wide event, such as user logoff or system shutdown.
So I guess, that when the plugin shuts down, it invokes ShutdownHooks
(correct me if I'm wrong). The issue is that I don't understand how I can separate between those two possibilities. I would like to catch only the signals and perform cleanup but I don't want to do it if one of the the VM shutsdown. Any ideas on how I should approach this issue?
Glicha
is the name of the tool.performClenup
stops all running jobs.Schedule
is a 3rd part framework (Maybe even built-in in Java). – Delorisdelorme