fix(codepipeline): snapshot actions before concurrent request reads (#7710)

Copy action-list membership under the existing execution monitor before
listing, approval and stage lookups. Reuse the snapshot for action ownership
lookup and stop completion so registration cannot invalidate a traversal.

Cover the four affected request paths with deterministic registration tests.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
Jackson Price authored and GitHub committed 2026-11-06 20:13:28 -04:00
1 parent 765647268b
commit a14dc18feb
2 files changed
+134 -6

No files matched your search

@@ -735,7 +735,7 @@ public class CodePipelineService {
String executionId = request.path("filter").path("pipelineExecutionId").asText(null);
List<ActionExecution> actions = executions(account, region, pipelineName).stream()
.filter(e -> executionId == null || executionId.equals(e.getPipelineExecutionId()))
.flatMap(e -> e.getActionExecutions().stream())
.flatMap(e -> actionExecutionsSnapshot(e).stream())
.sorted(Comparator.comparing(ActionExecution::getStartTime,
Comparator.nullsLast(Comparator.reverseOrder())))
.toList();
@@ -840,14 +840,14 @@ public class CodePipelineService {
CodePipelinePipeline pipeline = requirePipeline(account, region, pipelineName);
CodePipelineExecution execution = executions(account, region, pipelineName).stream()
.filter(e -> e.getActionExecutions().stream()
.filter(e -> actionExecutionsSnapshot(e).stream()
.anyMatch(a -> stageName.equals(a.getStageName())
&& actionName.equals(a.getActionName())
&& token.equals(a.getToken())))
.findFirst()
.orElse(null);
ActionExecution approval = execution == null ? null : execution.getActionExecutions().stream()
ActionExecution approval = execution == null ? null : actionExecutionsSnapshot(execution).stream()
.filter(a -> stageName.equals(a.getStageName()) && actionName.equals(a.getActionName()))
.filter(a -> token.equals(a.getToken()))
.findFirst()
@@ -2588,7 +2588,7 @@ public class CodePipelineService {
if (!execution.isStopRequested()) {
return false;
}
boolean anyRunning = execution.getActionExecutions().stream()
boolean anyRunning = actionExecutionsSnapshot(execution).stream()
.anyMatch(a -> "InProgress".equals(a.getStatus()));
if (!execution.isAbandon() && anyRunning) {
return false;
@@ -2770,6 +2770,12 @@ public class CodePipelineService {
throw new AwsException("StageNotFoundException", "Stage not found: " + stageName, 400);
}
private List<ActionExecution> actionExecutionsSnapshot(CodePipelineExecution execution) {
synchronized (execution) {
return List.copyOf(execution.getActionExecutions());
}
}
private Map<String, String> latestActionStatuses(CodePipelineExecution execution, String stageName) {
Map<String, String> statuses = new LinkedHashMap<>();
synchronized (execution) {
@@ -2896,14 +2902,14 @@ public class CodePipelineService {
private String pipelineExecutionId(ActionExecution action) {
return executionStore.scanAllAccounts().stream()
.filter(e -> e.getActionExecutions().stream()
.filter(e -> actionExecutionsSnapshot(e).stream()
.anyMatch(a -> action.getActionExecutionId().equals(a.getActionExecutionId())))
.map(CodePipelineExecution::getPipelineExecutionId)
.findFirst().orElse("");
}
private List<ActionExecution> actionExecutionsForStage(CodePipelineExecution execution, String stage) {
return execution.getActionExecutions().stream()
return actionExecutionsSnapshot(execution).stream()
.filter(a -> stage.equals(a.getStageName()))
.toList();
}
@@ -1174,6 +1174,128 @@ class CodePipelineServiceTest {
}
}
@Test
void listActionsIncludesAnActionRegisteredWhileTheRequestWaits() throws Exception {
String pipeline = "list-during-action-registration";
try {
CodePipelineExecution execution = createWaitingApprovalExecution(pipeline);
ActionExecution registering = approvalAction();
JsonNode response = handleWhileRegisteringAction("ListActionExecutions", """
{"pipelineName":"%s"}
""".formatted(pipeline), execution, registering);
JsonNode actions = response.path("actionExecutionDetails");
assertEquals(1, actions.size());
assertEquals(registering.getActionExecutionId(), actions.get(0).path("actionExecutionId").asText());
assertEquals(execution.getPipelineExecutionId(), actions.get(0).path("pipelineExecutionId").asText());
} finally {
service.shutdown();
}
}
@Test
void approvalAcceptsAnActionRegisteredWhileTheRequestWaits() throws Exception {
String pipeline = "approve-during-action-registration";
try {
CodePipelineExecution execution = createWaitingApprovalExecution(pipeline);
ActionExecution registering = approvalAction();
JsonNode response = handleWhileRegisteringAction("PutApprovalResult", """
{
"pipelineName": "%s",
"stageName": "External",
"actionName": "Approve",
"token": "%s",
"result": {"status": "Approved", "summary": "accepted"}
}
""".formatted(pipeline, registering.getToken()), execution, registering);
assertTrue(response.path("approvedAt").isNumber());
assertEquals("Succeeded", registering.getStatus());
assertEquals("accepted", registering.getSummary());
} finally {
service.shutdown();
}
}
@Test
void stageStateIncludesAnActionRegisteredOnAnOlderExecution() throws Exception {
String pipeline = "state-during-action-registration";
try {
CodePipelineExecution execution = createWaitingApprovalExecution(pipeline);
waitingExecution(storage, pipeline, "newer-execution", 2, "External");
JsonNode response = handleWhileRegisteringAction("GetPipelineState", """
{"name":"%s"}
""".formatted(pipeline), execution, approvalAction());
JsonNode stage = response.path("stageStates").get(0);
assertEquals("External", stage.path("stageName").asText());
assertEquals(execution.getPipelineExecutionId(),
stage.path("latestExecution").path("pipelineExecutionId").asText());
assertEquals("InProgress", stage.path("latestExecution").path("status").asText());
} finally {
service.shutdown();
}
}
@Test
void stageSuccessFilterExcludesAnActionRegisteredWhileTheRequestWaits() throws Exception {
String pipeline = "filter-during-action-registration";
try {
CodePipelineExecution execution = createWaitingApprovalExecution(pipeline);
JsonNode response = handleWhileRegisteringAction("ListPipelineExecutions", """
{
"pipelineName": "%s",
"filter": {"succeededInStage": {"stageName": "External"}}
}
""".formatted(pipeline), execution, approvalAction());
assertTrue(response.path("pipelineExecutionSummaries").isEmpty());
} finally {
service.shutdown();
}
}
private CodePipelineExecution createWaitingApprovalExecution(String pipeline) throws Exception {
createExternalActionPipeline(service, pipeline, """
{"name":"Approve","actionTypeId":{
"category":"Approval","owner":"AWS","provider":"Manual","version":"1"}}
""");
return waitingExecution(storage, pipeline, "registering-execution", 1, "External");
}
private ActionExecution approvalAction() {
ActionExecution action = new ActionExecution();
action.setActionExecutionId("registering-action");
action.setStageName("External");
action.setActionName("Approve");
action.setCategory("Approval");
action.setOwner("AWS");
action.setProvider("Manual");
action.setStatus("InProgress");
action.setStartTime(1.0);
action.setToken("approval-token");
return action;
}
private JsonNode handleWhileRegisteringAction(String operation, String body,
CodePipelineExecution execution,
ActionExecution registering) throws Exception {
try (ExecutorService requests = Executors.newSingleThreadExecutor()) {
AtomicReference<Thread> requestThread = new AtomicReference<>();
Future<JsonNode> response;
synchronized (execution) {
response = requests.submit(() -> {
requestThread.set(Thread.currentThread());
return handle(operation, body);
});
ThreadStates.awaitBlockedOrDone(requestThread, response);
// The worker finishes registering the action before the waiting reader acquires its monitor.
execution.getActionExecutions().add(registering);
}
return response.get(5, TimeUnit.SECONDS);
}
}
private CodePipelineService serviceWith(CodeBuildService codeBuildService,
CodeDeployService codeDeployService) {
return new CodePipelineService(