Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(stepfunctions): write state events for the states inside a Map or a Parallel branch (#2446)
mishukdutta-cz81ce23c31a97
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 41738cef..8f85a995 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,6 +14,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).- **Athena — the result file is saved at the reported `OutputLocation`** — a query's `<id>.csv` and `<id>.csv.metadata` are now saved directly under the result location, at the path that `GetQueryExecution` reports, as AWS does. Before, they were saved one folder too deep, at `<location>/<id>.csv/<id>.csv`, so a client that read the reported path got `NoSuchKey`.- **Athena — an enforced workgroup overrides the client-side result location** — when a workgroup has `EnforceWorkGroupConfiguration: true`, `StartQueryExecution` now writes results to the workgroup's `OutputLocation`, even when the request gives its own `ResultConfiguration.OutputLocation`, as AWS does. Before, the request's location was used.- **Lambda — a partial ARN as the `FunctionName`** — `123456789012:function:my-function[:qualifier]` resolved to the account ID as the function name, so `Invoke` and the other function operations failed with `Function not found: ...:function:123456789012`; it now resolves as the full ARN in the caller's region, as on AWS, and gives the same result as that full ARN.+- **Step Functions — state events inside a Map or a Parallel branch** — the states in an inline Map item or a Parallel branch wrote no `<Type>StateEntered` or `<Type>StateExited` events, so `GetExecutionHistory` did not show them. They are now in the execution history, as on AWS; for a Distributed Map they are in each child execution's history and not in the parent's. Events added at the same time from several Map items or branches can no longer get the same `id`.## [1.5.33] — 2026-10-26### Addeddiff --git a/ministack/services/stepfunctions.py b/ministack/services/stepfunctions.pyindex 3b1161a2..2f1b0120 100644--- a/ministack/services/stepfunctions.py+++ b/ministack/services/stepfunctions.py@@ -138,6 +138,7 @@ _executions = AccountRegionScopedDict()_HISTORY_TOKEN_TTL_SECONDS = 24 * 60 * 60_history_page_tokens = {}_history_page_tokens_lock = threading.Lock()+_events_lock = threading.Lock()_task_tokens = AccountRegionScopedDict()_tags = AccountRegionScopedDict()_activities = AccountRegionScopedDict()@@ -1432,14 +1433,16 @@ def _invalid_tag_resource_arn(arn):# ---------------------------------------------------------------------------def _add_event(execution, event_type, details=None):- event = {- "id": len(execution["events"]) + 1,- "type": event_type,- "timestamp": now_iso(),- }- if details:- event.update(details)- execution["events"].append(event)+ # Map items and Parallel branches add events from several threads+ with _events_lock:+ event = {+ "id": len(execution["events"]) + 1,+ "type": event_type,+ "timestamp": now_iso(),+ }+ if details:+ event.update(details)+ execution["events"].append(event)return event@@ -2889,9 +2892,23 @@ def _run_sub_machine(states, start_at, input_data, execution, ctx):state_type = state_def.get("Type")ctx["State"] = {"Name": current_name, "EnteredTime": now_iso(), "RetryCount": 0}+ _add_event(execution, f"{state_type}StateEntered", {+ "stateEnteredEventDetails": {+ "name": current_name,+ "input": json.dumps(current_input),+ },+ })+if state_type == "Succeed":- return _apply_output_path(state_def,- _apply_input_path(state_def, current_input, ctx), ctx)+ current_input = _apply_input_path(state_def, current_input, ctx)+ current_input = _apply_output_path(state_def, current_input, ctx)+ _add_event(execution, "SucceedStateExited", {+ "stateExitedEventDetails": {+ "name": current_name,+ "output": json.dumps(current_input),+ },+ })+ return current_inputif state_type == "Fail":raise _ExecutionError(*_fail_state_error(state_def, current_input, ctx))@@ -2909,12 +2926,20 @@ def _run_sub_machine(states, start_at, input_data, execution, ctx):"States.Runtime", f"Unknown state type: {state_type}")if state_type in ("Task", "Parallel", "Map"):- current_input, current_name = handler_fn(+ current_input, next_name = handler_fn(state_def, current_input, execution, ctx)else:- current_input, current_name = handler_fn(+ current_input, next_name = handler_fn(state_def, current_input, ctx)+ _add_event(execution, f"{state_type}StateExited", {+ "stateExitedEventDetails": {+ "name": current_name,+ "output": json.dumps(current_input),+ },+ })+ current_name = next_name+return current_inputdiff --git a/tests/test_stepfunctions.py b/tests/test_stepfunctions.pyindex 4abf6c33..febbedc7 100644--- a/tests/test_stepfunctions.py+++ b/tests/test_stepfunctions.py@@ -10154,3 +10154,73 @@ def test_sfn_distributed_map_runs_child_executions_of_a_map_run(sfn, s3):_, _, desc = run("dmap-list", {"ItemReader": list_reader}, {})assert [o["Key"] for o in json.loads(desc["output"])] == ["items.json", "rows.csv"]assert set(json.loads(desc["output"])[0]) == {"Etag", "Key", "LastModified", "Size", "StorageClass"}+++def _history(sfn, definition):+ arn = sfn.create_state_machine(name=f"sfn-inner-events-{_uuid_mod.uuid4().hex[:8]}",+ definition=json.dumps(definition),+ roleArn="arn:aws:iam::000000000000:role/R")["stateMachineArn"]+ execution = sfn.start_execution(stateMachineArn=arn, input=json.dumps({"items": [1, 2, 3]}))["executionArn"]+ assert _wait_sfn(sfn, execution, timeout=20)["status"] == "SUCCEEDED"+ return execution, sfn.get_execution_history(executionArn=execution, maxResults=1000)["events"]+++def _inner_events(events, state_name):+ return [e["type"] for e in events+ if (e.get("stateEnteredEventDetails") or e.get("stateExitedEventDetails") or {}).get("name") == state_name]+++def test_sfn_inline_map_writes_inner_state_events_to_the_execution_history(sfn):+ definition = {"StartAt": "Map", "States": {"Map": {+ "Type": "Map", "ItemsPath": "$.items", "End": True,+ "ItemProcessor": {"StartAt": "Inner", "States": {"Inner": {"Type": "Pass", "End": True}}},+ }}}+ _, events = _history(sfn, definition)+ assert sorted(_inner_events(events, "Inner")) == ["PassStateEntered"] * 3 + ["PassStateExited"] * 3+ entered = [e for e in events if e["type"] == "PassStateEntered"]+ assert sorted(json.loads(e["stateEnteredEventDetails"]["input"]) for e in entered) == [1, 2, 3]+++def test_sfn_parallel_writes_inner_state_events_to_the_execution_history(sfn):+ definition = {"StartAt": "Par", "States": {"Par": {"Type": "Parallel", "End": True, "Branches": [+ {"StartAt": "Left", "States": {"Left": {"Type": "Pass", "End": True}}},+ {"StartAt": "Right", "States": {"Right": {"Type": "Succeed"}}},+ ]}}}+ _, events = _history(sfn, definition)+ assert _inner_events(events, "Left") == ["PassStateEntered", "PassStateExited"]+ assert _inner_events(events, "Right") == ["SucceedStateEntered", "SucceedStateExited"]+++def test_sfn_distributed_map_writes_inner_state_events_to_the_child_executions(sfn):+ definition = {"StartAt": "Map", "States": {"Map": {+ "Type": "Map", "ItemsPath": "$.items", "End": True,+ "ItemProcessor": {"ProcessorConfig": {"Mode": "DISTRIBUTED", "ExecutionType": "STANDARD"},+ "StartAt": "Inner", "States": {"Inner": {"Type": "Pass", "End": True}}},+ }}}+ execution, events = _history(sfn, definition)+ assert _inner_events(events, "Inner") == []+ (map_run,) = sfn.list_map_runs(executionArn=execution)["mapRuns"]+ children = sfn.list_executions(mapRunArn=map_run["mapRunArn"])["executions"]+ assert len(children) == 3+ for child in children:+ child_events = sfn.get_execution_history(executionArn=child["executionArn"])["events"]+ assert _inner_events(child_events, "Inner") == ["PassStateEntered", "PassStateExited"]+++def test_sfn_concurrent_map_items_get_unique_sequential_event_ids(sfn):+ definition = {"StartAt": "Map", "States": {"Map": {+ "Type": "Map", "ItemsPath": "$.items", "End": True,+ "ItemProcessor": {"StartAt": "A", "States": {+ "A": {"Type": "Pass", "Next": "B"},+ "B": {"Type": "Pass", "Next": "C"},+ "C": {"Type": "Pass", "End": True},+ }},+ }}}+ arn = sfn.create_state_machine(name=f"sfn-event-ids-{_uuid_mod.uuid4().hex[:8]}",+ definition=json.dumps(definition),+ roleArn="arn:aws:iam::000000000000:role/R")["stateMachineArn"]+ execution = sfn.start_execution(stateMachineArn=arn,+ input=json.dumps({"items": list(range(50))}))["executionArn"]+ assert _wait_sfn(sfn, execution, timeout=20)["status"] == "SUCCEEDED"+ events = sfn.get_execution_history(executionArn=execution, maxResults=1000)["events"]+ assert [e["id"] for e in events] == list(range(1, len(events) + 1))