Skip to content

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

fix(lambda): throttle all invokes when ReservedConcurrentExecutions=0 (#1817)

Adrian-Ryan Acala448461b23a4b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 43cf186a..bdc206aa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,9 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Fixed
+
+- **Lambda — `ReservedConcurrentExecutions=0` disables the function** — a zero reservation was treated as unset, so the function ran unbounded instead of throttling every invoke with `TooManyRequestsException`. Zero now throttles across all executors, the Docker pool, and SQS event source mappings.
### Added
- **CloudFormation — AppConfig applications, environments, configuration profiles and deployment strategies update in place** — none of the four had an update handler, so any property change re-ran the create and the resource came back under a new id. The environments, configuration profiles and hosted configuration versions keyed by the old id were orphaned. Each type now updates in place what its resource reference lists as No interruption and replaces on the rest. Contributed by @iot-rocket.
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.py
index 15264214..87a35fe7 100644
--- a/ministack/services/lambda_svc.py
+++ b/ministack/services/lambda_svc.py
@@ -3333,7 +3333,8 @@ def _pool_acquire(key: str, max_concurrency: int | None):
`max_concurrency` semantics:
- int > 0 : per-function cap (ReservedConcurrentExecutions). At cap → (None, False).
- - None / 0 : no per-function cap. Always spawn a fresh container if no free one.
+ - 0 : function disabled (ReservedConcurrentExecutions=0). Always throttle.
+ - None : no per-function cap. Always spawn a fresh container if no free one.
Account-level cap (if `_ACCOUNT_CONCURRENCY_CAP > 0`) is enforced globally across all keys.
@@ -3356,8 +3357,8 @@ def _pool_acquire(key: str, max_concurrency: int | None):
e["in_use"] = True
e["last_used"] = time.time()
return e, "reused"
- # Function-level cap
- if max_concurrency and len(entries) >= max_concurrency:
+ # Function-level cap (0 disables the function — every invoke throttles)
+ if max_concurrency is not None and len(entries) >= max_concurrency:
return None, "func_cap"
# Account-level cap (count in-use entries across all pools)
if _ACCOUNT_CONCURRENCY_CAP > 0:
@@ -4635,7 +4636,8 @@ def _execute_function_docker(func: dict, event: dict) -> dict:
reserved = func.get("concurrency")
if isinstance(reserved, dict):
reserved = reserved.get("ReservedConcurrentExecutions")
- max_conc = int(reserved) if reserved else None # None = unbounded per-function
+ # None = unbounded per-function; 0 disables the function (throttle-all).
+ max_conc = None if reserved is None else int(reserved)
_ensure_reaper_thread()
key = _warm_pool_key(fn_name, config)
@@ -4881,7 +4883,7 @@ def _acquire_execution_slot(func: dict, config: dict):
reserved = _reserved_concurrency(func, config)
key = _inflight_key(config)
with _inflight_lock:
- if reserved and _inflight.get(key, 0) >= reserved:
+ if reserved is not None and _inflight.get(key, 0) >= reserved:
return None, "function"
if _ACCOUNT_CONCURRENCY_CAP > 0 and _inflight_total >= _ACCOUNT_CONCURRENCY_CAP:
return None, "account"
@@ -7089,7 +7091,7 @@ def _sqs_esm_concurrency(esm: dict, func_rec: dict, config: dict, queue: dict |
return 1
limit = (esm.get("ScalingConfig") or {}).get("MaximumConcurrency") or _ESM_SQS_DEFAULT_CONCURRENCY
reserved = _reserved_concurrency(func_rec, config)
- return min(limit, reserved) if reserved else limit
+ return min(limit, reserved) if reserved is not None else limit
def _dispatch_esm_batch(esm_id: str, fn, *args) -> None:
diff --git a/tests/test_lambda.py b/tests/test_lambda.py
index 5785d308..7d70ef9f 100644
--- a/tests/test_lambda.py
+++ b/tests/test_lambda.py
@@ -6664,6 +6664,26 @@ def test_function_concurrency_cap_none_is_unbounded():
assert reason == "spawn"
+def test_function_concurrency_cap_zero_disables():
+ """ReservedConcurrentExecutions=0 → every invoke throttles, even on an empty pool."""
+ entry, reason = lsvc._pool_acquire("k", max_concurrency=0)
+ assert entry is None
+ assert reason == "func_cap"
+
+
+def test_execution_slot_zero_reserved_throttles():
+ """ReservedConcurrentExecutions=0 → _acquire_execution_slot refuses the first slot."""
+ func = {"concurrency": 0}
+ config = {
+ "FunctionName": "cap-zero-unit",
+ "Version": "$LATEST",
+ "FunctionArn": "arn:aws:lambda:us-east-1:000000000000:function:cap-zero-unit",
+ }
+ slot, limit = lsvc._acquire_execution_slot(func, config)
+ assert slot is None
+ assert limit == "function"
+
+
def test_account_concurrency_cap_rejects(monkeypatch):
"""Global account cap: 3 in-use total → 4th is throttled as acct_cap."""
monkeypatch.setattr(lsvc, "_ACCOUNT_CONCURRENCY_CAP", 3)