fix(lifecycle): run every shutdown step even when one of them fails (#3472)

EmulatorLifecycle.onStop ran its cleanup steps as one straight sequence. A
RuntimeException from any step skipped everything after it, including the
final storage shutdown, so WAL and hybrid stores were left unflushed and
open whenever a proxy, container or UI manager failed to stop.

Each step now runs in isolation: a failure is logged with its stack trace
and the resource name, the remaining steps still run, and storage shutdown
always runs last. The step order is unchanged.
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-12 13:18:48 -04:00
1 parent 129ba0a260
commit 3bdb019e96
2 files changed
+63 -19

No files matched your search

@@ -302,24 +302,26 @@ public class EmulatorLifecycle {
// SIGTERM grace window and trigger SIGKILL; if the flush ran last it would be skipped and
// in-memory (hybrid) data would be lost on an otherwise-graceful shutdown. shutdownAll()
// still runs at the end to stop the flush schedulers and capture any shutdown-time writes.
storageFactory.flushAll();
if (config.services().ec2().enabled() && !config.services().ec2().mock()) {
ec2MetadataServer.stop();
}
elastiCacheProxyManager.stopAll();
rdsProxyManager.stopAll();
memoryDbProxyManager.stopAll();
neptuneProxyManager.stopAll();
elastiCacheContainerManager.stopAll();
elastiCacheMemcachedContainerManager.stopAll();
rdsContainerManager.stopAll();
memoryDbContainerManager.stopAll();
docDbContainerManager.stopAll();
neptuneContainerManager.stopAll();
rabbitMqManager.stopAll();
flinkContainerManager.stopAll();
ecrRegistryManager.shutdown();
flociUiManager.shutdown();
runCleanup("storage flush", storageFactory::flushAll);
runCleanup("EC2 metadata server", () -> {
if (config.services().ec2().enabled() && !config.services().ec2().mock()) {
ec2MetadataServer.stop();
}
});
runCleanup("ElastiCache proxy", elastiCacheProxyManager::stopAll);
runCleanup("RDS proxy", rdsProxyManager::stopAll);
runCleanup("MemoryDB proxy", memoryDbProxyManager::stopAll);
runCleanup("Neptune proxy", neptuneProxyManager::stopAll);
runCleanup("ElastiCache container", elastiCacheContainerManager::stopAll);
runCleanup("ElastiCache Memcached container", elastiCacheMemcachedContainerManager::stopAll);
runCleanup("RDS container", rdsContainerManager::stopAll);
runCleanup("MemoryDB container", memoryDbContainerManager::stopAll);
runCleanup("DocDB container", docDbContainerManager::stopAll);
runCleanup("Neptune container", neptuneContainerManager::stopAll);
runCleanup("RabbitMQ", rabbitMqManager::stopAll);
runCleanup("Flink", flinkContainerManager::stopAll);
runCleanup("ECR registry", ecrRegistryManager::shutdown);
runCleanup("Floci UI", flociUiManager::shutdown);
// Centralized teardown for process-bound containers (Lambda warm pool, ECS tasks,
// EC2 instances, in-flight build/job containers). Runs before shutdownAll() so any
// state written while stopping is captured by the final flush.
@@ -331,8 +333,16 @@ public class EmulatorLifecycle {
teardown.getClass().getSimpleName(), e.getMessage());
}
}
storageFactory.shutdownAll();
runCleanup("storage shutdown", storageFactory::shutdownAll);
LOG.info("=== AWS Local Emulator Stopped ===");
}
private void runCleanup(String resource, Runnable cleanup) {
try {
cleanup.run();
} catch (RuntimeException e) {
LOG.warnv(e, "Shutdown cleanup failed for {0}; continuing with the remaining steps", resource);
}
}
}
@@ -443,6 +443,40 @@ class EmulatorLifecycleTest {
inOrder.verify(storageFactory).shutdownAll();
}
@Test
@DisplayName("Should continue cleanup and shut down storage when the initial flush fails")
void shouldContinueCleanupWhenInitialFlushFails() {
doThrow(new IllegalStateException("flush failed")).when(storageFactory).flushAll();
emulatorLifecycle.onStop(Mockito.mock(ShutdownEvent.class));
verify(elastiCacheProxyManager).stopAll();
verify(rdsProxyManager).stopAll();
verify(storageFactory).shutdownAll();
}
@Test
@DisplayName("Should continue cleanup after a middle resource cleanup fails")
void shouldContinueCleanupAfterMiddleResourceFailure() {
doThrow(new IllegalStateException("proxy failed")).when(rdsProxyManager).stopAll();
emulatorLifecycle.onStop(Mockito.mock(ShutdownEvent.class));
verify(memoryDbProxyManager).stopAll();
verify(neptuneProxyManager).stopAll();
verify(storageFactory).shutdownAll();
}
@Test
@DisplayName("Should shut down storage when a late resource cleanup fails")
void shouldShutDownStorageAfterLateResourceFailure() {
doThrow(new IllegalStateException("UI shutdown failed")).when(flociUiManager).shutdown();
emulatorLifecycle.onStop(Mockito.mock(ShutdownEvent.class));
verify(storageFactory).shutdownAll();
}
@Test
@DisplayName("Should still run full resource cleanup when a pre-shutdown hook fails")
void shouldRunFullCleanupAfterFailingPreShutdownHook() throws IOException, InterruptedException {