fix(lifecycle): run shutdown hooks before HTTP server stops (#431) (#519)

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:
TiM authored and GitHub committed 2026-04-19 07:49:00 -05:00
1 parent 4719269df4
commit 6f12f78201
4 files changed
+118 -13

No files matched your search

@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheProxyMan
import io.github.hectorvent.floci.services.rds.container.RdsContainerManager;
import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager;
import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.ShutdownDelayInitiatedEvent;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;
import io.quarkus.vertx.http.HttpServerStart;
@@ -83,23 +84,29 @@ public class EmulatorLifecycle {
}
}
void onStop(@Observes ShutdownEvent ignored) throws IOException, InterruptedException {
void onPreShutdown(@Observes ShutdownDelayInitiatedEvent ignored) {
LOG.info("=== AWS Local Emulator Shutting Down ===");
// Log-and-continue for every failure mode. Resource cleanup in onStop() must still run,
// and cleanup routines (proxy/container/storage shutdown) must not see an interrupted
// thread, so we intentionally do NOT restore the interrupt flag here.
try {
initializationHooksRunner.run(InitializationHook.STOP);
} catch (IOException | InterruptedException e) {
} catch (InterruptedException e) {
LOG.error("Shutdown hook execution interrupted", e);
} catch (IOException e) {
LOG.error("Shutdown hook execution failed", e);
throw e;
} catch (RuntimeException e) {
LOG.error("Shutdown hook script failed", e);
} finally {
elastiCacheProxyManager.stopAll();
rdsProxyManager.stopAll();
elastiCacheContainerManager.stopAll();
rdsContainerManager.stopAll();
storageFactory.shutdownAll();
}
}
void onStop(@Observes ShutdownEvent ignored) {
elastiCacheProxyManager.stopAll();
rdsProxyManager.stopAll();
elastiCacheContainerManager.stopAll();
rdsContainerManager.stopAll();
storageFactory.shutdownAll();
LOG.info("=== AWS Local Emulator Stopped ===");
}