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): $$.State.RetryCount counts a state's retries (#2070)
Jeff Heller16945fcefbe3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 1ae3640b..3a5c600e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -21,6 +21,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).- **IoT — `DescribeCertificate` reports `certificateMode`** — the field was missing from the description. It is now `SNI_ONLY` for a certificate registered with `RegisterCertificateWithoutCA` and `DEFAULT` for the others, as AWS reports it. Contributed by @iot-rocket.### Fixed+- **Step Functions — `$$.State.RetryCount`** — the Context Object's `State` carried only `Name` and `EnteredTime`, so `"x.$": "$$.State.RetryCount"` resolved to `null`, and a Task's `Parameters` were evaluated once, so a retry resent the first attempt's values. `State.RetryCount` (a number in the Context Object documentation) is now `0` when a state is entered and counts its retries (Task, Parallel and Map), and a Task's `Parameters` are evaluated for each attempt, as JSONata `Arguments` already were.- **Step Functions — `StopExecution` keeps its `error` and `cause`** — they were recorded only in the `ExecutionAborted` history event, so `DescribeExecution` of the aborted execution returned neither. They are now kept on the execution and returned by `DescribeExecution`; AWS returns the `cause` of an execution stopped with one.- **Step Functions — the first matching retrier decides** — a retrier that had used its `MaxAttempts` (or had `MaxAttempts: 0`) was skipped, so a later matching retrier such as `States.ALL` retried the error anyway. The first retrier whose `ErrorEquals` matches now decides: once it has used its attempts the state fails (or goes to its `Catch`), as the Developer Guide's error-handling examples describe.- **S3 — cross-account replication** — `PutBucketReplication` dropped `Destination.Account` and `AccessControlTranslation`, and the replica write looked the destination bucket up only in the caller's account, so a destination owned by another account stamped every object `FAILED`. The destination is now found by its bucket name in any account and the replica (object, versions, tags, annotations, on-disk files) lands in the owner's scope. A cross-account destination requires its bucket policy to grant the rule's `Role` `s3:ReplicateObject`, plus `s3:ObjectOwnerOverrideToBucketOwner` when `AccessControlTranslation` is set and the destination is not `BucketOwnerEnforced`; otherwise the source reports `FAILED`. `PutBucketReplication` / `GetBucketReplication` round-trip `Filter` (`Prefix`, `Tag`, `And`), `Priority`, `DeleteMarkerReplication`, `Destination.Account` and `AccessControlTranslation`, and the destination-versioning check at configuration time sees buckets in any account. Contributed by @pingedbrain.diff --git a/ministack/services/stepfunctions.py b/ministack/services/stepfunctions.pyindex c191d4d9..3a6f6520 100644--- a/ministack/services/stepfunctions.py+++ b/ministack/services/stepfunctions.py@@ -1524,7 +1524,7 @@ def _run_execution(exec_arn):"States.Runtime",f"State '{current_name}' not found in definition")- ctx["State"] = {"Name": current_name, "EnteredTime": now_iso()}+ ctx["State"] = {"Name": current_name, "EnteredTime": now_iso(), "RetryCount": 0}state_type = state_def.get("Type")_add_event(execution, f"{state_type}StateEntered", {@@ -1679,17 +1679,16 @@ def _execute_task(state_def, raw_input, execution, ctx):if is_callback:ctx["Task"] = {"Token": new_uuid()}- effective = None- if query_language != "JSONata":- effective = _apply_input_path(state_def, raw_input, ctx)- effective = _apply_parameters(state_def, effective, ctx)-retriers = state_def.get("Retry", [])catchers = state_def.get("Catch", [])retry_counts: dict = {}last_error: _ExecutionError | None = Nonewhile True:+ # Each attempt sees its own $$.State.RetryCount.+ ctx["State"]["RetryCount"] = sum(retry_counts.values())+ if query_language != "JSONata":+ effective = _apply_parameters(state_def, _apply_input_path(state_def, raw_input, ctx), ctx)try:if query_language == "JSONata":effective = _apply_jsonata_arguments(state_def, raw_input, ctx)@@ -2194,8 +2193,11 @@ def _sleep_until(iso_ts):def _with_retry_and_catch(state_def, raw_input, ctx, attempt):"""Run a Parallel or Map attempt under the state's Retry and Catch fields."""+ state = ctx["State"]retry_counts: dict = {}while True:+ # Branches share ctx and leave their own State in it; each attempt starts from this state's, counted.+ ctx["State"] = {**state, "RetryCount": sum(retry_counts.values())}try:return attempt()except _ExecutionError as err:@@ -2348,7 +2350,7 @@ def _run_sub_machine(states, start_at, input_data, execution, ctx):"States.Runtime", f"State '{current_name}' not found")state_type = state_def.get("Type")- ctx["State"] = {"Name": current_name, "EnteredTime": now_iso()}+ ctx["State"] = {"Name": current_name, "EnteredTime": now_iso(), "RetryCount": 0}if state_type == "Succeed":return _apply_output_path(state_def,diff --git a/tests/test_stepfunctions.py b/tests/test_stepfunctions.pyindex 15de58b8..671f234d 100644--- a/tests/test_stepfunctions.py+++ b/tests/test_stepfunctions.py@@ -3496,29 +3496,40 @@ def test_sfn_task_failed_does_not_match_states_timeout(sfn, sqs):assert [e["type"] for e in events].count("TaskScheduled") == 1-def test_sfn_exhausted_retrier_does_not_fall_through_to_a_later_one(sfn, lam):- """The first matching retrier decides: MaxAttempts 0 never retries, even with States.ALL after it."""- fn = f"sfn-first-retrier-{time.time_ns()}"+def test_sfn_task_parameters_see_state_retry_count(sfn, lam):+ """$$.State.RetryCount is 0 on a state's first attempt and counts its retries (a Task's or a Parallel's);+ the next state starts at 0."""+ fn = f"sfn-retry-count-{time.time_ns()}"lam.create_function(FunctionName=fn, Runtime="python3.12", Role=_LAMBDA_ROLE, Handler="index.handler",- Code={"ZipFile": _make_zip("def handler(e, c): raise Exception('no')\n")},+ Code={"ZipFile": _make_zip(+ "def handler(e, c):\n"+ " if e['fail_until'] > e['retry_count']: raise Exception(str(e['retry_count']))\n"+ " return e['retry_count']\n")},)+ arn = lam.get_function(FunctionName=fn)["Configuration"]["FunctionArn"]++ retry = [{"ErrorEquals": ["States.ALL"], "IntervalSeconds": 1, "MaxAttempts": 2}]++ def task(fail_until, result_path, nxt):+ return {"Type": "Task", "Resource": arn, "ResultPath": result_path,+ "Parameters": {"fail_until": fail_until, "retry_count.$": "$$.State.RetryCount"}, "Retry": retry, **nxt}+sm = sfn.create_state_machine(name=fn, roleArn="arn:aws:iam::000000000000:role/R",- definition=json.dumps({"StartAt": "Call", "States": {- "Call": {"Type": "Task", "Resource": lam.get_function(FunctionName=fn)["Configuration"]["FunctionArn"],- "Retry": [{"ErrorEquals": ["States.TaskFailed"], "MaxAttempts": 0},- {"ErrorEquals": ["States.ALL"], "IntervalSeconds": 1, "MaxAttempts": 2}],- "Catch": [{"ErrorEquals": ["States.ALL"], "Next": "Caught"}], "End": True},- "Caught": {"Type": "Pass", "End": True},+ definition=json.dumps({"StartAt": "Retried", "States": {+ "Retried": task(1, "$.retried", {"Next": "Parallel"}),+ "Parallel": {"Type": "Parallel", "ResultPath": "$.parallel", "Retry": retry, "Next": "Next",+ "Parameters": {"fail_until": 1, "retry_count.$": "$$.State.RetryCount"},+ "Branches": [{"StartAt": "Call", "States": {"Call": {"Type": "Task", "Resource": arn, "End": True}}}]},+ "Next": task(0, "$.next", {"End": True}),}}),)ex = sfn.start_execution(stateMachineArn=sm["stateMachineArn"], input="{}")desc = _wait_sfn(sfn, ex["executionArn"], timeout=30)assert desc["status"] == "SUCCEEDED", desc.get("cause")- events = sfn.get_execution_history(executionArn=ex["executionArn"])["events"]- assert [e["type"] for e in events].count("TaskScheduled") == 1+ assert json.loads(desc["output"]) == {"retried": 1, "parallel": [1], "next": 0}_SFN_FAIL_BRANCH = {"StartAt": "F", "States": {"F": {"Type": "Fail", "Error": "MyErr", "Cause": "c"}}}