fix(stepfunctions): publish execution status after its terminal fields (#2234)

* fix(stepfunctions): publish execution status after its terminal fields

DescribeExecution returns the live Execution instance from the store, and the
executor mutated it in place with the status set before the fields that describe
it. A client polling for a terminal status could therefore observe SUCCEEDED with
a null output, which real Step Functions never returns.

This is what reddened main in run 31457665686: the poll in
StepFunctionsPassParametersIntegrationTest landed inside the window and Jackson
rejected the null document with IllegalArgumentException: argument "content" is
null. It is intermittent (one failure in the last fifteen main runs), because the
window is two adjacent field writes wide.

Set the terminal fields first and the status last on every terminal path, so the
status is the publication point: SUCCEEDED, both FAILED paths, failExecution,
TIMED_OUT, and ABORTED in stopExecution.

The outer catch in doExecute was worse than a race. It set only the status, so an
execution failing there reported FAILED with a null error and cause permanently.
It now records both.

No new test. The window is a few instructions wide while an HTTP round trip is
milliseconds, so a black box test cannot land in it, and onUpdate fires after every
setter so there is no seam to observe from. A concurrent polling test was written
and discarded after it passed against the unfixed code. Existing coverage does
guard the fields themselves: StepFunctionsJsonataIntegrationTest asserts error and
cause on failed executions in several places.

* test(stepfunctions): cover terminal error and cause on the outer catch

The outer catch in doExecute previously set only the status, leaving error
and cause null on an execution DescribeExecution reports as FAILED. That
path had no coverage: no test referenced the "State not found" throw, and
the existing States.Runtime assertions all go through the inner catch.

A missing state reaches it deterministically, since the throw sits outside
the per-state try and unwinds the whole execution loop. Verified as a real
guard rather than a green one: against the pre-fix outer catch these fail
with "expected: <States.Runtime> but was: <null>".

Field ordering is deliberately not asserted. The window between two
adjacent setters is a few instructions wide, so a black box test cannot
land in it.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-08-13 00:24:07 -05:00
1 parent c48c062117
commit 4ba854ab8c
3 files changed
+163 -8

No files matched your search

@@ -205,8 +205,8 @@ public class AslExecutor {
runUnderExecutionAccount(sm, () -> doExecute(sm, exec, history, onUpdate)));
f.get(300, TimeUnit.SECONDS);
} catch (java.util.concurrent.TimeoutException e) {
exec.setStatus("TIMED_OUT");
exec.setStopDate(System.currentTimeMillis() / 1000.0);
exec.setStatus("TIMED_OUT");
onUpdate.accept(exec, history);
} catch (Exception e) {
LOG.warnv("Sync execution wait failed for {0}: {1}", exec.getExecutionArn(), e.getMessage());
@@ -329,12 +329,12 @@ public class AslExecutor {
onUpdate.accept(exec, history);
return;
} catch (Exception e) {
exec.setStatus("FAILED");
exec.setStopDate(System.currentTimeMillis() / 1000.0);
String runtimeError = "States.Runtime";
String runtimeCause = e.getMessage() != null ? e.getMessage() : "Unknown error";
exec.setError(runtimeError);
exec.setCause(runtimeCause);
exec.setStopDate(System.currentTimeMillis() / 1000.0);
exec.setStatus("FAILED");
addEvent(history, eventId, "ExecutionFailed", null,
Map.of("error", runtimeError, "cause", runtimeCause));
onUpdate.accept(exec, history);
@@ -342,17 +342,25 @@ public class AslExecutor {
}
}
exec.setStatus("SUCCEEDED");
// Status is the publication point, so it is set last. describeExecution hands out this
// same live Execution, so a client polling for SUCCEEDED between setStatus and setOutput
// would read a terminal execution with a null output, which real Step Functions never
// returns. The same ordering applies to every terminal path below.
exec.setOutput(currentInput.toString());
exec.setStopDate(System.currentTimeMillis() / 1000.0);
exec.setStatus("SUCCEEDED");
addEvent(history, eventId, "ExecutionSucceeded", null,
Map.of("output", currentInput.toString()));
onUpdate.accept(exec, history);
} catch (Exception e) {
LOG.warnv("ASL execution failed for {0}: {1}", exec.getExecutionArn(), e.getMessage());
exec.setStatus("FAILED");
// This path previously set only the status, leaving error and cause null forever on an
// execution DescribeExecution reports as FAILED.
exec.setError("States.Runtime");
exec.setCause(e.getMessage() != null ? e.getMessage() : "Unknown error");
exec.setStopDate(System.currentTimeMillis() / 1000.0);
exec.setStatus("FAILED");
onUpdate.accept(exec, history);
}
}
@@ -2840,12 +2848,12 @@ public class AslExecutor {
}
private void failExecution(Execution exec, List<HistoryEvent> history, AtomicLong eventId, FailStateException e) {
exec.setStatus("FAILED");
exec.setStopDate(System.currentTimeMillis() / 1000.0);
String failError = e.error != null ? e.error : "States.Runtime";
String failCause = e.cause != null ? e.cause : "";
exec.setError(failError);
exec.setCause(failCause);
exec.setStopDate(System.currentTimeMillis() / 1000.0);
exec.setStatus("FAILED");
addEvent(history, eventId, "ExecutionFailed", null,
Map.of("error", failError, "cause", failCause));
}
@@ -258,8 +258,8 @@ public class StepFunctionsService implements Resettable {
if (!"RUNNING".equals(exec.getStatus())) {
return;
}
exec.setStatus("ABORTED");
exec.setStopDate(System.currentTimeMillis() / 1000.0);
exec.setStatus("ABORTED");
executionStore.put(arn, exec);
List<HistoryEvent> history = historyCache.getOrDefault(arn, new ArrayList<>());
@@ -0,0 +1,147 @@
package io.github.hectorvent.floci.services.stepfunctions;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbJsonHandler;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
import io.github.hectorvent.floci.services.lambda.LambdaExecutorService;
import io.github.hectorvent.floci.services.lambda.LambdaFunctionStore;
import io.github.hectorvent.floci.services.s3.S3Service;
import io.github.hectorvent.floci.services.sqs.SqsJsonHandler;
import io.github.hectorvent.floci.services.stepfunctions.model.Execution;
import io.github.hectorvent.floci.services.stepfunctions.model.HistoryEvent;
import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine;
import io.quarkus.test.junit.QuarkusTest;
import io.vertx.mutiny.core.Vertx;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mock;
/**
* Guards the terminal metadata written on the outer catch in {@code doExecute}, the path that
* previously set only the status and left {@code error} and {@code cause} null permanently on an
* execution DescribeExecution reports as FAILED.
*
* <p>A missing state reaches it deterministically: the "State not found" throw sits outside the
* per-state try, so it unwinds the whole execution loop rather than being caught as a state
* failure. The field ordering this class does not assert is untestable from here, since the window
* between two adjacent setters is a few instructions wide.
*/
@QuarkusTest
class AslExecutorTerminalStateTest {
private static final String REGION = "us-east-2";
private static final String ACCOUNT = "000000000000";
private final ObjectMapper objectMapper = new ObjectMapper();
private AslExecutor executor;
@Inject
Vertx vertx;
@BeforeEach
void setUp() {
executor = new AslExecutor(
mock(LambdaExecutorService.class),
mock(LambdaFunctionStore.class),
mock(DynamoDbService.class),
mock(DynamoDbJsonHandler.class),
mock(SqsJsonHandler.class),
mock(io.github.hectorvent.floci.services.cloudformation.CloudFormationQueryHandler.class),
mock(io.github.hectorvent.floci.services.ec2.Ec2Service.class),
mock(S3Service.class),
mock(io.github.hectorvent.floci.services.ecs.EcsService.class),
mock(io.github.hectorvent.floci.services.ecs.EcsJsonHandler.class),
objectMapper,
new JsonataEvaluator(objectMapper),
mock(Instance.class), mock(EmulatorConfig.class), vertx);
}
@Test
void executionFailedOnTheOuterCatchCarriesErrorAndCause() {
Execution execution = run("""
{
"StartAt": "NoSuchState",
"States": {
"Unreachable": {
"Type": "Succeed"
}
}
}
""");
assertEquals("FAILED", execution.getStatus());
assertEquals("States.Runtime", execution.getError());
assertEquals("State not found: NoSuchState", execution.getCause());
assertNotNull(execution.getStopDate());
}
@Test
void executionFailedOnAnUnresolvableNextCarriesErrorAndCause() {
Execution execution = run("""
{
"StartAt": "First",
"States": {
"First": {
"Type": "Pass",
"Next": "Vanished"
}
}
}
""");
assertEquals("FAILED", execution.getStatus());
assertEquals("States.Runtime", execution.getError());
assertEquals("State not found: Vanished", execution.getCause());
assertNotNull(execution.getStopDate());
}
@Test
void succeededExecutionCarriesItsOutputAndStopDate() {
Execution execution = run("""
{
"StartAt": "Only",
"States": {
"Only": {
"Type": "Pass",
"End": true
}
}
}
""");
assertEquals("SUCCEEDED", execution.getStatus());
assertNotNull(execution.getOutput());
assertNotNull(execution.getStopDate());
}
private Execution run(String definition) {
StateMachine stateMachine = new StateMachine();
stateMachine.setName("terminal-state-test");
stateMachine.setStateMachineArn(
"arn:aws:states:%s:%s:stateMachine:terminal-state-test".formatted(REGION, ACCOUNT));
stateMachine.setRoleArn("arn:aws:iam::%s:role/test-role".formatted(ACCOUNT));
stateMachine.setDefinition(definition);
Execution execution = new Execution();
execution.setName("terminal-state-execution");
execution.setExecutionArn(
"arn:aws:states:%s:%s:execution:terminal-state-test:terminal-state-execution"
.formatted(REGION, ACCOUNT));
execution.setStateMachineArn(stateMachine.getStateMachineArn());
execution.setInput("{}");
List<HistoryEvent> history = new ArrayList<>();
executor.executeSync(stateMachine, execution, history, (updated, events) -> {
});
return execution;
}
}