From 79e47cd7fceb2103f389383aaec2c8ca122c1714 Mon Sep 17 00:00:00 2001 From: berce <75228263+bercedev@users.noreply.github.com> Date: Tue, 27 Oct 2026 03:31:56 +0300 Subject: [PATCH] fix(ec2): seed the default network before marking the scope seeded (#6637) * fix(ec2): seed the default network before marking the scope seeded ensureDefaultResources added the account/region to the seeded set before seeding, so a concurrent first call returned at once and read a partial or empty default VPC. seed under a lock and mark the scope afterwards. * test(ec2): check the seed race with ParkingStore instead of a fixed wait The first call parks in its first subnet write and the test waits until the second call is blocked on the seed lock, so it fails at once on the old code and no longer sleeps two seconds. --- .../floci/services/ec2/Ec2Service.java | 14 +++- .../ec2/Ec2DefaultResourcesSeedTest.java | 79 +++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DefaultResourcesSeedTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java index e8f13a3101..d0a583b107 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java @@ -1427,9 +1427,21 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab public void ensureDefaultResources(String region) { String ownerAccountId = callerAccountId(); String seedScope = ownerAccountId + "::" + region; - if (!seededAccountRegions.add(seedScope)) { + if (seededAccountRegions.contains(seedScope)) { return; } + // Mark the scope only once it is seeded: a concurrent first call waits here instead of + // reading a half-built default network (#6247). + synchronized (seededAccountRegions) { + if (seededAccountRegions.contains(seedScope)) { + return; + } + seedDefaultResources(region, ownerAccountId); + seededAccountRegions.add(seedScope); + } + } + + private void seedDefaultResources(String region, String ownerAccountId) { List existingVpcs = vpcs.scan(k -> k.startsWith(region + "::")); // Already provisioned in a previous run and reloaded from persistent storage: the default // VPC (and everything else) is present, so don't re-seed and create duplicates (#1297). diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DefaultResourcesSeedTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DefaultResourcesSeedTest.java new file mode 100644 index 0000000000..9eaa4f6a61 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2DefaultResourcesSeedTest.java @@ -0,0 +1,79 @@ +package io.github.hectorvent.floci.services.ec2; + +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.services.ec2.model.Subnet; +import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager; +import io.github.hectorvent.floci.testing.ParkingStore; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import static io.github.hectorvent.floci.testing.ThreadStates.awaitBlockedOrDone; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Regression test for issue #6247: a call that arrives while the first call for an account and + * region is still seeding the default network waits for it instead of reading a partial network. + */ +class Ec2DefaultResourcesSeedTest { + + private static final String REGION = "us-east-1"; + + @Test + void aConcurrentFirstCallSeesTheWholeDefaultNetwork() throws Exception { + ParkingStore subnets = new ParkingStore<>(); + Ec2Service service = newService(subnets); + // Holds the first call in the middle of seeding, before any default subnet is stored. + subnets.parkNextWrite(); + ExecutorService pool = Executors.newFixedThreadPool(2); + try { + Future first = pool.submit(() -> service.ensureDefaultResources(REGION)); + assertTrue(subnets.awaitParked(), "the first call must reach its first subnet write"); + AtomicReference secondThread = new AtomicReference<>(); + Future second = pool.submit(() -> { + secondThread.set(Thread.currentThread()); + return service.describeSubnets(REGION, List.of(), Map.of()).size(); + }); + awaitBlockedOrDone(secondThread, second); + assertFalse(second.isDone(), "the second call read the default network while it was still being seeded"); + + subnets.release(); + first.get(2, TimeUnit.SECONDS); + assertEquals(3, second.get(2, TimeUnit.SECONDS), "the second call sees the three default subnets"); + } finally { + pool.shutdownNow(); + } + } + + private Ec2Service newService(StorageBackend subnets) { + EmulatorConfig config = mock(EmulatorConfig.class); + EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class); + EmulatorConfig.Ec2ServiceConfig ec2 = mock(EmulatorConfig.Ec2ServiceConfig.class); + when(config.defaultAccountId()).thenReturn("000000000000"); + when(config.services()).thenReturn(services); + when(services.ec2()).thenReturn(ec2); + when(ec2.mock()).thenReturn(true); + when(ec2.mockPublicAddresses()).thenReturn(EmulatorConfig.Ec2ServiceConfig.MockPublicAddresses.NONE); + Ec2ImageCatalog imageCatalog = new Ec2ImageCatalog(); + return new Ec2Service(config, null, mock(Ec2PortForwardManager.class), + new AmiImageResolver(imageCatalog), imageCatalog, + new Ec2InstanceTypeCatalog(), + new InMemoryStorage<>(), subnets, new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>()); + } +}