Repository navigation
Description
Found while implementing #903, which fixed the reset half of this. This is the live-path half, and it is a correctness bug rather than a leak.
LambdaExecutor.pool is keyed by function ARN alone (emulator/lambda_exec.go:110, :128):
if h, ok := e.pool[fn.FunctionArn]; ok {
e.mu.Unlock()
return h, nil
}There is no code identity in the key and no comparison against the handle — not CodeSha256, not RevisionID, not ImageURI. A warm container is started once, with the ZIP written into a temp directory (startZIPContainer) or the image URI baked into the docker run (startImageContainer), and reused for every later invoke of that ARN.
Nothing invalidates it but Shutdown and the idle TTL. Neither updateFunctionCode (emulator/lambda_plugin.go:439-490) nor deleteFunction (:540-559) touches the executor: the first rewrites the function record and the stored ZIP and returns, the second deletes three state keys and returns. WarmPoolTTL defaults to 5 minutes (lambda_exec.go:52-53), and eviction is by idle time, so a function invoked in a loop is never evicted at all.
Two consequences, both with Docker execution enabled:
UpdateFunctionCodethenInvokeruns the previous code for up toWarmPoolTTL, or indefinitely under steady invocation.GetFunctionreports the newCodeSha256andRevisionIdwhile the invoke returns the old function's output — the two disagree, and the response a caller is testing against is the stale one.DeleteFunctionthenCreateFunctionunder the same name thenInvokeruns the deleted function's code. The ARN is derived from account, Region and name, so the recreated function collides with the dead one's pool entry. This is the same name-derived-key trap A state reset leaves Lambda ESM pollers and the Lambda/RDS container pools running #903 fixed for pollers, on the live path.
UpdateFunctionConfiguration is a third case worth deciding rather than assuming: it can change Handler, Runtime, Environment, MemorySize and Timeout, all of which are baked into the container at start.
Why this is not #903
#903 was about state a reset could not reach. This needs no reset: two ordinary API calls in one run are enough. The fix is on the other side — an eviction at the point the code or the function identity changes — and LambdaExecutor.DrainPool (added by #903) is the wrong granularity for it, since it drops every function's container rather than one.
Notes for whoever takes it
- The obvious shape is a per-ARN
evict(arn)on the executor, called fromupdateFunctionCodeanddeleteFunction(and, if the decision goes that way,updateFunctionConfiguration). It has to run undere.muand stop the container outside it, followingDrainPool. - Guard on
p.executor != nil: the executor is absent unless Docker execution is configured, and it is absent in CI, so a test of the invalidation itself needs the Docker gate (isDockerAvailable,emulator/docker_test_helpers_test.go). The bookkeeping — that the pool entry is gone — is assertable without Docker through anexport_test.goaccessor, in the manner ofRDSActiveContainerCountForTest. - Adjacent, and cheap to fix in the same pass:
deleteFunctiondoes not delete the stored ZIP (lambdaZipStateKey), so the bytes of every deleted function accumulate for the life of the process. No stale read follows from it today — the invoke path is gated onZipStored, which a recreated function sets for itself — so it is a leak, not a wrong answer.
Acceptance criteria
- A decision recorded here on which operations invalidate a warm container:
UpdateFunctionCodeandDeleteFunctionat minimum, and whetherUpdateFunctionConfigurationjoins them, with the reasoning. - The chosen behaviour implemented, per-ARN rather than by draining the pool.
- A test that the pool entry for a function is gone after each of the chosen operations (assertable without Docker), and a Docker-gated test that an invoke after
UpdateFunctionCoderuns the new code. -
deleteFunctionreleases the stored ZIP, or the leak is recorded as deliberate.
Activity
scttfrdmn commentedon Sep 18, 2026
Verified against
mainbefore opening the PR. The issue's analysis holds; five corrections and one decision, recorded here rather than left as a silent divergence in the CHANGELOG.1. The line ranges have drifted.
updateFunctionCodeislambda_plugin.go:439-498, not:439-490— #1007's body guard added lines to it.deleteFunctionis:550-569, not:540-559. The pool lookup and insert are still exactly at:110and:128oflambda_exec.go.2.
containerHandlecarries no code identity at all, which is a stronger statement than "no code identity in the key": the struct is{containerID, port, tempDir, lastUsed}(lambda_exec.go:29-34). So there is nothing to compare even ifgetOrStartContainerwanted to, and a fix that added a comparison would have to widen the struct first. Eviction is the smaller change and is what this PR does.3.
UpdateFunctionConfigurationis in scope, and the evidence is in the tree rather than a judgement call. The issue lists five members it "can change" and asks for a decision. Three of them are demonstrably baked into the container at start:Runtime→runtimeToImage(fn.Runtime)picks the image (lambda_exec.go:154, used at:205).Handler→ passed asAWS_LAMBDA_FUNCTION_HANDLERand as the container's command argument (:164,:174).Environment→ one-eper variable, on both the ZIP and the image path (:169-171,:206-208).MemorySizeandTimeoutare not: no--memoryflag and no timeout are passed on either path, so they are recorded in the function's state and do not reach the container. That splits the decision cleanly — the three that are baked in invalidate, and the two that are not do not need to. Substrate evicts on any successfulUpdateFunctionConfigurationanyway, because the alternative is comparing five members against a handle that stores none of them, and dropping a warm container is always safe (the next invoke starts a fresh one) where keeping a stale one is not.4.
lambda_plugin.go:83-86already states this bug in prose.ResetForRun's doc comment says the warm pool "has to be dropped because it is keyed by function ARN alone. A function recreated under the same name after a reset would be invoked in the previous run's container, running the previous run's code." That is consequence 2 of this issue, written down in the fix for #903 and then scoped to the reset boundary only. The comment needs the second half now.5. Two adjacent leaks folded in, both cheap once this code is being touched.
stopContainer(lambda_exec.go:342-352) runsdocker stopwithoutdocker rm, so every evicted container stays on the host as an exited container for the life of the machine, not the process. This is the inverse of what A state reset leaves Lambda ESM pollers and the Lambda/RDS container pools running #903 taughtRDSExecutor, and eviction is about to be called far more often than it was — anUpdateFunctionCodeloop would leave one dead container per call.deleteFunctionnever deleteslambdaZipStateKey, so the bytes of every deleted function accumulate. The issue records it correctly as a leak rather than a wrong answer; the invoke path is gated onZipStored, which a recreated function sets for itself.Also noted, not fixed here:
evictStale(:284) usestime.Now()rather than theTimeController, so the idle TTL is the one piece of Lambda's timing that a test cannot advance. That is a separate defect from this one — it makes the TTL path untestable rather than answering wrongly — and it is not in this issue's scope.