Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(lambda): run durable invocations on the warm pool (#1703)

iot-rocketf9286ee8596c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fe130095..ca626a72 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,13 +10,15 @@ Versioning follows [Semantic Versioning](https://semver.org/).
### Fixed
- **S3 — the `s3:TestEvent` no longer reaches Lambda targets** — `PutBucketNotificationConfiguration` fanned the test event out to every destination, Lambda included, but AWS sends it to SQS queues and SNS topics only; a Lambda destination is verified through its function permissions, not by invoking it. The payload carries no `Records` array, so each S3-triggered function raised on it, and because delivery goes through the async path the event was retried to `MaximumRetryAttempts` (default 2) and then routed to the function's DLQ or `OnFailure` destination, where a synthetic event AWS never sends looked like a genuinely dropped message. Any CDK stack with an S3-triggered Lambda hit this through `BucketNotificationsHandler`. Queue and topic destinations still receive the test event, and real object events still invoke the function. Contributed by @ppettitau.
### Changed
-- **Lambda — warm local custom runtimes** — `provided.*` bootstraps now reuse the existing subprocess worker pool instead of restarting on every invocation. Each invocation receives its request metadata through the Lambda Runtime API, concurrent calls use separate workers, and failed environments are cleaned up before reuse. Durable invocations retain their one-shot executor; Docker, image, and proxy execution are unchanged.
+- **Lambda — warm local custom runtimes** — `provided.*` bootstraps now reuse the existing subprocess worker pool instead of restarting on every invocation. Each invocation receives its request metadata through the Lambda Runtime API, concurrent calls use separate workers, and failed environments are cleaned up before reuse. Durable `provided.*` invocations retain their one-shot executor, because a bootstrap process reads its environment once, at spawn; Docker, image, and proxy execution are unchanged.
### Fixed
- **CloudFormation — an `AWS::ApiGateway::Method`'s `MethodResponses` and `IntegrationResponses` are provisioned** — the provisioner read the method and integration properties and discarded both response lists, so every REST API deployed from a template lost its mapped response headers. The visible casualty was the CDK's `defaultCorsPreflightOptions`, whose generated `OPTIONS` method returns the `Access-Control-Allow-*` headers through a MOCK integration's `responseParameters`: the preflight answered without a single CORS header and the browser blocked the request. That became load-bearing once `COGNITO_USER_POOLS` authorizers were enforced in 1.5.10, because the preflight can no longer fall through to a proxy integration and be answered by the application's own CORS middleware. Both lists are now provisioned onto the method, where the existing request-time mapping picks them up, and a stack update reprovisions them from the template. Contributed by @ppettitau.
- **Transcribe — `StartTranscriptionJob` returns a job already `IN_PROGRESS`** — the start response reported `QUEUED` with no `StartTime`, and the job reached `IN_PROGRESS` only from the background worker, after the response had been sent. A caller that reads the status off the start response and then waits for the terminal `Transcribe Job State Change` event, rather than polling, therefore never saw an in-progress state at all: the job went straight from queued to complete. Jobs now start `IN_PROGRESS` with `StartTime` set, matching the response the AWS CLI walkthrough documents, and `TRANSCRIBE_JOB_RUN_SECONDS` covers the whole run rather than half of it. `QUEUED`, which AWS reaches only for a request that opted into job queueing through `JobExecutionSettings.AllowDeferredExecution` while the account is at its concurrent job limit, stays reachable through the new `TRANSCRIBE_JOB_QUEUE_SECONDS` (default `0`); both knobs are settable at runtime through `/_ministack/config`. Contributed by @ppettitau.
+- **Lambda — a durable invocation runs on the warm pool** — durable Python and Node.js invocations were sent to the one-shot executor, because the three `AWS_LAMBDA_DURABLE_*` variables change on every call while a pooled worker's environment is fixed when it spawns. Each of them therefore paid a fresh interpreter start and the botocore import inside the function's `Timeout`, whose default is 3 seconds, so on a loaded machine every durable invocation ended as a timeout. The values now travel in the event, the channel the X-Ray trace header and the invoke-depth counter already use, and both worker bootstraps move them into the environment before the handler runs and drop them again when the next invocation is not durable. Contributed by @iot-rocket.
+
## [1.5.10] — 2026-09-10
### Added
diff --git a/README.md b/README.md
index b9fa79ed..ae6bf3ce 100644
--- a/README.md
+++ b/README.md
@@ -1102,7 +1102,7 @@ aws --endpoint-url=http://localhost:4566 eks delete-cluster --name my-cluster
MiniStack keeps Python, Node.js, and `provided.*` Lambda functions warm between invocations with the local executor. After the first call (cold start), the handler module or custom-runtime bootstrap stays alive in a persistent subprocess. Subsequent calls skip initialization, matching real AWS warm-start behaviour and making test suites significantly faster.
-Custom runtimes receive each event through the Lambda Runtime API's long-polling `/runtime/invocation/next` endpoint, with a fresh request ID, deadline, and X-Ray trace header. Concurrent invocations lease separate workers; function updates and deletion invalidate the workers. A timeout or crashed bootstrap is discarded so the next invocation starts a fresh process. Durable invocations retain the one-shot executor because their execution context changes per call. Docker, container-image, and proxy execution are unchanged.
+Custom runtimes receive each event through the Lambda Runtime API's long-polling `/runtime/invocation/next` endpoint, with a fresh request ID, deadline, and X-Ray trace header. Concurrent invocations lease separate workers; function updates and deletion invalidate the workers. A timeout or crashed bootstrap is discarded so the next invocation starts a fresh process. Durable invocations of a custom runtime retain the one-shot executor: a bootstrap's environment is fixed when the process starts, while Python and Node.js carry their per-call durable context in the event and stay on the warm pool. Docker, container-image, and proxy execution are unchanged.
### Lambda Node.js Runtimes
diff --git a/ministack/core/lambda_runtime.py b/ministack/core/lambda_runtime.py
index 26a54d1d..45c24d29 100644
--- a/ministack/core/lambda_runtime.py
+++ b/ministack/core/lambda_runtime.py
@@ -143,13 +143,33 @@ INVOKE_DEPTH_HEADER = "X-Ministack-Invoke-Depth"
# trace header uses.
INVOKE_DEPTH_EVENT_KEY = "_ministack_invoke_depth"
+# The durable-execution variables change on every invocation of the same
+# function, so a pooled worker cannot carry them in its spawn environment
+# either. They ride in the event under this key, as a mapping of the three
+# names below, and both worker bootstraps move them into the environment
+# before the handler runs.
+#
+# One declaration for both readers: lambda_svc builds the overlay from the
+# durable context under these keys, the bootstraps iterate the names to set
+# them and to delete them again. A fourth variable added to only one of the
+# two would be set and never cleared, which is the leak the event carrier
+# exists to avoid.
+DURABLE_CTX_EVENT_KEY = "_ministack_durable_ctx"
+DURABLE_ENV_VARS = {
+ "AWS_LAMBDA_DURABLE_EXECUTION_ARN": "arn",
+ "AWS_LAMBDA_DURABLE_CHECKPOINT_TOKEN": "token",
+ "AWS_LAMBDA_DURABLE_EXECUTION_NAME": "name",
+}
+
-def _sub_depth_tokens(script: str) -> str:
- """Substitute the invoke-depth carrier names into a bootstrap script."""
+def _sub_runtime_tokens(script: str) -> str:
+ """Substitute the per-invocation carrier names into a bootstrap script."""
return (
script.replace("__DEPTH_ENV__", INVOKE_DEPTH_ENV)
.replace("__DEPTH_HEADER__", INVOKE_DEPTH_HEADER)
.replace("__DEPTH_EVENT_KEY__", INVOKE_DEPTH_EVENT_KEY)
+ .replace("__DURABLE_EVENT_KEY__", DURABLE_CTX_EVENT_KEY)
+ .replace("__DURABLE_ENV_VARS__", json.dumps(list(DURABLE_ENV_VARS)))
)
@@ -163,11 +183,12 @@ def _sub_depth_tokens(script: str) -> str:
# handler alongside it for ministack's depth counter.
#
# Importing botocore is what a cold start pays for the counter (~0.15s here),
-# once per worker — or once per invocation on the one-shot executor, which
-# only durable invocations use. A function whose environment has no botocore
-# is not instrumented: it would have to be calling Invoke over raw urllib,
-# which never carried lineage anyway.
-INVOKE_DEPTH_BOOTSTRAP = _sub_depth_tokens('''
+# once per worker. The one-shot executor's wrapper carries the same bootstrap,
+# but nothing reaches it any more: python and nodejs go to the pool, and every
+# other runtime gets a mock answer before the subprocess starts. A function
+# whose environment has no botocore is not instrumented: it would have to be
+# calling Invoke over raw urllib, which never carried lineage anyway.
+INVOKE_DEPTH_BOOTSTRAP = _sub_runtime_tokens('''
def _ms_install_invoke_depth():
import os
try:
@@ -187,7 +208,7 @@ def _ms_install_invoke_depth():
# Python worker script (runs inside a persistent subprocess)
# ---------------------------------------------------------------------------
-_PYTHON_WORKER_SCRIPT = INVOKE_DEPTH_BOOTSTRAP + _sub_depth_tokens('''
+_PYTHON_WORKER_SCRIPT = INVOKE_DEPTH_BOOTSTRAP + _sub_runtime_tokens('''
import sys, json, importlib, traceback, os, time
def run():
@@ -269,6 +290,15 @@ def run():
os.environ["__DEPTH_ENV__"] = str(_ms_depth)
elif "__DEPTH_ENV__" in os.environ:
del os.environ["__DEPTH_ENV__"]
+ # Durable execution: the ARN, the checkpoint token and the execution
+ # name belong to this invocation only, so they arrive with the event
+ # and are dropped again when the next one is not durable.
+ _ms_durable = event.pop("__DURABLE_EVENT_KEY__", None) or {}
+ for _ms_var in __DURABLE_ENV_VARS__:
+ if _ms_var in _ms_durable:
+ os.environ[_ms_var] = str(_ms_durable[_ms_var])
+ elif _ms_var in os.environ:
+ del os.environ[_ms_var]
_function_name = init.get("function_name", "")
_deadline = time.time() + float(os.environ.get("_LAMBDA_TIMEOUT", "3"))
context = type("Context", (), {
@@ -297,7 +327,7 @@ run()
# Node.js worker script (runs inside a persistent subprocess)
# ---------------------------------------------------------------------------
-_NODEJS_WORKER_SCRIPT = _sub_depth_tokens(r'''
+_NODEJS_WORKER_SCRIPT = _sub_runtime_tokens(r'''
const readline = require("readline");
const path = require("path");
const http = require("http");
@@ -933,6 +963,17 @@ rl.on("line", async (line) => {
} else if ("__DEPTH_ENV__" in process.env) {
delete process.env.__DEPTH_ENV__;
}
+ // Durable execution: per-invocation values, so they ride in the event and
+ // are dropped again when the next invocation is not durable.
+ const _msDurable = event.__DURABLE_EVENT_KEY__ || {};
+ for (const _msVar of __DURABLE_ENV_VARS__) {
+ if (_msVar in _msDurable) {
+ process.env[_msVar] = String(_msDurable[_msVar]);
+ } else if (_msVar in process.env) {
+ delete process.env[_msVar];
+ }
+ }
+ delete event.__DURABLE_EVENT_KEY__;
delete event.__DEPTH_EVENT_KEY__;
delete event._x_amzn_trace_id;
delete event._request_id;
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.py
index d19cd7d2..dd7b79df 100644
--- a/ministack/services/lambda_svc.py
+++ b/ministack/services/lambda_svc.py
@@ -53,6 +53,8 @@ from ministack.core import container_reaper
from ministack.core.arn import ArnParseError, parse_arn
from ministack.core.concurrency import run_reentrant
from ministack.core.lambda_runtime import (
+ DURABLE_CTX_EVENT_KEY,
+ DURABLE_ENV_VARS,
INVOKE_DEPTH_BOOTSTRAP,
INVOKE_DEPTH_ENV,
INVOKE_DEPTH_EVENT_KEY,
@@ -1308,11 +1310,7 @@ def _durable_env_overlay() -> dict[str, str]:
ctx = _durable_ctx.get()
if not ctx:
return {}
- return {
- "AWS_LAMBDA_DURABLE_EXECUTION_ARN": ctx.get("arn", ""),
- "AWS_LAMBDA_DURABLE_CHECKPOINT_TOKEN": ctx.get("token", ""),
- "AWS_LAMBDA_DURABLE_EXECUTION_NAME": ctx.get("name", ""),
- }
+ return {var: ctx.get(key, "") for var, key in DURABLE_ENV_VARS.items()}
def invoke_durable_resume(function_name: str, durable_arn: str, original_event: dict) -> None:
@@ -4836,25 +4834,19 @@ def _execute_function_dispatch(func: dict, config: dict, event: dict,
else:
runtime = config.get("Runtime", "python3.12")
if runtime.startswith("provided"):
- # Durable invocations need a per-call environment (the
- # DurableExecutionArn / CheckpointToken change every invoke), and a
- # reused environment's env is fixed at spawn — so they keep the
- # one-shot executor, exactly as durable python/nodejs does above.
+ # A durable invocation needs a per-call environment (the
+ # DurableExecutionArn and CheckpointToken change every invoke) and a
+ # pooled worker's env is fixed at spawn, so provided.* durable
+ # invocations keep the one-shot executor. python and nodejs carry
+ # that context in the event instead, which is why they can be pooled.
if _durable_ctx.get():
result = _execute_function_provided(func, event)
else:
result = _execute_function_provided_warm(func, event, request_id)
- elif (runtime.startswith("python") or runtime.startswith("nodejs")) \
- and not _durable_ctx.get():
- # Warm pool reuses worker subprocesses whose env was fixed at
- # spawn time. Durable invocations need per-call env (the
- # DurableExecutionArn + CheckpointToken change every invoke),
- # so route them through the per-call local executor.
- result = _execute_function_warm(func, event)
elif runtime.startswith(("python", "nodejs")):
- # Durable python/nodejs falls through to local subprocess (per
- # the elif above we already filtered durable out of warm).
- result = _execute_function_local(func, event)
+ # Durable invocations included: their per-call context rides in
+ # the event, so the pooled worker can serve them.
+ result = _execute_function_warm(func, event)
else:
# java*/dotnet*/ruby* need the real RIE image — there's no
# in-process executor that can run JVM bytecode or .NET IL.
@@ -5029,6 +5021,14 @@ def _execute_function_warm(func: dict, event: dict) -> dict:
# the counter.
if isinstance(event, dict):
event[INVOKE_DEPTH_EVENT_KEY] = _invoke_depth.get()
+ # Durable executions ride the same channel: the ARN, the checkpoint
+ # token and the execution name differ per invocation, which is why
+ # they cannot be part of the worker's spawn environment. The
+ # bootstrap clears them again when the key is absent, so a worker
+ # reused for a non-durable call does not see the previous one's.
+ durable = _durable_env_overlay()
+ if durable:
+ event[DURABLE_CTX_EVENT_KEY] = durable
result = worker.invoke(event, new_uuid())
if result.get("status") == "ok":
return {"body": result.get("result"), "log": result.get("log", "")}
diff --git a/tests/test_lambda.py b/tests/test_lambda.py
index e99d6183..26a00484 100644
--- a/tests/test_lambda.py
+++ b/tests/test_lambda.py
@@ -8227,6 +8227,111 @@ def handler(event, context):
pass
+def test_lambda_durable_invocation_reuses_the_warm_worker(lam):
+ """Durable invocations go through the warm pool like every other
+ python/nodejs invocation: the second call reuses the worker (module state
+ survives) and still gets its own execution ARN and checkpoint token."""
+ import base64 as _b64
+ import json as _json
+ fname = f"durable-warm-{_uuid_mod.uuid4().hex[:8]}"
+ try:
+ lam.delete_function(FunctionName=fname)
+ except Exception:
+ pass
+ # The counter lives in the module, so it only survives if the same worker
+ # process serves both invocations.
+ code = """
+import os
+_CALLS = 0
+def handler(event, context):
+ global _CALLS
+ _CALLS += 1
+ return {
+ "calls": _CALLS,
+ "arn": os.environ.get("AWS_LAMBDA_DURABLE_EXECUTION_ARN"),
+ "token": os.environ.get("AWS_LAMBDA_DURABLE_CHECKPOINT_TOKEN"),
+ }
+"""
+ zip_b64 = _b64.b64encode(_make_zip(code)).decode()
+ _raw_durable("POST", "/2015-03-31/functions", body={
+ "FunctionName": fname,
+ "Runtime": "python3.12",
+ "Role": _LAMBDA_ROLE,
+ "Handler": "index.handler",
+ "Code": {"ZipFile": zip_b64},
+ # A one-shot subprocess pays interpreter start plus the botocore import
+ # inside this budget; a warm worker pays it once, before the invoke.
+ "Timeout": 1,
+ "DurableConfig": {"Enabled": True},
+ })
+ try:
+ first = _json.loads(lam.invoke(FunctionName=fname, Payload=b"{}")["Payload"].read())
+ second = _json.loads(lam.invoke(FunctionName=fname, Payload=b"{}")["Payload"].read())
+ assert first["calls"] == 1
+ assert second["calls"] == 2, "durable invocation did not reuse the warm worker"
+ assert first["arn"] and second["arn"]
+ assert first["arn"] != second["arn"]
+ assert first["token"] != second["token"]
+ finally:
+ try:
+ lam.delete_function(FunctionName=fname)
+ except Exception:
+ pass
+
+
+def test_lambda_durable_context_is_dropped_when_the_next_invocation_is_not_durable(lam):
+ """A worker that served a durable invocation is reused for a non-durable one
+ of the same function, and the bootstrap deletes the three variables the
+ previous invocation set, so the handler cannot read a stale context."""
+ import base64 as _b64
+ import json as _json
+ fname = f"durable-drop-{_uuid_mod.uuid4().hex[:8]}"
+ try:
+ lam.delete_function(FunctionName=fname)
+ except Exception:
+ pass
+ code = """
+import os
+_CALLS = 0
+_NAMES = (
+ "AWS_LAMBDA_DURABLE_EXECUTION_ARN",
+ "AWS_LAMBDA_DURABLE_CHECKPOINT_TOKEN",
+ "AWS_LAMBDA_DURABLE_EXECUTION_NAME",
+)
+def handler(event, context):
+ global _CALLS
+ _CALLS += 1
+ return {"calls": _CALLS, "env": {n: os.environ.get(n) for n in _NAMES}}
+"""
+ zip_b64 = _b64.b64encode(_make_zip(code)).decode()
+ _raw_durable("POST", "/2015-03-31/functions", body={
+ "FunctionName": fname,
+ "Runtime": "python3.12",
+ "Role": _LAMBDA_ROLE,
+ "Handler": "index.handler",
+ "Code": {"ZipFile": zip_b64},
+ "Timeout": 5,
+ "DurableConfig": {"Enabled": True},
+ })
+ try:
+ first = _json.loads(lam.invoke(FunctionName=fname, Payload=b"{}")["Payload"].read())
+ assert first["calls"] == 1
+ assert all(first["env"].values()), first
+ # Switching the function off durable does not respawn its worker: the
+ # counter must keep counting in the same process.
+ code, body = _raw_durable("PUT", f"/2015-03-31/functions/{fname}/configuration",
+ body={"DurableConfig": {"Enabled": False}})
+ assert code == 200, body
+ second = _json.loads(lam.invoke(FunctionName=fname, Payload=b"{}")["Payload"].read())
+ assert second["calls"] == 2, "the non-durable invocation did not reuse the worker"
+ assert not any(second["env"].values()), second
+ finally:
+ try:
+ lam.delete_function(FunctionName=fname)
+ except Exception:
+ pass
+
+
@pytest.mark.serial
def test_lambda_durable_chained_invoke_runs_child(lam):
"""A CHAINED_INVOKE checkpoint update with Action=START actually spawns
@@ -8248,7 +8353,6 @@ def test_lambda_durable_chained_invoke_runs_child(lam):
"Role": _LAMBDA_ROLE,
"Handler": "index.handler",
"Code": {"ZipFile": _b64.b64encode(_make_zip(child_code)).decode()},
- "Timeout": 30,
})
parent_code = "def handler(e,c): return {}"
_raw_durable("POST", "/2015-03-31/functions", body={
@@ -8258,7 +8362,6 @@ def test_lambda_durable_chained_invoke_runs_child(lam):
"Handler": "index.handler",
"Code": {"ZipFile": _b64.b64encode(_make_zip(parent_code)).decode()},
"DurableConfig": {"Enabled": True},
- "Timeout": 30,
})
try:
# Invoke parent to spin up its durable execution.
@@ -8538,6 +8641,50 @@ def test_lambda_get_state_prunes_orphan_blobs(lambda_svc_isolated):
assert not (blob_dir / f"{hashlib.sha256(old_code).hexdigest()}.zip").exists()
+def test_lambda_durable_invocation_reuses_the_warm_worker_nodejs(lam):
+ """The nodejs bootstrap takes the same durable context off the event: the
+ second call reuses the worker (module state survives) and still gets its
+ own execution ARN and checkpoint token."""
+ import base64 as _b64
+ import json as _json
+ fname = f"durable-warm-js-{_uuid_mod.uuid4().hex[:8]}"
+ try:
+ lam.delete_function(FunctionName=fname)
+ except Exception:
+ pass
+ code = """
+let calls = 0;
+exports.handler = async () => ({
+ calls: ++calls,
+ arn: process.env.AWS_LAMBDA_DURABLE_EXECUTION_ARN,
+ token: process.env.AWS_LAMBDA_DURABLE_CHECKPOINT_TOKEN,
+});
+"""
+ zip_b64 = _b64.b64encode(_make_zip_js(code)).decode()
+ _raw_durable("POST", "/2015-03-31/functions", body={
+ "FunctionName": fname,
+ "Runtime": "nodejs20.x",
+ "Role": _LAMBDA_ROLE,
+ "Handler": "index.handler",
+ "Code": {"ZipFile": zip_b64},
+ "Timeout": 3,
+ "DurableConfig": {"Enabled": True},
+ })
+ try:
+ first = _json.loads(lam.invoke(FunctionName=fname, Payload=b"{}")["Payload"].read())
+ second = _json.loads(lam.invoke(FunctionName=fname, Payload=b"{}")["Payload"].read())
+ assert first["calls"] == 1
+ assert second["calls"] == 2, "durable nodejs invocation did not reuse the warm worker"
+ assert first["arn"] and second["arn"]
+ assert first["arn"] != second["arn"]
+ assert first["token"] != second["token"]
+ finally:
+ try:
+ lam.delete_function(FunctionName=fname)
+ except Exception:
+ pass
+
+
def test_lambda_durable_event_wrapped_with_sdk_fields(lam):
"""A durable invocation's event payload is wrapped with the fields the
aws-durable-execution-sdk-python SDK reads from the Lambda event:
@@ -8563,10 +8710,6 @@ def handler(event, context):
"Handler": "index.handler",
"Code": {"ZipFile": _b64.b64encode(_make_zip(code)).decode()},
"DurableConfig": {"Enabled": True},
- # Lambda's default 3s timeout reads a loaded CI runner's slow
- # subprocess cold start as a function timeout (no SDK fields in the
- # error body); the budget, not the wrapping, is what varies here.
- "Timeout": 30,
})
try:
resp = lam.invoke(FunctionName=fname, Payload=b'{"user":"data"}')
@@ -8574,6 +8717,9 @@ def handler(event, context):
# SDK requires these three top-level keys.
for key in ("DurableExecutionArn", "CheckpointToken", "InitialExecutionState"):
assert key in body["keys"], f"missing {key} in {body['keys']}"
+ # The emulator's own carrier keys (durable context, depth, trace) are
+ # popped by the bootstrap before the handler sees the event.
+ assert not any(k.startswith("_ministack") for k in body["keys"]), body["keys"]
ops = body["event"]["InitialExecutionState"]["Operations"]
# AWS seeds the synthetic EXECUTION-type op with the input payload.
assert len(ops) == 1 and ops[0]["Type"] == "EXECUTION"
diff --git a/tests/test_lambda_provided_dispatch.py b/tests/test_lambda_provided_dispatch.py
index cdda61fe..80b9b265 100644
--- a/tests/test_lambda_provided_dispatch.py
+++ b/tests/test_lambda_provided_dispatch.py
@@ -129,6 +129,68 @@ def test_provided_dispatch_preserves_other_executors(monkeypatch, mode, target):
executors[target].assert_called_once_with(func, {}, "request-1")
[email protected]("runtime", ["python3.12", "nodejs20.x"])
[email protected]("durable", [False, True])
+def test_python_and_nodejs_dispatch_to_the_warm_pool_durable_or_not(
+ monkeypatch, runtime, durable):
+ """The sibling of the provided.* routing above: python and nodejs reach
+ _execute_function_warm whether or not the invocation is durable, because
+ their per-call durable context rides in the event rather than in the
+ worker's spawn environment."""
+ monkeypatch.setattr(lambda_svc, "LAMBDA_EXECUTOR", "local")
+ monkeypatch.setattr(lambda_svc, "LAMBDA_STRICT", False)
+ monkeypatch.setattr(lambda_svc, "_proxy_url_for", lambda config: None)
+ monkeypatch.setattr(lambda_svc, "_emit_lambda_logs", Mock())
+ names = ["_execute_function_warm", "_execute_function_local",
+ "_execute_function_provided", "_execute_function_provided_warm",
+ "_execute_function_docker", "_execute_function_proxy"]
+ executors = {name: Mock(return_value={"body": name}) for name in names}
+ for name, executor in executors.items():
+ monkeypatch.setattr(lambda_svc, name, executor)
+ config = _config()
+ config["Runtime"] = runtime
+ func = {"config": config, "code_zip": b"zip"}
+ token = lambda_svc._durable_ctx.set({"test": True} if durable else None)
+ try:
+ result = lambda_svc._execute_function_dispatch(func, config, {}, "request-1", time.time())
+ finally:
+ lambda_svc._durable_ctx.reset(token)
+ assert result == {"body": "_execute_function_warm"}
+ for name, executor in executors.items():
+ assert executor.call_count == (1 if name == "_execute_function_warm" else 0)
+
+
[email protected]("unavailable", ["sdk", "daemon"])
+def test_docker_unavailable_fallback_carries_the_durable_context(monkeypatch, unavailable):
+ """Both permissive Docker fallbacks send python and nodejs to the warm
+ pool, so the durable context reaches the handler there too. Before the
+ carrier moved into the event they invoked with the three variables unset."""
+ monkeypatch.setattr(lambda_svc, "_docker_available", unavailable != "sdk")
+ monkeypatch.setattr(lambda_svc, "_get_docker_client",
+ lambda: None if unavailable == "daemon" else object())
+ monkeypatch.setattr(lambda_svc, "LAMBDA_STRICT", False)
+ warm = Mock(return_value={"body": "warm"})
+ one_shot = Mock(return_value={"body": "one-shot"})
+ monkeypatch.setattr(lambda_svc, "_execute_function_warm", warm)
+ monkeypatch.setattr(lambda_svc, "_execute_function_local", one_shot)
+ config = _config()
+ config["Runtime"] = "python3.12"
+ func = {"config": config, "code_zip": b"zip"}
+ token = lambda_svc._durable_ctx.set({"arn": "exec-arn", "token": "tok", "name": "exec"})
+ try:
+ assert lambda_svc._execute_function_docker(func, {}) == {"body": "warm"}
+ overlay = lambda_svc._durable_env_overlay()
+ finally:
+ lambda_svc._durable_ctx.reset(token)
+ warm.assert_called_once_with(func, {})
+ one_shot.assert_not_called()
+ assert overlay == {
+ "AWS_LAMBDA_DURABLE_EXECUTION_ARN": "exec-arn",
+ "AWS_LAMBDA_DURABLE_CHECKPOINT_TOKEN": "tok",
+ "AWS_LAMBDA_DURABLE_EXECUTION_NAME": "exec",
+ }
+
+
def test_provided_env_keeps_function_vars_and_endpoint_precedence(monkeypatch):
config = _config()
config.update(MemorySize=256, Environment={"Variables": {