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(lambda): report a bare-string RIE timeout as a function error (#1855)
Younes Beriane0fea8e368d39
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.pyindex 9ee26f0c..6da9e6d3 100644--- a/ministack/services/lambda_svc.py+++ b/ministack/services/lambda_svc.py@@ -3845,6 +3845,11 @@ def _docker_cp_dir(container, src_dir: str, dest_dir: str, arcname: str = "."):container.put_archive(dest_dir, buf)+# Bare-text body the RIE answers with (HTTP 200) when a run hits the function+# timeout (#1845); it is not a JSON error payload.+_RIE_TIMEOUT_TEXT_RE = re.compile(r"Task timed out after \d+\.\d\d seconds")++def _classify_function_error(parsed, err_header: str) -> str | None:"""Classify an RIE response body/header as a function error.@@ -3872,6 +3877,8 @@ def _classify_function_error(parsed, err_header: str) -> str | None:"""if err_header:return "Unhandled"+ if isinstance(parsed, str) and _RIE_TIMEOUT_TEXT_RE.fullmatch(parsed):+ return "Unhandled"if not (isinstance(parsed, dict) and parsed.get("errorType")):return Nonereturn "Handled" if "statusCode" in parsed else "Unhandled"@@ -4008,6 +4015,9 @@ def _invoke_rie(container, event: dict, timeout: int) -> dict:if function_error is not None:result["error"] = Trueresult["function_error"] = function_error+ if isinstance(parsed, str):+ # A bare timeout string: same shape as _rie_terminal_result.+ result["body"] = {"errorMessage": parsed, "errorType": "Runtime.ExitError"}return resultexcept (urllib.error.URLError, ConnectionRefusedError, OSError) as exc:if not _rie_failure_is_retryable(exc):diff --git a/tests/test_lambda.py b/tests/test_lambda.pyindex 74432706..ab7cfba7 100644--- a/tests/test_lambda.py+++ b/tests/test_lambda.py@@ -14093,3 +14093,37 @@ def test_proxy_via_apigw_aws_proxy_integration(proxy_server):forwarded = json.loads(_ProxyHandler.received[-1]["body"])assert forwarded.get("rawPath") == "/hello"assert forwarded.get("requestContext", {}).get("http", {}).get("method") == "GET"+++def test_invoke_rie_reports_a_bare_string_timeout_as_a_function_error():+ """RIE can end a run with a plain-text "Task timed out" body and HTTP 200.++ That body is not JSON, so it reached callers as a successful payload:+ Step Functions recorded TaskSucceeded and never ran Catch. It must take+ the same shape as the read-timeout path.+ """+ from ministack.services.lambda_svc import _invoke_rie++ class _FakeResp:+ headers = {}++ def read(self):+ return b"Task timed out after 300.00 seconds"++ with patch("urllib.request.urlopen", lambda req, timeout=None: _FakeResp()):+ result = _invoke_rie(_RieFakeContainer(), {"k": "v"}, timeout=900)++ assert result["error"] is True+ assert result["function_error"] == "Unhandled"+ assert result["body"] == {+ "errorMessage": "Task timed out after 300.00 seconds",+ "errorType": "Runtime.ExitError",+ }+++def test_classify_function_error_bare_timeout_string_is_unhandled():+ import ministack.services.lambda_svc as lsvc++ assert lsvc._classify_function_error("Task timed out after 300.00 seconds", "") == "Unhandled"+ # Only the exact runtime message counts; other handler strings stay successes.+ assert lsvc._classify_function_error("the Task timed out after 3.00 seconds today", "") is None