Ensure shutdown hook is registered when a handler is added

Closes gh-34627
This commit is contained in:
Andy Wilkinson 2023-04-18 18:19:07 +01:00
parent 3dac258aa8
commit 28b7c583a9
2 changed files with 10 additions and 0 deletions

View File

@ -170,6 +170,7 @@ class SpringApplicationShutdownHook implements Runnable {
@Override
public void add(Runnable action) {
Assert.notNull(action, "Action must not be null");
addRuntimeShutdownHookIfNecessary();
synchronized (SpringApplicationShutdownHook.class) {
assertNotInProgress();
this.actions.add(action);

View File

@ -57,6 +57,15 @@ class SpringApplicationShutdownHookTests {
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isTrue();
}
@Test
void shutdownHookIsNotAddedUntilHandlerIsRegistered() {
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isFalse();
shutdownHook.getHandlers().add(() -> {
});
assertThat(shutdownHook.isRuntimeShutdownHookAdded()).isTrue();
}
@Test
void runClosesContextsBeforeRunningHandlerActions() {
TestSpringApplicationShutdownHook shutdownHook = new TestSpringApplicationShutdownHook();