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
This commit is contained in:
Matheus André authored and GitHub committed 2026-09-20 16:41:37 -04:00
1 parent 4c64f2e397
commit 173854cdeb
3 files changed
+36 -4

No files matched your search

@@ -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() {}
}
@@ -167,11 +167,9 @@ public class EmulatorInfoController {
for (Resettable service : resettables) {
services.add(service);
}
List<Resettable> 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) {
@@ -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();
}
}