From 173854cdeb69acd4f626026e04b15c33f441e123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Andr=C3=A9?= <92062874+matheusandre1@users.noreply.github.com> Date: Sun, 20 Sep 2026 17:41:37 -0300 Subject: [PATCH] fix(lifecycle): run afterReset on every Resettable when a beforeReset throws (#4024) performReset stops every managed container first, then walks the Resettable list and adds each service to `prepared` right before calling its beforeReset(). When one beforeReset() threw, the loop stopped and every later service was never added, so the finally block skipped their afterReset(). Their teardown had already run, though: the SageMaker training runner and endpoint manager had shut their worker pools down and EcsService had stopped its reconciler, and those are only restored in afterReset() (#3937). After such a reset every CreateTrainingJob, CreateEndpoint and UpdateEndpoint returned 500 with RejectedExecutionException and ECS services were never reconciled again until the process restarted. The finally block now runs afterReset() over the full service list in reverse order, dropping the `prepared` list. afterReset() already had to tolerate a reset that failed before clear(); tolerating one that failed before beforeReset() is the same contract, and the Resettable Javadoc now says so. A controller test registers a Resettable whose beforeReset() throws between two others and asserts that the error is rethrown, the wipe and clear() are skipped, and afterReset() still runs on all three, including the one whose beforeReset() never ran. Closes #3952 Closes #3796 --- .../floci/core/common/Resettable.java | 5 +++- .../lifecycle/EmulatorInfoController.java | 7 +++-- .../lifecycle/EmulatorInfoControllerTest.java | 28 +++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/core/common/Resettable.java b/src/main/java/io/github/hectorvent/floci/core/common/Resettable.java index 1aead9edfa..7a23bd59fb 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/Resettable.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/Resettable.java @@ -12,6 +12,9 @@ public interface Resettable { void clear(); - /** Resume after reset, including when a storage/clear operation failed. */ + /** + * Resume after reset, including when a storage/clear operation failed or an earlier service's + * beforeReset() threw, so this may run without a preceding {@link #beforeReset()} call. + */ default void afterReset() {} } 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 898e144f8d..b8b4d63757 100644 --- a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java +++ b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java @@ -167,11 +167,9 @@ public class EmulatorInfoController { for (Resettable service : resettables) { services.add(service); } - List prepared = new ArrayList<>(); RuntimeException failure = null; try { for (Resettable service : services) { - prepared.add(service); service.beforeReset(); } // Storage still precedes clear(): services recreate their bootstrap state there. @@ -182,7 +180,10 @@ public class EmulatorInfoController { } catch (RuntimeException e) { failure = e; } finally { - for (Resettable service : prepared.reversed()) { + // Every service, not only those whose beforeReset() ran: the teardowns above already + // shut down the pools that afterReset() restores, and a beforeReset() that throws + // would otherwise leave every later service with its pool terminated for good. + for (Resettable service : services.reversed()) { try { service.afterReset(); } catch (RuntimeException e) { diff --git a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerTest.java b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerTest.java index 527f2a00d0..1023f6964f 100644 --- a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerTest.java +++ b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerTest.java @@ -17,6 +17,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.util.List; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.inOrder; @@ -120,4 +121,31 @@ class EmulatorInfoControllerTest { verify(resettable).afterReset(); verify(resettable, never()).clear(); } + + @Test + @DisplayName("Should call afterReset on every service, even those after a beforeReset that failed, then rethrow") + void reset_runsAfterResetOnEveryServiceWhenABeforeResetFails() { + Resettable failing = mock(Resettable.class); + Resettable later = mock(Resettable.class); + doThrow(new IllegalStateException("drain timed out")).when(failing).beforeReset(); + when(containerTeardowns.iterator()).thenReturn(List.of(sageMakerTeardown).iterator()); + when(resettables.iterator()).thenReturn(List.of(resettable, failing, later).iterator()); + + IllegalStateException thrown = assertThrows(IllegalStateException.class, controller::reset); + + assertEquals("drain timed out", thrown.getMessage()); + // The teardown already shut down the pools that afterReset() restores, so the service + // whose beforeReset() never ran needs afterReset() just as much as the ones that ran. + InOrder order = inOrder(sageMakerTeardown, resettable, failing, later); + order.verify(sageMakerTeardown).stopManagedContainers(); + order.verify(resettable).beforeReset(); + order.verify(failing).beforeReset(); + order.verify(later).afterReset(); + order.verify(failing).afterReset(); + order.verify(resettable).afterReset(); + verify(later, never()).beforeReset(); + verify(storageFactory, never()).clearAll(); + verify(resettable, never()).clear(); + verify(later, never()).clear(); + } }