From e32f762899e4d6e48cc561320e6302bb12a52a57 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Wed, 16 Sep 2026 19:19:37 -0400 Subject: [PATCH] refactor(lifecycle): dedupe container teardown loop between shutdown and reset (#3718) (#3785) EmulatorLifecycle.onStop and EmulatorInfoController's /state/reset and /state/nuke each independently ran an identical loop over every ContainerTeardown implementation, log text included (flagged on #3718). Extracted the loop into ContainerTeardowns.stopAll(), following the same pattern as the recent cron/SigV4/SSRF dedupes. Callers keep passing their own LOG so the log category stays tied to the calling class instead of shifting to ContainerTeardowns. Also fixed a stale comment in EmulatorInfoController: "Storage first" sat under a "Containers first" comment that now runs before it; it means "Storage before resettables" today. No behavior change, so no new tests: EmulatorInfoControllerTest already covers the ordering and per-teardown failure tolerance this loop provides. --- .../floci/core/common/ContainerTeardown.java | 21 +++++++-------- .../floci/core/common/ContainerTeardowns.java | 27 +++++++++++++++++++ .../lifecycle/EmulatorInfoController.java | 14 +++------- .../floci/lifecycle/EmulatorLifecycle.java | 10 ++----- 4 files changed, 43 insertions(+), 29 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/core/common/ContainerTeardowns.java diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ContainerTeardown.java b/src/main/java/io/github/hectorvent/floci/core/common/ContainerTeardown.java index 71872cf079..b0964d15b5 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/ContainerTeardown.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/ContainerTeardown.java @@ -3,17 +3,16 @@ package io.github.hectorvent.floci.core.common; /** * Implemented by services that launch Docker containers whose lifetime is bound to the * emulator process (Lambda warm pool, ECS tasks, EC2 instances, in-flight build/job - * containers). {@code EmulatorLifecycle.onStop} invokes every implementation during the - * ShutdownEvent phase, before {@code StorageFactory.shutdownAll()}, so teardown runs at - * a deterministic point and state changes made while stopping are still captured by the - * final flush. A {@code @PreDestroy} alone is not sufficient for that: bean destruction - * runs after the ShutdownEvent observers, when the storage flush schedulers are already - * stopped. - * - *
{@code EmulatorInfoController} also invokes every implementation on {@code /state/reset} - * and {@code /state/nuke}, since those wipe the same tracking state these containers were - * started against; without this, a reset leaves them running with nothing left to reconcile - * them against. + * containers). {@link ContainerTeardowns#stopAll} runs every implementation, tolerating + * individual failures, and is invoked from two call sites: {@code EmulatorLifecycle.onStop} + * during the ShutdownEvent phase, before {@code StorageFactory.shutdownAll()}, so teardown + * runs at a deterministic point and state changes made while stopping are still captured + * by the final flush; and {@code EmulatorInfoController} on {@code /state/reset} and + * {@code /state/nuke}, since those wipe the same tracking state these containers were + * started against, and without this a reset would leave them running with nothing left + * to reconcile them against. A {@code @PreDestroy} alone is not sufficient for the + * shutdown case: bean destruction runs after the ShutdownEvent observers, when the + * storage flush schedulers are already stopped. * *
Implementations must be idempotent; they may also be invoked from {@code @PreDestroy}
* as a fallback.
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ContainerTeardowns.java b/src/main/java/io/github/hectorvent/floci/core/common/ContainerTeardowns.java
new file mode 100644
index 0000000000..22a4ed9626
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/core/common/ContainerTeardowns.java
@@ -0,0 +1,27 @@
+package io.github.hectorvent.floci.core.common;
+
+import jakarta.enterprise.inject.Instance;
+import org.jboss.logging.Logger;
+
+/**
+ * Runs every {@link ContainerTeardown} implementation, tolerating a failure in one so it
+ * doesn't block the others or whatever cleanup the caller runs next. {@code EmulatorLifecycle}
+ * (on {@code ShutdownEvent}) and {@code EmulatorInfoController} (on {@code /state/reset} and
+ * {@code /state/nuke}) each independently ran this identical loop before this class existed.
+ */
+public final class ContainerTeardowns {
+
+ private ContainerTeardowns() {
+ }
+
+ public static void stopAll(Instance