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.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-16 19:19:37 -04:00
1 parent 996a680cdc
commit e32f762899
4 files changed
+43 -29

No files matched your search

@@ -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.
*
* <p>{@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.
*
* <p>Implementations must be idempotent; they may also be invoked from {@code @PreDestroy}
* as a fallback.
@@ -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<ContainerTeardown> teardowns, Logger log) {
for (ContainerTeardown teardown : teardowns) {
try {
teardown.stopManagedContainers();
} catch (Exception e) {
log.warnv("Container teardown failed for {0}: {1}",
teardown.getClass().getSimpleName(), e.getMessage());
}
}
}
}
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.ContainerCaBundle;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.config.FlociCertificateAuthority;
import io.github.hectorvent.floci.core.common.ContainerTeardown;
import io.github.hectorvent.floci.core.common.ContainerTeardowns;
import io.github.hectorvent.floci.core.common.ServiceRegistry;
import io.github.hectorvent.floci.lifecycle.inithook.InitializationHook;
import jakarta.inject.Inject;
@@ -157,16 +158,9 @@ public class EmulatorInfoController {
// in any order relative to the storage wipe below, but stopping them here means a
// client's reset actually reflects a clean slate instead of leaving Batch, CodeBuild,
// or SageMaker containers running with no record of them left in the store.
for (ContainerTeardown teardown : containerTeardowns) {
try {
teardown.stopManagedContainers();
} catch (Exception e) {
LOG.warnv("Container teardown failed for {0}: {1}",
teardown.getClass().getSimpleName(), e.getMessage());
}
}
// Storage first. Services re-create their bootstrap state in clear(), and a wipe
// afterwards would remove it again until the next restart.
ContainerTeardowns.stopAll(containerTeardowns, LOG);
// Storage before resettables: services re-create their bootstrap state in clear(), and
// a wipe afterwards would remove it again until the next restart.
storageFactory.clearAll();
for (Resettable r : resettables) {
r.clear();
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.lifecycle;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.ContainerTeardown;
import io.github.hectorvent.floci.core.common.ContainerTeardowns;
import io.github.hectorvent.floci.core.common.ServiceRegistry;
import io.github.hectorvent.floci.core.storage.PersistentPathValidator;
import io.github.hectorvent.floci.core.storage.StorageFactory;
@@ -329,14 +330,7 @@ public class EmulatorLifecycle {
// 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.
for (ContainerTeardown teardown : containerTeardowns) {
try {
teardown.stopManagedContainers();
} catch (Exception e) {
LOG.warnv("Container teardown failed for {0}: {1}",
teardown.getClass().getSimpleName(), e.getMessage());
}
}
ContainerTeardowns.stopAll(containerTeardowns, LOG);
runCleanup("storage shutdown", storageFactory::shutdownAll);
LOG.info("=== AWS Local Emulator Stopped ===");