mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
Shutdown hooks could not reach Floci's HTTP endpoint (e.g. `aws --endpoint-url http://localhost:4566 ...`) because they ran in the ShutdownEvent observer, which Quarkus fires only AFTER the HTTP server has already stopped. Split the lifecycle: - onPreShutdown(@Observes ShutdownDelayInitiatedEvent) runs stop hooks while the HTTP server is still accepting connections. - onStop(@Observes ShutdownEvent) handles resource cleanup only. Enable the pre-shutdown phase via `quarkus.shutdown.delay-enabled: true`. No `delay` value is set, so observers run synchronously with no artificial sleep introduced. Swallow all hook failures (including InterruptedException) without restoring the interrupt flag, so the subsequent ShutdownEvent cleanup phase always runs with a clean thread state and resource shutdown is guaranteed even when hooks fail. Fixes floci-io/floci#431
This commit is contained in:
4 files changed
+118
-13
No files matched your search
@@ -9,6 +9,8 @@ import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheCont
|
||||
import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheProxyManager;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsContainerManager;
|
||||
import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager;
|
||||
import io.quarkus.runtime.ShutdownDelayInitiatedEvent;
|
||||
import io.quarkus.runtime.ShutdownEvent;
|
||||
import io.quarkus.runtime.StartupEvent;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
@@ -20,6 +22,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -41,18 +44,22 @@ class EmulatorLifecycleTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
when(config.storage()).thenReturn(storageConfig);
|
||||
when(storageConfig.mode()).thenReturn("in-memory");
|
||||
when(storageConfig.persistentPath()).thenReturn("/app/data");
|
||||
emulatorLifecycle = new EmulatorLifecycle(
|
||||
storageFactory, serviceRegistry, config,
|
||||
elastiCacheContainerManager, elastiCacheProxyManager,
|
||||
rdsContainerManager, rdsProxyManager, initializationHooksRunner);
|
||||
}
|
||||
|
||||
private void stubStorageConfig() {
|
||||
when(config.storage()).thenReturn(storageConfig);
|
||||
when(storageConfig.mode()).thenReturn("in-memory");
|
||||
when(storageConfig.persistentPath()).thenReturn("/app/data");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should log Ready immediately when no startup hooks exist")
|
||||
void shouldLogReadyImmediatelyWhenNoHooksExist() throws IOException, InterruptedException {
|
||||
stubStorageConfig();
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(false);
|
||||
|
||||
emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class));
|
||||
@@ -65,6 +72,7 @@ class EmulatorLifecycleTest {
|
||||
@Test
|
||||
@DisplayName("Should defer hook execution when startup hooks exist")
|
||||
void shouldDeferHookExecutionWhenHooksExist() throws IOException, InterruptedException {
|
||||
stubStorageConfig();
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(true);
|
||||
|
||||
emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class));
|
||||
@@ -74,4 +82,84 @@ class EmulatorLifecycleTest {
|
||||
// run() is NOT called synchronously — it will be called by the virtual thread
|
||||
verify(initializationHooksRunner, never()).run(InitializationHook.START);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should run shutdown hooks in the pre-shutdown phase, before the HTTP server stops")
|
||||
void shouldRunShutdownHooksInPreShutdownPhase() throws IOException, InterruptedException {
|
||||
emulatorLifecycle.onPreShutdown(Mockito.mock(ShutdownDelayInitiatedEvent.class));
|
||||
|
||||
verify(initializationHooksRunner).run(InitializationHook.STOP);
|
||||
// Resource cleanup must NOT happen in pre-shutdown; it belongs to ShutdownEvent.
|
||||
verify(storageFactory, never()).shutdownAll();
|
||||
verify(elastiCacheProxyManager, never()).stopAll();
|
||||
verify(rdsProxyManager, never()).stopAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should swallow RuntimeException from shutdown hook scripts")
|
||||
void shouldSwallowRuntimeExceptionFromShutdownHook() throws IOException, InterruptedException {
|
||||
doThrow(new IllegalStateException("boom")).when(initializationHooksRunner).run(InitializationHook.STOP);
|
||||
|
||||
emulatorLifecycle.onPreShutdown(Mockito.mock(ShutdownDelayInitiatedEvent.class));
|
||||
|
||||
verify(initializationHooksRunner).run(InitializationHook.STOP);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should swallow IOException from shutdown hook scripts so resource cleanup still runs")
|
||||
void shouldSwallowIOExceptionFromShutdownHook() throws IOException, InterruptedException {
|
||||
doThrow(new IOException("io")).when(initializationHooksRunner).run(InitializationHook.STOP);
|
||||
|
||||
emulatorLifecycle.onPreShutdown(Mockito.mock(ShutdownDelayInitiatedEvent.class));
|
||||
|
||||
verify(initializationHooksRunner).run(InitializationHook.STOP);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should swallow InterruptedException from shutdown hooks without poisoning cleanup thread")
|
||||
void shouldSwallowInterruptedExceptionWithoutPropagatingInterrupt() throws IOException, InterruptedException {
|
||||
doThrow(new InterruptedException("interrupted")).when(initializationHooksRunner).run(InitializationHook.STOP);
|
||||
|
||||
Thread.interrupted();
|
||||
try {
|
||||
emulatorLifecycle.onPreShutdown(Mockito.mock(ShutdownDelayInitiatedEvent.class));
|
||||
// The thread must NOT be left interrupted: ShutdownEvent cleanup runs next and
|
||||
// interruptible I/O inside stopAll()/shutdownAll() would short-circuit otherwise.
|
||||
org.junit.jupiter.api.Assertions.assertFalse(Thread.currentThread().isInterrupted(),
|
||||
"Interrupt flag must not leak into ShutdownEvent cleanup");
|
||||
} finally {
|
||||
Thread.interrupted();
|
||||
}
|
||||
verify(initializationHooksRunner).run(InitializationHook.STOP);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should clean up resources on ShutdownEvent without running hooks again")
|
||||
void shouldCleanUpResourcesOnShutdownWithoutRunningHooks() throws IOException, InterruptedException {
|
||||
emulatorLifecycle.onStop(Mockito.mock(ShutdownEvent.class));
|
||||
|
||||
verify(elastiCacheProxyManager).stopAll();
|
||||
verify(rdsProxyManager).stopAll();
|
||||
verify(elastiCacheContainerManager).stopAll();
|
||||
verify(rdsContainerManager).stopAll();
|
||||
verify(storageFactory).shutdownAll();
|
||||
// Hooks are handled by onPreShutdown, never from ShutdownEvent.
|
||||
verify(initializationHooksRunner, never()).run(InitializationHook.STOP);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should still run full resource cleanup when a pre-shutdown hook fails")
|
||||
void shouldRunFullCleanupAfterFailingPreShutdownHook() throws IOException, InterruptedException {
|
||||
doThrow(new IOException("hook blew up")).when(initializationHooksRunner).run(InitializationHook.STOP);
|
||||
|
||||
emulatorLifecycle.onPreShutdown(Mockito.mock(ShutdownDelayInitiatedEvent.class));
|
||||
emulatorLifecycle.onStop(Mockito.mock(ShutdownEvent.class));
|
||||
|
||||
verify(initializationHooksRunner).run(InitializationHook.STOP);
|
||||
verify(elastiCacheProxyManager).stopAll();
|
||||
verify(rdsProxyManager).stopAll();
|
||||
verify(elastiCacheContainerManager).stopAll();
|
||||
verify(rdsContainerManager).stopAll();
|
||||
verify(storageFactory).shutdownAll();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user