Skip to content

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

fix(lambda): code caches belong to one instance (#2418)

Jeff Hellerd242e7cc3750
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6666c144..d1a7b2ee 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Fixed
+- **Lambda — two MiniStacks on one host no longer remove each other's function code** — the provided-runtime and Docker-executor extraction caches lived at fixed paths in the shared temp directory, so a second instance on the same host deleted a tree the first was still running from ("leftover from a previous process") and its `reset()` removed the first one's caches; an invocation then failed with `No such file or directory: .../bootstrap`. Both caches are now named for the instance's `GATEWAY_PORT`. Contributed by @skialpine.
### Added
- **OpenSearch — `OPENSEARCH_JAVA_OPTS`** — a small machine could not cap the heap of the engine containers MiniStack starts; the setting is now passed to each of them (per-domain and Serverless collection), and unset passes nothing. Contributed by @skialpine.
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.py
index 4e521376..1ad303f6 100644
--- a/ministack/services/lambda_svc.py
+++ b/ministack/services/lambda_svc.py
@@ -376,8 +376,11 @@ def _runtime_env_vars(config: dict) -> dict:
# sha256, matching real Lambda's read-only /var/task — and both extraction
# and every spawn take ``_provided_code_lock`` so no fork can overlap an
# extraction write. Directories accumulate per distinct code sha within
-# ``tempfile.gettempdir()``; ``reset()`` removes them.
-_PROVIDED_CODE_CACHE = os.path.join(tempfile.gettempdir(), "ministack-provided-code")
+# ``tempfile.gettempdir()``; ``reset()`` removes them. Both caches are named
+# for this instance's gateway port: another MiniStack on the host shares the
+# temp directory and would otherwise remove a tree that is still in use.
+_GATEWAY_PORT = os.environ.get("GATEWAY_PORT") or os.environ.get("EDGE_PORT") or "4566"
+_PROVIDED_CODE_CACHE = os.path.join(tempfile.gettempdir(), f"ministack-provided-code-{_GATEWAY_PORT}")
_provided_code_lock = threading.Lock()
_provided_code_dirs: dict[str, str] = {}
@@ -414,7 +417,7 @@ def _provided_runtime_code_dir(code_zip: bytes) -> str:
# environment and mounts /var/task and /opt read-only. Directories accumulate
# per distinct blob within ``tempfile.gettempdir()``; ``reset()`` removes
# them, like the provided-runtime cache above.
-_DOCKER_EXTRACT_CACHE = os.path.join(tempfile.gettempdir(), "ministack-lambda-extract")
+_DOCKER_EXTRACT_CACHE = os.path.join(tempfile.gettempdir(), f"ministack-lambda-extract-{_GATEWAY_PORT}")
_docker_extract_lock = threading.Lock()
_docker_extract_dirs: dict[str, str] = {}
diff --git a/tests/test_lambda.py b/tests/test_lambda.py
index 2fd1c8f2..00370bbd 100644
--- a/tests/test_lambda.py
+++ b/tests/test_lambda.py
@@ -12955,6 +12955,9 @@ def _fresh_extract_cache(monkeypatch, tmp_path):
"""Point the extraction cache at an empty per-test root."""
monkeypatch.setattr(lsvc, "_DOCKER_EXTRACT_CACHE", str(tmp_path / "extract"))
monkeypatch.setattr(lsvc, "_docker_extract_dirs", {})
+ # reset() below also removes the provided-runtime cache, which this process shares with its server.
+ monkeypatch.setattr(lsvc, "_PROVIDED_CODE_CACHE", str(tmp_path / "provided"))
+ monkeypatch.setattr(lsvc, "_provided_code_dirs", {})
def _count_extractions(monkeypatch):
@@ -15751,3 +15754,12 @@ def test_lambda_function_url_event_and_response_follow_aws(lam):
assert re.fullmatch(r"Root=1-[0-9a-f]{8}-[0-9a-f]{24};Parent=[0-9a-f]{16};Sampled=0;Lineage=1:[0-9a-f]{8}:0", trace)
finally:
lam.delete_function(FunctionName=name)
+
+
+def test_lambda_code_caches_belong_to_this_instance():
+ """The code caches are named for the instance's gateway port, so two MiniStacks on one host keep apart."""
+ from ministack.services import lambda_svc as lsvc
+
+ port = os.environ.get("GATEWAY_PORT") or os.environ.get("EDGE_PORT") or "4566"
+ assert os.path.basename(lsvc._PROVIDED_CODE_CACHE) == f"ministack-provided-code-{port}"
+ assert os.path.basename(lsvc._DOCKER_EXTRACT_CACHE) == f"ministack-lambda-extract-{port}"