Skip to content

Lambda: a warm container is never invalidated, so UpdateFunctionCode then Invoke runs the previous code #1035

Closed

Description

@scttfrdmn

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:

  1. UpdateFunctionCode then Invoke runs the previous code for up to WarmPoolTTL, or indefinitely under steady invocation. GetFunction reports the new CodeSha256 and RevisionId while the invoke returns the old function's output — the two disagree, and the response a caller is testing against is the stale one.
  2. DeleteFunction then CreateFunction under the same name then Invoke runs 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 from updateFunctionCode and deleteFunction (and, if the decision goes that way, updateFunctionConfiguration). It has to run under e.mu and stop the container outside it, following DrainPool.
  • 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 an export_test.go accessor, in the manner of RDSActiveContainerCountForTest.
  • Adjacent, and cheap to fix in the same pass: deleteFunction does 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 on ZipStored, 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: UpdateFunctionCode and DeleteFunction at minimum, and whether UpdateFunctionConfiguration joins 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 UpdateFunctionCode runs the new code.
  • deleteFunction releases the stored ZIP, or the leak is recorded as deliberate.

Activity

  1. added this to the v0.119.0 milestone on Sep 17, 2026
  2. added
    type: bug
    priority: medium
    on Sep 17, 2026
  3. scttfrdmn commented on Sep 18, 2026

    @scttfrdmn
    OwnerAuthor

    Verified against main before 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. updateFunctionCode is lambda_plugin.go:439-498, not :439-490 — #1007's body guard added lines to it. deleteFunction is :550-569, not :540-559. The pool lookup and insert are still exactly at :110 and :128 of lambda_exec.go.

    2. containerHandle carries 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 if getOrStartContainer wanted 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. UpdateFunctionConfiguration is 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 as AWS_LAMBDA_FUNCTION_HANDLER and as the container's command argument (:164, :174).
    • Environment → one -e per variable, on both the ZIP and the image path (:169-171, :206-208).

    MemorySize and Timeout are not: no --memory flag 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 successful UpdateFunctionConfiguration anyway, 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-86 already 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) runs docker stop without docker 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 taught RDSExecutor, and eviction is about to be called far more often than it was — an UpdateFunctionCode loop would leave one dead container per call.
    • deleteFunction never deletes lambdaZipStateKey, 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 on ZipStored, which a recreated function sets for itself.

    Also noted, not fixed here: evictStale (:284) uses time.Now() rather than the TimeController, 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: lambdapriority: mediumtype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions