From 2a40bd78532049038f4fd6c507244b00c43f643e Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 19 Oct 2020 09:16:07 +0100 Subject: [PATCH] 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 --- .../main/java/org/springframework/boot/SpringApplication.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 5ca1aa695eb..8ff49fc9fd8 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -394,7 +394,6 @@ public class SpringApplication { } private void refreshContext(ConfigurableApplicationContext context) { - refresh(context); if (this.registerShutdownHook) { try { context.registerShutdownHook(); @@ -403,6 +402,7 @@ public class SpringApplication { // Not allowed in some environments. } } + refresh(context); } private void configureHeadlessProperty() {