fix(stepfunctions): abort executions abandoned by a restart (#2969)

* test(stepfunctions): cover executions abandoned by a restart

* fix(stepfunctions): abort executions abandoned by a restart

An execution runs inside the Floci process, so a restart leaves it stored as
RUNNING with no worker behind it: DescribeExecution reported RUNNING forever
and GetExecutionHistory reported nothing, so a caller waiting for a final
status hung until its own timeout with no diagnosis.

EmulatorLifecycle.onStart now calls StepFunctionsService.abortAbandonedExecutions
after storageFactory.loadAll(), which retires every still-RUNNING execution with
status ABORTED, a stopDate, error ExecutionAbandoned and a cause naming the
restart, and seals its history with a single ExecutionAborted event. ABORTED is
the status AWS gives an execution ended from outside its own definition, and it
is the only terminal status that keeps the execution inside RedriveExecution's
reach.

The sweep runs with no request context, so it scans every account and writes
each execution back under the account that owns it. The terminal write is shared
with StopExecution, which handled the same window for its own half already.

* fix(stepfunctions): file the abort history under the storage key

markAborted read the history key off the stored value. An execution whose
executionArn field is null made computeIfAbsent throw out of the startup sweep,
which EmulatorLifecycle.onStart calls unguarded, so the process did not boot and
the sweep stopped mid list with earlier entries flipped in memory and never
written back. A field that disagreed with the storage key filed the
ExecutionAborted event where GetExecutionHistory does not look. The key is now a
parameter: StopExecution passes the ARN it was called with, the sweep passes the
key the entry came under.

The tests bound the stopDate between two literal epoch-second instants instead of
asserting it is non-null, so a wrong unit fails; assert the sweep stays silent
when nothing is RUNNING, which is what the count guard is for; and pin the WARN
record by level and count rather than by its whole sentence. The lifecycle test
whose plain verify was subsumed by the ordered one is gone.

The documentation no longer claims the ExecutionAborted event survives a further
restart: histories live in memory, so the event is written on the boot that
aborts the execution, while the status, error and cause persist.

* test(stepfunctions): require the AWS shape of an abort with no reason

The startup sweep is measured against us-east-1 with StopExecution called
without --error and --cause: status ABORTED, DescribeExecution omitting both
keys, and executionAbortedEventDetails present and empty. The persistence test
pins the sweep, and the integration test pins the wire shape, where an omitted
key is distinguishable from a null one.

* fix(stepfunctions): abort abandoned executions the way AWS aborts

ExecutionAbandoned is a code AWS never emits, and the sweep was the only
producer of it. Measured against us-east-1 with StopExecution called without
--error and --cause, the abort reports status ABORTED, DescribeExecution omits
the error and cause keys, and the terminal event carries an empty
executionAbortedEventDetails. The sweep now takes that same path; the WARN line
with the count is where the reason lives.

markAborted keeps both parameters: StopExecution passes the caller's error and
cause through them.

---------

Co-authored-by: Facundo Paredes <25681464+facuparedes@users.noreply.github.com>
This commit is contained in:
Facundo ParedesandFacundo Paredes authored and GitHub committed 2026-09-06 15:44:31 -05:00
1 parent 618ca58424
commit 504203f8e8
6 files changed
+386 -14

No files matched your search

@@ -28,6 +28,7 @@ import io.github.hectorvent.floci.services.appsync.graphql.SchemaCreationWorker;
import io.github.hectorvent.floci.services.elb.ElbClassicService;
import io.github.hectorvent.floci.services.elbv2.ElbV2Service;
import io.github.hectorvent.floci.services.rds.RdsService;
import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService;
import io.github.hectorvent.floci.services.memorydb.container.MemoryDbContainerManager;
import io.github.hectorvent.floci.services.memorydb.proxy.MemoryDbProxyManager;
import io.github.hectorvent.floci.services.rds.container.RdsContainerManager;
@@ -94,6 +95,7 @@ public class EmulatorLifecycle {
private final FlociUiManager flociUiManager;
private final InitLifecycleState initLifecycleState;
private final SchemaCreationWorker schemaCreationWorker;
private final StepFunctionsService stepFunctionsService;
private final jakarta.enterprise.inject.Instance<ContainerTeardown> containerTeardowns;
private final PersistentPathValidator persistentPathValidator;
@@ -127,6 +129,7 @@ public class EmulatorLifecycle {
FlociUiManager flociUiManager,
InitLifecycleState initLifecycleState,
SchemaCreationWorker schemaCreationWorker,
StepFunctionsService stepFunctionsService,
jakarta.enterprise.inject.Instance<ContainerTeardown> containerTeardowns,
PersistentPathValidator persistentPathValidator) {
this.storageFactory = storageFactory;
@@ -159,6 +162,7 @@ public class EmulatorLifecycle {
this.flociUiManager = flociUiManager;
this.initLifecycleState = initLifecycleState;
this.schemaCreationWorker = schemaCreationWorker;
this.stepFunctionsService = stepFunctionsService;
this.containerTeardowns = containerTeardowns;
this.persistentPathValidator = persistentPathValidator;
}
@@ -191,6 +195,7 @@ public class EmulatorLifecycle {
}
schemaCreationWorker.recoverOrphans();
schemaCreationWorker.rehydrateSchemas();
stepFunctionsService.abortAbandonedExecutions();
sqsPoller.startPersistedPollers();
kinesisPoller.startPersistedPollers();
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.resource.ExplorerResource;
import io.github.hectorvent.floci.core.resource.ResourceProvider;
import io.github.hectorvent.floci.core.resource.SupportedResourceType;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import com.fasterxml.jackson.core.type.TypeReference;
@@ -43,7 +44,8 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
private static final Logger LOG = Logger.getLogger(StepFunctionsService.class);
private final StorageBackend<String, StateMachine> stateMachineStore;
private final StorageBackend<String, Execution> executionStore;
// Account-aware: the startup sweep has no request context and must reach every account.
private final AccountAwareStorageBackend<Execution> executionStore;
private final StorageBackend<String, Activity> activityStore;
private final StorageBackend<String, MapRun> mapRunStore;
private final Map<String, ExecutionHistory> historyCache = new ConcurrentHashMap<>();
@@ -705,36 +707,86 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
/**
* Aborts a running execution. The caller's {@code error} and {@code cause} land on the
* Execution itself, not only on the history event, so DescribeExecution reports them.
*
* <p>The worker thread may still be inside a state when this runs. It shares this Execution
* instance and reads the status it publishes here, so the writes are made under the same
* monitor the worker's terminal write takes: ABORTED is the status that stands.
*
* <p>ExecutionAborted seals the history for the same reason: the worker still has the state it
* is inside left to record, and those events belong to an execution the caller has already
* been told is finished.
* {@link #markAborted} carries the terminal write and the reasons it is made under the
* Execution's own monitor.
*/
public void stopExecution(String arn, String cause, String error) {
Execution exec = describeExecution(arn);
if (!markAborted(arn, exec, error, cause)) {
return;
}
executionStore.put(arn, exec);
}
/**
* Retires the executions a restart abandoned: they came back from storage as RUNNING with no
* worker behind them, and their only other writers, the worker and StopExecution, are gone.
* Called once at startup, before any request is served, so it takes no lock beyond the one
* {@link #markAborted} takes on each Execution.
*
* <p>Scans every account and writes each execution back under the account that owns it: startup
* has no request context, so the account-scoped accessors would silently cover only the
* configured default account.
*
* <p>Aborts with no error and no cause, the shape AWS returns for StopExecution called without
* them: the status is the whole report. The WARN below is where the reason lives.
*/
public void abortAbandonedExecutions() {
int abandonedCount = 0;
for (AccountAwareStorageBackend.AccountEntry<Execution> entry
: executionStore.scanAllAccountEntries(key -> true)) {
if (!markAborted(entry.key(), entry.value(), null, null)) {
continue;
}
executionStore.putForAccount(entry.accountId(), entry.key(), entry.value());
abandonedCount++;
}
if (abandonedCount > 0) {
LOG.warnv("Aborted {0} Step Functions execution(s) left RUNNING by a restart",
abandonedCount);
}
}
/**
* Writes the terminal ABORTED status on a running execution and seals the history filed under
* {@code arn}, returning false when the execution is already terminal. The key is the caller's
* because it is the one GetExecutionHistory looks the history up by: StopExecution has the ARN
* it was called with, the startup sweep has the storage key the entry came under. Persisting
* the execution is the caller's too, because StopExecution writes it under the caller's account
* and the sweep writes it under the account that owns the entry.
*
* <p>The worker thread may still be inside a state when StopExecution runs. It shares this
* Execution instance and reads the status published here, so the writes are made under the same
* monitor the worker's terminal write takes: ABORTED is the status that stands.
*
* <p>ExecutionAborted seals the history for the same reason: the worker still has the state it
* is inside left to record, and those events belong to an execution the caller has already been
* told is finished.
*/
private boolean markAborted(String arn, Execution exec, String error, String cause) {
synchronized (exec) {
if (!"RUNNING".equals(exec.getStatus())) {
return;
return false;
}
exec.setError(error);
exec.setCause(cause);
exec.setStopDate(System.currentTimeMillis() / 1000.0);
exec.setStatus("ABORTED");
}
executionStore.put(arn, exec);
Map<String, Object> details = new HashMap<>();
if (error != null) details.put("error", error);
if (cause != null) details.put("cause", cause);
if (error != null) {
details.put("error", error);
}
if (cause != null) {
details.put("cause", cause);
}
// An execution can outlive its history: the executions are stored, the histories are held in
// memory only, so a restart in persistent mode brings a RUNNING execution back with nothing
// behind it. The abort still gets recorded, against a history that starts here.
historyCache.computeIfAbsent(arn, key -> new ExecutionHistory())
.sealWith("ExecutionAborted", details);
return true;
}
public List<HistoryEvent> getExecutionHistory(String arn) {