Skip to content

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

feat(var): add LS pair for keepalive (#1333)

Nahuel Nucera5425e5b19f00
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.py
index 6ae8c61b..20475224 100644
--- a/ministack/services/lambda_svc.py
+++ b/ministack/services/lambda_svc.py
@@ -2611,6 +2611,14 @@ _WARM_CONTAINER_TTL = 300 # seconds idle before eviction. AWS doesn't publish
# observations. Can be overridden via env var.
_WARM_CONTAINER_TTL = int(os.environ.get("LAMBDA_WARM_TTL_SECONDS", _WARM_CONTAINER_TTL))
+# LocalStack-compat (not an AWS behavior): LAMBDA_KEEPALIVE_MS=0 forces a cold
+# start after EVERY invocation by tearing the Docker RIE container down on
+# release instead of returning it to the warm pool. This is a test-isolation
+# lever for suites that must not let a prior invocation's warm container reuse
+# in-memory state (e.g. a cached auth token). Any non-zero/unset value keeps the
+# warm-pool behavior; idle eviction is still governed by LAMBDA_WARM_TTL_SECONDS.
+_KEEPALIVE_KILL_ON_RELEASE = os.environ.get("LAMBDA_KEEPALIVE_MS", "").strip() == "0"
+
# Per-function concurrency: only applied when ReservedConcurrentExecutions is
# explicitly set on the function. Otherwise the function can consume the
# full account pool, matching AWS.
@@ -2708,6 +2716,11 @@ def _pool_register(key: str, container, tmpdir) -> dict:
def _pool_release(entry: dict) -> None:
+ # LAMBDA_KEEPALIVE_MS=0: tear the container down instead of pooling it, so
+ # the next invocation is a guaranteed cold start (INIT re-runs).
+ if _KEEPALIVE_KILL_ON_RELEASE:
+ _pool_remove(entry)
+ return
with _warm_pool_lock:
entry["in_use"] = False
entry["last_used"] = time.time()
diff --git a/tests/test_lambda.py b/tests/test_lambda.py
index b9505c5d..f2a18e6e 100644
--- a/tests/test_lambda.py
+++ b/tests/test_lambda.py
@@ -9173,3 +9173,47 @@ def test_lambda_esm_dynamodb_max_record_age_to_failure_destination(lam, ddb, sqs
lam.delete_event_source_mapping(UUID=esm["UUID"])
lam.delete_function(FunctionName=fn)
ddb.delete_table(TableName=table)
+
+
+def test_lambda_keepalive_zero_forces_cold_start(monkeypatch):
+ """LAMBDA_KEEPALIVE_MS=0 tears the Docker RIE container down on release, so
+ the next invocation is a cold start instead of reusing the warm container.
+ Regression for #1300 (LocalStack-compat cold-start isolation)."""
+ from ministack.services import lambda_svc as _svc
+
+ class _StubContainer:
+ status = "running"
+ def __init__(self):
+ self.stopped = False
+ self.removed = False
+ def reload(self):
+ pass
+ def stop(self, timeout=2):
+ self.stopped = True
+ def remove(self, force=False):
+ self.removed = True
+
+ stub = _StubContainer()
+ entry = {"container": stub, "tmpdir": None, "in_use": True,
+ "last_used": 0, "created": 0}
+ key = "111122223333:us-east-1:fn-keepalive:zip:sha-v1"
+ with _svc._warm_pool_lock:
+ _svc._warm_pool.setdefault(key, []).append(entry)
+ try:
+ # Default (keepalive off): release keeps the container warm in the pool.
+ monkeypatch.setattr(_svc, "_KEEPALIVE_KILL_ON_RELEASE", False)
+ _svc._pool_release(entry)
+ with _svc._warm_pool_lock:
+ assert entry in _svc._warm_pool.get(key, [])
+ assert not stub.stopped and entry["in_use"] is False
+
+ # LAMBDA_KEEPALIVE_MS=0: release tears the container down and drops it.
+ monkeypatch.setattr(_svc, "_KEEPALIVE_KILL_ON_RELEASE", True)
+ _svc._pool_release(entry)
+ with _svc._warm_pool_lock:
+ assert entry not in _svc._warm_pool.get(key, [])
+ assert stub.stopped and stub.removed
+ finally:
+ with _svc._warm_pool_lock:
+ _svc._warm_pool.pop(key, None)
diff --git a/tests/test_s3.py b/tests/test_s3.py
index a923e8ff..340c6d04 100644
--- a/tests/test_s3.py
+++ b/tests/test_s3.py
@@ -4006,13 +4006,23 @@ def test_s3_owner_id_consistent_between_listing_and_acl(s3):
assert listed == acl_owner
-def test_s3_presigned_url_expires(s3):
- """An expired presigned URL is rejected with 403. Regression for #1322 defect 10."""
+def test_s3_presigned_url_expires():
+ """An expired SigV4 presigned URL is rejected with 403. Regression for
+ #1322 defect 10. Uses an explicit s3v4 client (the expiry check lives in the
+ SigV4 verification path); the default fixture presigns with SigV2."""
+ import boto3
import requests
+ from botocore.config import Config
+ ep = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566").rstrip("/")
+ v4 = boto3.client(
+ "s3", endpoint_url=ep, region_name="us-east-1",
+ aws_access_key_id="test", aws_secret_access_key="test",
+ config=Config(signature_version="s3v4", s3={"addressing_style": "path"}),
+ )
bucket = "intg-s3-presign-expiry"
- s3.create_bucket(Bucket=bucket)
- s3.put_object(Bucket=bucket, Key="k", Body=b"x")
- url = s3.generate_presigned_url(
+ v4.create_bucket(Bucket=bucket)
+ v4.put_object(Bucket=bucket, Key="k", Body=b"x")
+ url = v4.generate_presigned_url(
"get_object", Params={"Bucket": bucket, "Key": "k"}, ExpiresIn=1
)
time.sleep(2)