Skip to content

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

fix(ESM): persistance in sqs (#893)

Nahuel Nuceraaf5e6fb82649
diff --git a/ministack/app.py b/ministack/app.py
index 05bad538..8aafba6a 100644
--- a/ministack/app.py
+++ b/ministack/app.py
@@ -1934,6 +1934,23 @@ def _load_persisted_state():
_get_module("lambda_durable")
logger.info("Lambda Durable: eager-loaded module to restore persisted executions")
+ # Lambda event source mappings (SQS / Kinesis / DynamoDB Streams) are
+ # polled by a background thread that lambda_svc starts from its
+ # import-time restore (`_ensure_poller`). lambda_svc is otherwise imported
+ # lazily on the first Lambda request — so after a persisted restart a
+ # workload that is pure SQS (just sending to a mapped queue) never imports
+ # the module, the poller never starts, and the restored ESM sits
+ # Enabled-but-unpolled while messages pile up (#889). Eager-import at boot
+ # when persisted ESMs exist so polling resumes exactly like a fresh
+ # CreateEventSourceMapping. Narrow: only pay the cold-start when there are
+ # mappings to poll. The `_data` reach gets all accounts' ESMs (the bool of
+ # an AccountScopedDict is account-scoped and would be 0 with no request
+ # context at boot).
+ _lam = load_state("lambda")
+ if _lam and getattr(_lam.get("esms"), "_data", _lam.get("esms")):
+ _get_module("lambda_svc") # module file is lambda_svc.py (lambda is a keyword)
+ logger.info("Lambda: eager-loaded module to resume event-source-mapping pollers at boot")
+
async def _wait_for_port(port, timeout=30):
"""Wait until the server is accepting TCP connections."""
diff --git a/tests/test_persistence.py b/tests/test_persistence.py
index b0098aaa..0a639752 100644
--- a/tests/test_persistence.py
+++ b/tests/test_persistence.py
@@ -355,6 +355,39 @@ def test_pipes_restore_starts_poller_for_running_pipes(monkeypatch):
mod.reset()
+def test_lambda_esm_eager_loaded_at_boot_when_persisted(monkeypatch):
+ """#889: persisted SQS event source mappings must resume polling after a
+ warm restart even under pure-SQS traffic. The ESM poller starts from
+ lambda_svc's import-time restore (`_ensure_poller`), and lambda_svc is
+ otherwise imported lazily only on a Lambda request — so `_load_persisted_state`
+ must eager-import it at boot when ESMs are persisted, else the restored
+ mapping sits Enabled-but-unpolled and messages pile up. A restore_state-level
+ test does NOT catch this: the bug is the module never being imported."""
+ import ministack.app as app
+ monkeypatch.setattr(app, "load_state",
+ lambda key: {"esms": {"uuid-1": {"Enabled": True}}} if key == "lambda" else None)
+ requested = []
+ real = app._get_module
+ monkeypatch.setattr(app, "_get_module", lambda n: (requested.append(n), real(n))[1])
+ app._load_persisted_state()
+ assert "lambda_svc" in requested, (
+ "#889: persisted ESMs present but lambda_svc was not eager-imported at "
+ "boot — the SQS poller never starts under pure-SQS traffic after restart."
+ )
+
+
+def test_lambda_not_eager_loaded_without_persisted_esms(monkeypatch):
+ """Narrow: no persisted ESMs → don't pay the lambda_svc cold-start at boot."""
+ import ministack.app as app
+ monkeypatch.setattr(app, "load_state",
+ lambda key: {"esms": {}} if key == "lambda" else None)
+ requested = []
+ real = app._get_module
+ monkeypatch.setattr(app, "_get_module", lambda n: (requested.append(n), real(n))[1])
+ app._load_persisted_state()
+ assert "lambda_svc" not in requested
+
+
# ── PERSIST_STATE gating ──────────────────────────────────────────────
@pytest.mark.parametrize("svc_key", [