From ef12a83418c2092bf57e40647e383f16dc4adbbc Mon Sep 17 00:00:00 2001
From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com>
Date: Wed, 16 Sep 2026 18:33:40 -0400
Subject: [PATCH] fix(lifecycle): stop managed containers on state reset, not
just shutdown (#3718)
* fix(lifecycle): stop managed containers on state reset, not just shutdown
/state/reset and /state/nuke clear StorageBackend and every Resettable
bean, but ContainerTeardown was only invoked from EmulatorLifecycle.onStop.
A reset left Batch, CodeBuild, and now SageMaker training/endpoint
containers running with no record of them left in the store to reconcile
against. EmulatorInfoController now runs the same teardown loop before
wiping storage, tolerating individual failures the same way shutdown does.
* fix(lifecycle): use explicit InOrder type instead of var in test
AGENTS.md disallows var for local declarations; the concrete Mockito
type at a call site is the thing worth seeing in a review.
---
.../floci/core/common/ContainerTeardown.java | 7 +-
.../lifecycle/EmulatorInfoController.java | 19 ++++
.../lifecycle/EmulatorInfoControllerTest.java | 87 +++++++++++++++++++
3 files changed, 112 insertions(+), 1 deletion(-)
create mode 100644 src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerTest.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 2d5e0211e7..71872cf079 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
@@ -4,12 +4,17 @@ 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
+ * 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.
+ *
*
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/lifecycle/EmulatorInfoController.java b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java
index b9ccb8a0f4..740474c25e 100644
--- a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java
+++ b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.lifecycle;
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.ServiceRegistry;
import io.github.hectorvent.floci.lifecycle.inithook.InitializationHook;
import jakarta.inject.Inject;
@@ -11,6 +12,7 @@ import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
+import org.jboss.logging.Logger;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.core.common.Resettable;
@@ -28,12 +30,15 @@ import java.util.Optional;
@Produces(MediaType.APPLICATION_JSON)
public class EmulatorInfoController {
+ private static final Logger LOG = Logger.getLogger(EmulatorInfoController.class);
+
private final ServiceRegistry serviceRegistry;
private final InitLifecycleState initLifecycleState;
private final String version;
private final StorageFactory storageFactory;
private final Instance resettables;
+ private final Instance containerTeardowns;
private final FlociCertificateAuthority certificateAuthority;
private final EmulatorConfig config;
@@ -42,12 +47,14 @@ public class EmulatorInfoController {
InitLifecycleState initLifecycleState,
StorageFactory storageFactory,
Instance resettables,
+ Instance containerTeardowns,
FlociCertificateAuthority certificateAuthority,
EmulatorConfig config) {
this.serviceRegistry = serviceRegistry;
this.initLifecycleState = initLifecycleState;
this.storageFactory = storageFactory;
this.resettables = resettables;
+ this.containerTeardowns = containerTeardowns;
this.certificateAuthority = certificateAuthority;
this.config = config;
this.version = resolveVersion();
@@ -146,6 +153,18 @@ public class EmulatorInfoController {
}
private void performReset() {
+ // Containers first: they are tracked independently of StorageBackend, so this can run
+ // 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.
storageFactory.clearAll();
diff --git a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerTest.java b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerTest.java
new file mode 100644
index 0000000000..0dd276a3aa
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerTest.java
@@ -0,0 +1,87 @@
+package io.github.hectorvent.floci.lifecycle;
+
+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.Resettable;
+import io.github.hectorvent.floci.core.common.ServiceRegistry;
+import io.github.hectorvent.floci.core.storage.StorageFactory;
+import jakarta.enterprise.inject.Instance;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InOrder;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.List;
+
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class EmulatorInfoControllerTest {
+
+ @Mock private ServiceRegistry serviceRegistry;
+ @Mock private InitLifecycleState initLifecycleState;
+ @Mock private StorageFactory storageFactory;
+ @Mock private Instance resettables;
+ @Mock private Instance containerTeardowns;
+ @Mock private FlociCertificateAuthority certificateAuthority;
+ @Mock private EmulatorConfig config;
+ @Mock private ContainerTeardown sageMakerTeardown;
+ @Mock private ContainerTeardown batchTeardown;
+ @Mock private Resettable resettable;
+
+ private EmulatorInfoController controller;
+
+ @BeforeEach
+ void setUp() {
+ controller = new EmulatorInfoController(serviceRegistry, initLifecycleState, storageFactory,
+ resettables, containerTeardowns, certificateAuthority, config);
+ }
+
+ @Test
+ @DisplayName("Should stop every managed container before wiping storage on reset")
+ void reset_stopsManagedContainersBeforeClearingStorage() {
+ when(containerTeardowns.iterator()).thenReturn(List.of(sageMakerTeardown, batchTeardown).iterator());
+ when(resettables.iterator()).thenReturn(List.of(resettable).iterator());
+
+ controller.reset();
+
+ InOrder order = inOrder(sageMakerTeardown, batchTeardown, storageFactory, resettable);
+ order.verify(sageMakerTeardown).stopManagedContainers();
+ order.verify(batchTeardown).stopManagedContainers();
+ order.verify(storageFactory).clearAll();
+ order.verify(resettable).clear();
+ }
+
+ @Test
+ @DisplayName("Should continue tearing down other containers and still wipe storage when one teardown fails")
+ void reset_continuesAfterATeardownFailure() {
+ doThrow(new IllegalStateException("docker daemon unreachable"))
+ .when(sageMakerTeardown).stopManagedContainers();
+ when(containerTeardowns.iterator()).thenReturn(List.of(sageMakerTeardown, batchTeardown).iterator());
+ when(resettables.iterator()).thenReturn(List.of().iterator());
+
+ controller.reset();
+
+ verify(batchTeardown).stopManagedContainers();
+ verify(storageFactory).clearAll();
+ }
+
+ @Test
+ @DisplayName("nuke should stop managed containers just like reset")
+ void nuke_alsoStopsManagedContainers() {
+ when(containerTeardowns.iterator()).thenReturn(List.of(sageMakerTeardown).iterator());
+ when(resettables.iterator()).thenReturn(List.of().iterator());
+
+ controller.nuke();
+
+ verify(sageMakerTeardown).stopManagedContainers();
+ verify(storageFactory).clearAll();
+ }
+}