fix(codepipeline): serialize stop publication with completion (#7939)

Serialize stop metadata and completion while keeping event publication outside the execution monitor. Preserve ordinary stop and abandon regressions.

Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-10 18:17:16 -04:00
1 parent bad32b3598
commit da5c0d8b36
2 files changed
+93 -32

No files matched your search

@@ -645,22 +645,19 @@ public class CodePipelineService {
private ObjectNode stopPipelineExecution(JsonNode request, String region, String account) {
CodePipelineExecution execution = requireExecution(
account, region, text(request, "pipelineName"), text(request, "pipelineExecutionId"));
if (isTerminal(execution.getStatus())) {
throw new AwsException("PipelineExecutionNotStoppableException",
"Pipeline execution is already in a terminal state", 400);
}
// Publish the stop mode before the stop signal. The provider polling loop reads
// stopRequested first, so observing it also observes the matching abandon value.
execution.setAbandon(request.path("abandon").asBoolean(false));
execution.setStopRequested(true);
execution.setStatus("Stopping");
execution.setStatusSummary(request.path("reason").asText("Stop requested."));
execution.setLastUpdateTime(now());
if (execution.getCurrentStage() != null) {
execution.getStageExecutionStatuses().put(execution.getCurrentStage(), "Stopping");
}
if (execution.isAbandon()) {
synchronized (execution) {
synchronized (execution) {
if (isTerminal(execution.getStatus())) {
throw new AwsException("PipelineExecutionNotStoppableException",
"Pipeline execution is already in a terminal state", 400);
}
execution.setAbandon(request.path("abandon").asBoolean(false));
execution.setStatus("Stopping");
execution.setStatusSummary(request.path("reason").asText("Stop requested."));
execution.setLastUpdateTime(now());
if (execution.getCurrentStage() != null) {
execution.getStageExecutionStatuses().put(execution.getCurrentStage(), "Stopping");
}
if (execution.isAbandon()) {
execution.getActionExecutions().stream()
.filter(a -> "InProgress".equals(a.getStatus()))
.forEach(a -> {
@@ -668,8 +665,9 @@ public class CodePipelineService {
a.setLastUpdateTime(now());
});
}
execution.setStopRequested(true);
putExecution(execution);
}
putExecution(execution);
eventPublisher.pipelineStateChange(execution, "STOPPING");
return mapper.createObjectNode().put("pipelineExecutionId", execution.getPipelineExecutionId());
}
@@ -2585,22 +2583,24 @@ public class CodePipelineService {
}
private boolean finishIfStopped(CodePipelineExecution execution) {
if (!execution.isStopRequested()) {
return false;
synchronized (execution) {
if (!execution.isStopRequested()) {
return false;
}
boolean anyRunning = actionExecutionsSnapshot(execution).stream()
.anyMatch(a -> "InProgress".equals(a.getStatus()));
if (!execution.isAbandon() && anyRunning) {
return false;
}
execution.setStatus("Stopped");
execution.setStatusSummary("Pipeline execution stopped.");
execution.setLastUpdateTime(now());
if (execution.getCurrentStage() != null) {
execution.getStageExecutionStatuses().put(execution.getCurrentStage(), "Stopped");
}
putExecution(execution);
return true;
}
boolean anyRunning = actionExecutionsSnapshot(execution).stream()
.anyMatch(a -> "InProgress".equals(a.getStatus()));
if (!execution.isAbandon() && anyRunning) {
return false;
}
execution.setStatus("Stopped");
execution.setStatusSummary("Pipeline execution stopped.");
execution.setLastUpdateTime(now());
if (execution.getCurrentStage() != null) {
execution.getStageExecutionStatuses().put(execution.getCurrentStage(), "Stopped");
}
putExecution(execution);
return true;
}
private void initializeTransitions(CodePipelinePipeline pipeline) {
@@ -22,6 +22,8 @@ import io.floci.aws.services.s3.model.S3Object;
import io.floci.aws.testing.ThreadStates;
import io.smallrye.config.SmallRyeConfigBuilder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
@@ -48,6 +50,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
@@ -1132,6 +1135,64 @@ class CodePipelineServiceTest {
}
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
void workerCompletionCannotBeOverwrittenByStopPublication(boolean abandon) throws Exception {
String pipeline = "stop-publication";
String executionId = "stopping-execution";
CountDownLatch stopPublished = new CountDownLatch(1);
CountDownLatch releaseStop = new CountDownLatch(1);
AtomicReference<Thread> requester = new AtomicReference<>();
AtomicReference<Thread> worker = new AtomicReference<>();
try (ExecutorService requests = Executors.newSingleThreadExecutor()) {
createExternalActionPipeline(service, pipeline, """
{"name":"Approve","actionTypeId":{
"category":"Approval","owner":"AWS","provider":"Manual","version":"1"}}
""");
CodePipelineExecution execution = spy(waitingExecution(storage, pipeline, executionId, 1, null));
doAnswer(invocation -> {
boolean requested = (boolean) invocation.callRealMethod();
if (requested && Thread.currentThread() != requester.get()) {
worker.compareAndSet(null, Thread.currentThread());
}
return requested;
}).when(execution).isStopRequested();
doAnswer(invocation -> {
invocation.callRealMethod();
if ((boolean) invocation.getArgument(0)) {
stopPublished.countDown();
assertTrue(releaseStop.await(5, TimeUnit.SECONDS));
}
return null;
}).when(execution).setStopRequested(anyBoolean());
storage.executionStore.putForAccount(ACCOUNT, REGION + ":" + pipeline + ":" + executionId, execution);
service.resumePersistedExecutions();
await().atMost(Duration.ofSeconds(3)).untilAsserted(() ->
assertFalse(handle("ListActionExecutions", """
{"pipelineName":"%s","filter":{"pipelineExecutionId":"%s"}}
""".formatted(pipeline, executionId)).path("actionExecutionDetails").isEmpty()));
Future<?> stopped = requests.submit(() -> {
requester.set(Thread.currentThread());
stopExecution(service, pipeline, executionId, abandon);
return null;
});
try {
assertTrue(stopPublished.await(5, TimeUnit.SECONDS));
await().atMost(Duration.ofSeconds(3)).until(() -> worker.get() != null);
ThreadStates.awaitBlockedOrDone(worker.get());
} finally {
releaseStop.countDown();
}
stopped.get(5, TimeUnit.SECONDS);
await().atMost(Duration.ofSeconds(3))
.until(() -> !service.activeRuns.contains(ACCOUNT + ":" + executionId));
assertEquals("Stopped", execution.getStatus());
} finally {
releaseStop.countDown();
service.shutdown();
}
}
@Test
void abandonIncludesAnActionRegisteredWhileTheStopWaits() throws Exception {
String pipeline = "abandon-during-action-registration";