Questions tagged [shutdown-hook]

A shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently.

public void addShutdownHook(Thread hook)

Registers a new virtual-machine shutdown hook.

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 ^C, or a system-wide event, such as user logoff or system shutdown. A shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently. When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled. Finally, the virtual machine will halt. Note that daemon threads will continue to run during the shutdown sequence, as will non-daemon threads if shutdown was initiated by invoking the exit method.

Once the shutdown sequence has begun it can be stopped only by invoking the halt method, which forcibly terminates the virtual machine.

Once the shutdown sequence has begun it is impossible to register a new shutdown hook or de-register a previously-registered hook. Attempting either of these operations will cause an IllegalStateException to be thrown.

Shutdown hooks run at a delicate time in the life cycle of a virtual machine and should therefore be coded defensively. They should, in particular, be written to be thread-safe and to avoid deadlocks insofar as possible. They should also not rely blindly upon services that may have registered their own shutdown hooks and therefore may themselves in the process of shutting down. Attempts to use other thread-based services such as the AWT event-dispatch thread, for example, may lead to deadlocks.

Shutdown hooks should also finish their work quickly. When a program invokes exit the expectation is that the virtual machine will promptly shut down and exit. When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook.

Uncaught exceptions are handled in shutdown hooks just as in any other thread, by invoking the uncaughtException method of the thread's ThreadGroup object. The default implementation of this method prints the exception's stack trace to System.err and terminates the thread; it does not cause the virtual machine to exit or halt.

In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.

220 questions
13
votes
1 answer

What happens if System.exit is called from a shutdown hook?

I have a rather complicated shutdown in Java - there's a lot of clean up work to do. In particular I'm trying to figure out how to error handle from a shutdown hook thread. My code includes this currently: try { return shutdownPromise =…
djechlin
  • 59,258
  • 35
  • 162
  • 290
12
votes
1 answer

Library shutdown routine that works well in a "normal" Java application and in a web application

I maintain a JDBC driver that also has an embedded database server mode provided through a native library, which is accessed through JNA. The shutdown process, done as part of unloading the native library itself, runs into problems on Windows due to…
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
11
votes
1 answer

Programmatically disabling shutdown hook in log4j 2

It's possible to disable shutdown hooks in log4j2 via configuration: Is it possible to do so programmatically?
Justin Wong
  • 1,455
  • 3
  • 18
  • 25
8
votes
1 answer

Java deleteOnExit and addShutdownHook - which comes first?

I have a number of temporary files that I call deleteOnExit() on. I also have a runnable that I register with addShutdownHook() to run when System.exit is called. Is there any guarantee that the temporary files will still be available when my…
Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70
8
votes
3 answers

java shutdown hook with more than one thread

I am trying to get a shutdown hook to work on my ubuntu server, however I seem to have an issue with more than one thread. Using the a basic ShutdownHook, the following bit of code does work when I kill the process using kill , meaning the…
Reese
  • 173
  • 3
  • 7
7
votes
2 answers

Catching Ctrl+C signal throws exception "Job manager has been shut down"

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…
vesii
  • 2,760
  • 4
  • 25
  • 71
7
votes
1 answer

Controlling Spring Boot graceful shutdown

Is there a way in Spring Boot to control the graceful shutdown of the app? I know that you can have @PreDestroy methods in beans, but how can you control the ordering in which those @PreDestroy methods are called? You can have multiple beans…
simonC
  • 4,101
  • 10
  • 50
  • 78
7
votes
2 answers

How to get return code in shutdown hook

I need to modify JVM return code according to my application result. But it is risky to explicitly call System.exit(code) coz the application is complicated and it is hard to identify the end of running threads. So I come up with using shutdown hook…
Leslie Wu
  • 760
  • 3
  • 13
  • 29
7
votes
0 answers

Spark issue with "Shutdown hook" being called before final status is reported (Closed)

I'm trying to run spark on a working hadoop cluster. When I run my python job with a small dataset size, everything seems to work fine. However when I use a larger dataset, the task fails and in the hadoop resource manager I get the diagnostic:…
7
votes
2 answers

Dropwizard Shutdown Hook

The problem is, that I stop Dropwizard application (via ctrl + c) and I have inserted a Shutdown Hook in main class to do some stuff before shutdown. But now ServerConnector for the application is closed before I can do what I want to do. There is…
heaphach
  • 1,492
  • 1
  • 20
  • 44
7
votes
1 answer

Is it safe to send SIGTERM to JVM

Although JVM will translate SIGTERM and similar signals to shutdown hooks, many service shutdown scripts use a TCP port to initiate a shutdown. (e.g. Tomcat's shutdown port, Java Service Wrapper, JBoss' management interfaces, etc.) So I thought…
lyomi
  • 4,230
  • 6
  • 30
  • 39
6
votes
2 answers

Wait for threads to write changes in the shutdownhook

I Have a shutdownhook, that is executed when the process is terminated. However the changes that the program makes to the h2 database are not persistent. Only if I let the shutdown thread wait some time after the commit, I will see the changes in…
morja
  • 8,297
  • 2
  • 39
  • 59
6
votes
4 answers

how to check if exit is clean in a shutdown function in PHP?

How can I test if exit is clean in a shutdown function in PHP? By clean exit I mean that the script was not terminated due to an error.
agsamek
  • 8,734
  • 11
  • 36
  • 43
6
votes
2 answers

Node.JS Shutdown Hook

Is it possible to intercept the default kill signal and use it as a command for a graceful shutdown? This is for Solaris SMF. The easiest way to have a stoppable service that I have found is to set :kill as the shutdown script and then to add a…
700 Software
  • 85,281
  • 83
  • 234
  • 341
6
votes
2 answers

How does a JVM running multiple threads handle ctrl-c, w/ and w/o shutdown hooks?

Could not find this answer online. When Ctrl+C is hit: When we don't have any shutdown hook, what happens to the running threads - do they each get hit with an InterruptedException? When we have shutdown hook(s), I know that the shutdown hooks get…
jwayne
  • 706
  • 2
  • 7
  • 20
1
2
3
14 15