Skip to content

Commit 28b7c58

Browse files
committed
Ensure shutdown hook is registered when a handler is added
Closes gh-34627
1 parent 3dac258 commit 28b7c58

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplicationShutdownHook.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ private class Handlers implements SpringApplicationShutdownHandlers, Runnable {
170170
@Override
171171
public void add(Runnable action) {
172172
Assert.notNull(action, "Action must not be null");
173+
addRuntimeShutdownHookIfNecessary();
173174
synchronized (SpringApplicationShutdownHook.class) {
174175
assertNotInProgress();
175176
this.actions.add(action);

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationShutdownHookTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ void shutdownHookIsNotAddedUntilContextIsRegistered() {
5757
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isTrue();
5858
}
5959

60+
@Test
61+
void shutdownHookIsNotAddedUntilHandlerIsRegistered() {
62+
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();
63+
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isFalse();
64+
shutdownHook.getHandlers().add(() -> {
65+
});
66+
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isTrue();
67+
}
68+
6069
@Test
6170
void runClosesContextsBeforeRunningHandlerActions() {
6271
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();

0 commit comments

Comments
 (0)