fix(autoscaling): skip groups deleted mid-iteration and lock instance updates (#5751)

The groups map is a StorageBackedMap once storage is initialised. Its
iterator snapshots keys and reads each value lazily, so a group deleted
between the two yields a null value. describeAutoScalingGroups returned
that null (the reconciler then NPE'd in reconcile and again in its catch
block), and describeAutoScalingInstances, SetInstanceHealth and
TerminateInstanceInAutoScalingGroup NPE'd outright (HTTP 500).

Skip null values at those iteration sites. SetInstanceHealth and
TerminateInstanceInAutoScalingGroup now re-read and update the group
under its per-group lock, as saveAutoScalingGroupIfPresent does, instead
of an unlocked find/mutate/put that could race the reconciler or
resurrect a just-deleted group.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-15 19:50:58 -04:00
1 parent 0492fc9842
commit f045d7d0a8
3 files changed
+90 -30

No files matched your search

@@ -391,6 +391,19 @@ class AutoScalingRefreshStorageTest {
assertEquals(1, group.getInstances().size());
}
@ParameterizedTest
@ValueSource(strings = {"persistent", "hybrid", "wal"})
void terminatedInstanceDropsOutOfActiveRefresh(String mode) throws IOException {
setUp(mode);
service.startInstanceRefresh(REGION, "suspended-group", new InstanceRefresh());
service.terminateInstanceInAutoScalingGroup(REGION, "i-original", true);
restart(mode);
AutoScalingGroup restored = service.describeAutoScalingGroups(REGION, List.of("suspended-group")).getFirst();
InstanceRefresh refresh = service.activeInstanceRefresh(restored);
assertNotNull(refresh);
assertTrue(refresh.getReplacements().isEmpty(), "terminated instance must leave the refresh");
}
@ParameterizedTest
@ValueSource(strings = {"persistent", "hybrid", "wal"})
void explicitTerminationOfSelectedOriginalIsNotPaused(String mode) throws IOException {
@@ -2,6 +2,8 @@ package io.github.hectorvent.floci.services.autoscaling;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.storage.StorageBackedMap;
import io.github.hectorvent.floci.services.autoscaling.model.AsgInstance;
import io.github.hectorvent.floci.services.autoscaling.model.AsgOptionalFields;
import io.github.hectorvent.floci.services.autoscaling.model.AutoScalingGroup;
@@ -27,8 +29,10 @@ import org.junit.jupiter.params.provider.MethodSource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import java.util.zip.GZIPOutputStream;
@@ -908,6 +912,37 @@ class AutoScalingServiceTest {
.getFirst().getHealthStatus());
}
@Test
void groupDeletedMidIterationIsSkippedNotReturnedAsNull() {
// A StorageBackedMap snapshots keys, then reads each value lazily; a group deleted between the
// two reads comes back as a key with no value. Model that with a key the store lists but cannot get.
InMemoryStorage<String, AutoScalingGroup> storage = new InMemoryStorage<>() {
@Override
public Set<String> keys() {
Set<String> keys = new LinkedHashSet<>();
keys.add(AutoScalingService.asgKey(REGION, "deleted-asg"));
keys.addAll(super.keys());
return keys;
}
};
StorageBackedMap<AutoScalingGroup> groups = new StorageBackedMap<>(storage);
groups.putAll(service.groups);
service.groups = groups;
service.attachInstances(REGION, "test-asg", List.of("i-live"));
assertFalse(service.describeAutoScalingGroups(null, null).contains(null));
assertEquals(List.of("test-asg"), service.describeAutoScalingGroups(REGION, null).stream()
.map(AutoScalingGroup::getAutoScalingGroupName).toList());
assertEquals(1, service.describeAutoScalingInstances(REGION, null).size());
service.setInstanceHealth(REGION, "i-live", "Unhealthy", false);
service.terminateInstanceInAutoScalingGroup(REGION, "i-live", false);
AsgInstance stored = storage.get(AutoScalingService.asgKey(REGION, "test-asg")).orElseThrow()
.getInstances().getFirst();
assertEquals("Unhealthy", stored.getHealthStatus());
assertEquals("Terminating", stored.getLifecycleState());
}
private static final class AutoScalingGroupFixture {
private static void addInstance(AutoScalingService service, String region, String name, String instanceId,
String lifecycleState, String launchTemplateId, String launchTemplateVersion) {