fix(apigateway): take the usage plan lock on delete (#6546)

This commit is contained in:
Mario Toffia authored and GitHub committed 2026-10-25 16:47:45 -04:00
1 parent 7b9e877f7a
commit 3389621dc6
2 files changed
+47 -4

No files matched your search

@@ -133,8 +133,9 @@ public class ApiGatewayService implements ResourceProvider {
private final Object domainNameLock = new Object();
private final Object vpcLinkTagLock = new Object();
/**
* Guards a usage plan patch. A patch writes back the stages, throttle and quota it read, so two at
* once would restore one another's stale values.
* Guards a usage plan patch and delete. A patch writes back the stages, throttle and quota it
* read, so two at once would restore one another's stale values, and a delete between its read
* and its write would be undone.
*/
private final Object usagePlanLock = new Object();
private final TlsCertificateManager certificateManager;
@@ -1604,8 +1605,10 @@ public class ApiGatewayService implements ResourceProvider {
}
public void deleteUsagePlan(String region, String usagePlanId) {
getUsagePlan(region, usagePlanId);
usagePlanStore.delete(usagePlanKey(region, usagePlanId));
synchronized (usagePlanLock) {
getUsagePlan(region, usagePlanId);
usagePlanStore.delete(usagePlanKey(region, usagePlanId));
}
}
/**
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.apigateway;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.config.TlsCertificateManager;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
@@ -125,4 +126,43 @@ class ApiGatewayUsagePlanServiceTest {
assertEquals(0L, mixed.get(), "limits mixing two revisions, for example " + samples);
}
}
// A patch writes back the plan it read, so without the lock a delete between the two is undone.
@Test
void aDeleteDuringAPatchStaysDeleted() throws Exception {
ApiGatewayService service = service();
List<Map<String, String>> patch = new ArrayList<>();
for (int i = 0; i < 200; i++) {
patch.add(Map.of("op", "add", "path", "/apiStages", "value", "api:s" + i));
}
int resurrected = 0;
try (ExecutorService executor = Executors.newFixedThreadPool(2)) {
for (int round = 0; round < 200; round++) {
String planId = service.createUsagePlan(REGION, Map.of("name", "delete-race")).getId();
CountDownLatch start = new CountDownLatch(1);
Future<?> patcher = executor.submit(() -> {
assertTrue(start.await(5, TimeUnit.SECONDS));
try {
service.updateUsagePlan(REGION, planId, patch);
} catch (AwsException expected) {
// The delete went first, so the patch finds no plan.
assertEquals("NotFoundException", expected.getErrorCode());
}
return null;
});
Future<?> deleter = executor.submit(() -> {
assertTrue(start.await(5, TimeUnit.SECONDS));
service.deleteUsagePlan(REGION, planId);
return null;
});
start.countDown();
patcher.get(10, TimeUnit.SECONDS);
deleter.get(10, TimeUnit.SECONDS);
if (service.getUsagePlans(REGION).stream().anyMatch(plan -> plan.getId().equals(planId))) {
resurrected++;
}
}
}
assertEquals(0, resurrected, "plans a patch wrote back after they were deleted");
}
}