Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shutdown Hooks Not Running #20

Open
darkfrog26 opened this issue Jan 28, 2013 · 10 comments
Open

Shutdown Hooks Not Running #20

darkfrog26 opened this issue Jan 28, 2013 · 10 comments

Comments

@darkfrog26
Copy link

My configuration is fairly simple, but at startup I add a shutdown hook:

Runtime.getRuntime.addShutdownHook(...)

However, at re-stop (or ~re-start code change) the hook is never fired. The documentation explicitly states this is how shutdown should be handled. Is there additional configuration I need to specify to make those hooks fire?

@jrudolph
Copy link
Member

jrudolph commented Jan 29, 2013

We only have very coarse control over the spawned process. To stop it we use Process.destroy which I guess is equivalent to kill -9.

The first thing to do proper shutdown would be a platform independent way to signal a termination wish to a process. Do you know if there's a Java library doing this? (like using kill -SIGTERM in linux/unix, taskkill in windows, etc.)

@jrudolph
Copy link
Member

Another possibility would be to allow a user-defined way of closing an application and leave the current force-close behavior as default or fallback.

@magnusart
Copy link

I have this problem as well. Shutdown is not handled correctly and then a new process is spawned but fails because it can't bind to the port that is taken. I have to shut down SBT every time I want to restart. My setup is a multi project setup. Even when I try to run re-stop it doesn't shut down the processes correctly.

@jrudolph
Copy link
Member

jrudolph commented Jun 3, 2013

Magnus, this sounds like quite the opposite problem: where the original issue was about the process being closed to eagerly so that the shutdown hooks are not run, what you are experiencing seems to be that Process.destroy is not working for some reason. Can you open a new issue for that and post an sbt console log exhibiting the problem?

@magnusart
Copy link

You're correct. I found that my issue was better described in the other bug and that fix seems to have made the trick for my issue. Thank you.

@jrudolph
Copy link
Member

Closing...

@yanns
Copy link

yanns commented Apr 5, 2017

Maybe we can let this issue opened, as it is present.

We could try a proper shutdown, then wait a bit and then kill the process if it's still there.
There does not seem to be any java lib to handle a proper shutdown.
A possibility seen on http://stackoverflow.com/questions/6356340/killing-a-process-using-java:

 Runtime rt = Runtime.getRuntime();
  if (System.getProperty("os.name").toLowerCase().indexOf("windows") > -1) 
     rt.exec("taskkill " +....);
   else
     rt.exec("kill -9 " +....);

@jrudolph
Copy link
Member

Please open a new ticket if you have a concrete idea how to fix this. As written above, the current behavior is currently equivalent to kill -9. What would be nice if there would be an equivalent of kill -15 instead. The problem with your example is the ..... The java.lang.Process API has no access to the PID so we cannot call the external kill. (Using reflection tricks might get us there, though.)

@dwickern
Copy link

I don't this problem on UNIX/OSX because Process.destroy() sends a SIGTERM. Note that the I/O redirection stops so you won't see any stdout/stderr from your shutdown hook, but the hook is getting called.

I do have this problem on Windows. Process.destroy() calls the TerminateProcess Windows API which is like a kill -9. There's no workaround as far as I know. The way to gracefully stop a process on Windows is to send a WM_CLOSE message but that won't work for a command-line application.

@jrudolph jrudolph reopened this Sep 12, 2019
@jrudolph
Copy link
Member

@dwickern that's interesting, since that behavior seems to have changed for Unix only with Java 8. Since then there are two methods destroy and destroyForcibly, so the original behavior of destroy should do now what was asked for in this ticket originally.

That said, it seems you are also right about Windows. Should we keep that open as a reminder that it still doesn't work on Windows?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants