diff --git a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java index c935796c18..49364eef2d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java +++ b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java @@ -660,12 +660,14 @@ public class CodePipelineService { execution.getStageExecutionStatuses().put(execution.getCurrentStage(), "Stopping"); } if (execution.isAbandon()) { - execution.getActionExecutions().stream() - .filter(a -> "InProgress".equals(a.getStatus())) - .forEach(a -> { - a.setStatus("Abandoned"); - a.setLastUpdateTime(now()); - }); + synchronized (execution) { + execution.getActionExecutions().stream() + .filter(a -> "InProgress".equals(a.getStatus())) + .forEach(a -> { + a.setStatus("Abandoned"); + a.setLastUpdateTime(now()); + }); + } } putExecution(execution); eventPublisher.pipelineStateChange(execution, "STOPPING"); diff --git a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineServiceTest.java index d7f748da67..478eb75453 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineServiceTest.java @@ -13,11 +13,13 @@ import io.github.hectorvent.floci.services.codebuild.model.Build; import io.github.hectorvent.floci.services.codedeploy.CodeDeployService; import io.github.hectorvent.floci.services.codedeploy.model.Deployment; import io.github.hectorvent.floci.services.codepipeline.model.CodePipelineExecution; +import io.github.hectorvent.floci.services.codepipeline.model.CodePipelineExecution.ActionExecution; import io.github.hectorvent.floci.services.codepipeline.model.CodePipelinePipeline; import io.github.hectorvent.floci.services.codepipeline.model.CodePipelinePipeline.TransitionState; import io.github.hectorvent.floci.services.lambda.LambdaService; import io.github.hectorvent.floci.services.s3.S3Service; import io.github.hectorvent.floci.services.s3.model.S3Object; +import io.github.hectorvent.floci.testing.ThreadStates; import io.smallrye.config.SmallRyeConfigBuilder; import org.junit.jupiter.api.Test; @@ -31,6 +33,9 @@ import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; +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.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; @@ -1127,6 +1132,48 @@ class CodePipelineServiceTest { } } + @Test + void abandonIncludesAnActionRegisteredWhileTheStopWaits() throws Exception { + String pipeline = "abandon-during-action-registration"; + String executionId = "registering-execution"; + try (ExecutorService requests = Executors.newSingleThreadExecutor()) { + createExternalActionPipeline(service, pipeline, """ + {"name":"Approve","actionTypeId":{ + "category":"Approval","owner":"AWS","provider":"Manual","version":"1"}} + """); + CodePipelineExecution execution = waitingExecution(storage, pipeline, executionId, 1, "External"); + ActionExecution completed = new ActionExecution(); + completed.setStatus("Succeeded"); + execution.getActionExecutions().add(completed); + ActionExecution registering = new ActionExecution(); + registering.setStatus("InProgress"); + AtomicReference stoppingThread = new AtomicReference<>(); + Future stopped; + synchronized (execution) { + stopped = requests.submit(() -> { + stoppingThread.set(Thread.currentThread()); + return handle("StopPipelineExecution", """ + { + "pipelineName": "%s", + "pipelineExecutionId": "%s", + "abandon": true + } + """.formatted(pipeline, executionId)); + }); + ThreadStates.awaitBlockedOrDone(stoppingThread, stopped); + // The worker registers actions under this monitor before the stop can traverse them. + execution.getActionExecutions().add(registering); + } + assertEquals(executionId, stopped.get(5, TimeUnit.SECONDS).path("pipelineExecutionId").asText()); + assertEquals("Abandoned", registering.getStatus()); + assertEquals("Succeeded", completed.getStatus()); + assertEquals("Stopping", execution.getStatus()); + assertTrue(execution.isStopRequested()); + } finally { + service.shutdown(); + } + } + private CodePipelineService serviceWith(CodeBuildService codeBuildService, CodeDeployService codeDeployService) { return new CodePipelineService(