fix(cloudformation): bound operation executor (#3761)

* fix(cloudformation): bound operations and restore stacks safely

Limit concurrent stack operations, return the AWS LimitExceeded error code,
and restore rejected mutations under the stack lock.

* fix(cloudformation): prevent nested delete deadlocks

Use the AWS LimitExceededException wire code and delete nested stacks inline
so bounded operation workers cannot wait on child tasks queued behind them.
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-19 20:06:26 -05:00
1 parent eef69445cc
commit 91de9e452e
3 files changed
+216 -35

No files matched your search

@@ -6,12 +6,20 @@ import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
/**
* Verifies the resource-level {@code DeletionPolicy} attribute (issue #1555): {@code Retain} keeps a
@@ -27,6 +35,50 @@ class CloudFormationDeletionPolicyIntegrationTest {
private static final String CUSTOM_AUTH =
"AWS4-HMAC-SHA256 Credential=111122223333/20260205/eu-west-1/cloudformation/aws4_request";
@Test
void deletingNestedStacksDoesNotExhaustOperationWorkers() throws InterruptedException {
String suffix = Long.toString(System.nanoTime(), 36);
String childTemplateKey = "child-delete-workers-" + suffix + ".json";
given().when().put("/nested-stack-templates").then().statusCode(200);
given()
.contentType("application/json")
.body("{\"Resources\":{}}")
.when()
.put("/nested-stack-templates/" + childTemplateKey)
.then()
.statusCode(200);
List<String> stackIds = new ArrayList<>();
List<String> stackNames = new ArrayList<>();
String childUrl = "http://localhost/nested-stack-templates/" + childTemplateKey;
for (int i = 0; i < 16; i++) {
String stackName = "nested-delete-worker-" + suffix + "-" + i;
String template = "{\"Resources\":{\"Child\":{\"Type\":\"AWS::CloudFormation::Stack\","
+ "\"Properties\":{\"TemplateURL\":\"" + childUrl + "\"}}}}";
String stackId = createStack(stackName, template);
awaitStackStatus(stackId, "CREATE_COMPLETE");
stackNames.add(stackName);
stackIds.add(stackId);
}
assertTimeoutPreemptively(Duration.ofSeconds(20), () -> {
try (ExecutorService clients = Executors.newFixedThreadPool(16)) {
List<CompletableFuture<Void>> deletes = stackNames.stream()
.map(name -> CompletableFuture.runAsync(() -> deleteStack(name), clients))
.toList();
CompletableFuture.allOf(deletes.toArray(CompletableFuture[]::new)).join();
for (String stackId : stackIds) {
try {
awaitStackStatus(stackId, "DELETE_COMPLETE");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new AssertionError("Interrupted while waiting for nested stack deletion", e);
}
}
}
});
}
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
@@ -0,0 +1,49 @@
package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.core.common.AwsException;
import org.junit.jupiter.api.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class CloudFormationServiceExecutorTest {
@Test
void operationExecutorRejectsWorkAfterThreadsAndQueueAreFull() throws Exception {
ThreadPoolExecutor executor = CloudFormationService.newOperationExecutor();
CountDownLatch release = new CountDownLatch(1);
try {
for (int i = 0; i < executor.getMaximumPoolSize(); i++) {
executor.execute(() -> await(release));
}
int queueCapacity = executor.getQueue().remainingCapacity();
for (int i = 0; i < queueCapacity; i++) {
executor.execute(() -> { });
}
assertThrows(RejectedExecutionException.class, () -> executor.execute(() -> { }));
} finally {
release.countDown();
executor.shutdownNow();
}
}
@Test
void rejectedCloudFormationOperationUsesAwsErrorCode() {
AwsException exception = CloudFormationService.operationLimitExceeded();
assertEquals("LimitExceededException", exception.getErrorCode());
}
private static void await(CountDownLatch release) {
try {
release.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}