Register shutdown hook so it can tidy up a partial refresh

Previously, the shutdown hook was only registered once refresh has
completed. If the JVM was shut down during refresh (or after refresh
and before the hook was registered) the hook wouldn't run and the
partially refreshed context would not be cleaned up.

This commit moves the registration of the shutdown hook to before
refresh processing begins. This ensures that the hook is available
to clean up the context if the JVM is shutdown while refresh is in
progress.

Fixes gh-23625
pull/23820/head
Andy Wilkinson 4 years ago
parent f43970e6e0
commit 2a40bd7853

@ -394,7 +394,6 @@ public class SpringApplication {
} }
private void refreshContext(ConfigurableApplicationContext context) { private void refreshContext(ConfigurableApplicationContext context) {
refresh(context);
if (this.registerShutdownHook) { if (this.registerShutdownHook) {
try { try {
context.registerShutdownHook(); context.registerShutdownHook();
@ -403,6 +402,7 @@ public class SpringApplication {
// Not allowed in some environments. // Not allowed in some environments.
} }
} }
refresh(context);
} }
private void configureHeadlessProperty() { private void configureHeadlessProperty() {

Loading…
Cancel
Save