Skip to content

A state reset leaves Lambda ESM pollers and the Lambda/RDS container pools running #903

Closed

Description

@scttfrdmn

Found while auditing plugin-held mutable state for #886.

#886 added ResettablePlugin so a reset can return plugin-held mutable state to its start-of-run value, and implemented it for the three plugins whose state feeds a minted identifier (S3Plugin.versionSeq, SESv2Plugin.msgSeq, OmicsPlugin.rng). The audit turned up three more pieces of plugin-held mutable state, all of them process-lifecycle rather than identifier-minting, and all of them deliberately out of #886's scope:

plugin field what it holds
LambdaPlugin esmStop map[string]chan struct{} (emulator/lambda_plugin.go:32) one stop channel per event-source mapping, each with a live go p.sqsPollerLoop(...) goroutine (:1241, :1374)
LambdaPlugin executor *LambdaExecutor (emulator/lambda_plugin.go:29) pool map[string]*containerHandle of warm containers (emulator/lambda_exec.go:39-47)
RDSPlugin executor *RDSExecutor (emulator/rds_plugin.go:22) active map[string]*RDSContainerHandle of live Postgres containers (emulator/rds_exec.go:25-29)

All three are torn down in the plugin's Shutdown, and by nothing else. So after POST /v1/state/reset — or a replay reset — the event-source-mapping records are gone from the StateManager while their poller goroutines keep running against queues that no longer exist, and the container pools keep whatever they had.

Why this was not folded into #886

Stopping a poller or tearing down a container is not reversible the way rewinding a counter is, and per the scope boundary in CLAUDE.md the executors run the workload rather than answer an API observation — so "reset" for them is a lifecycle question (what should a mid-run reset do to work already in flight?) rather than a determinism one. It wants its own decision.

What needs deciding

  • Should a state reset stop the ESM pollers whose records it just deleted? A poller that outlives its record is a goroutine leak with no API-visible owner.
  • Should a state reset drain the Lambda warm-container pool and the RDS container set, or is a warm pool legitimately process-scoped (a cache, not state)?
  • If yes to either, does that go through ResetForRun (making LambdaPlugin and RDSPlugin implementers) or through a separate, coarser hook?

Acceptance criteria

  • A decision recorded here for each of the three, with the reasoning.
  • The chosen behaviour implemented.
  • A test that creates an event-source mapping, resets state, and asserts no poller is still running (a goroutine count, or an assertion that the queue receives no further polls).

Activity

  1. added
    type: chore
    on Sep 15, 2026
  2. added this to the v0.118.0 milestone on Sep 15, 2026
  3. scttfrdmn commented on Sep 17, 2026

    @scttfrdmn
    OwnerAuthor

    Decision, one per row: stop the pollers, drain both container sets, and do it through ResetForRun

    1. ESM pollers — stopped

    A poller that outlives its record is not merely a leak; it acts on the next run. DeleteEventSourceMapping is the only call that closes a stop channel, and it answers ResourceNotFoundException once the record is gone — so after a reset the goroutine is unreachable through the API while still ticking every wall-clock second. Both the queue URL and the function ARN are name-derived, so the next test case to create a queue and a function under the same names has its messages received and deleted, and its function invoked, by a poller it never created. That is exactly the timing-dependent cross-case interference TestServer.ResetState exists to remove, so this one is not a judgement call.

    One theory I had here was wrong and is worth recording so nobody re-derives it: a ghost poller does not pollute the recorded event stream. PluginRegistry.RouteRequest calls p.HandleRequest directly, with no pipeline and no event recording, so the poller's SQS traffic is invisible to the event store. The cross-case argument above is the real one, and it is stronger.

    2. Lambda warm pool — drained, through a new DrainPool rather than StopAll

    A warm pool is a cache, so the issue's "legitimately process-scoped?" reading is defensible in general — but not as substrate keys it. e.pool[fn.FunctionArn] is keyed by function ARN alone, with no revision or code identity in the key, so a function recreated under the same name after a reset would run the previous run's code. Dropping a warm container is always safe (the next invoke starts one), so there is no cost on the other side of the trade.

    It must not go through StopAll, which is the trap in this row: StopAll closes the channel evictLoop selects on, so a reset calling it would leave every container started afterwards warm until Shutdown, however long it sat idle. Hence a new LambdaExecutor.DrainPool that empties the pool and leaves the eviction goroutine running; StopAll is now layered on it. LambdaPlugin.Shutdown also stops the pollers now — it did not before.

    3. RDS containers — stopped and removed

    Same reachability argument as the pollers, plus a second failure: DeleteDBInstance stops a container by reading "dbinstance_container:<scope>/<id>" back out of the StateManager, so once a reset deletes that key nothing short of Shutdown can reach the container — and it still holds the Docker name substrate-rds-<id> that the next CreateDBInstance for the same identifier needs, which fails docker run and falls back silently to a synthetic endpoint. So the leak degrades the next run rather than just consuming a container.

    Two adjacent defects found in the same file and fixed with it: StopContainer did not docker rm the container although its doc comment claimed it did (leaving the name taken), and nothing ever dropped the active entry — so the map grew for the life of the process and StopAll re-stopped containers it had already stopped.

    4. The hook — ResetForRun, not a coarser one

    The existing contract ("return plugin-held mutable state to the value it had at the start of the run") fits all three without amendment, and all three reset paths — POST /v1/state/reset, TestServer.ResetState, ReplayEngine.resetState — inherit it with no new wiring. A second, coarser hook would need three new call sites and a rule for which hook a new plugin implements. LambdaPlugin and RDSPlugin become implementers; the asserted set in TestPluginReset_DefaultPluginsImplementingTheHook goes from three to five.

    On the scope-boundary worry in the issue: this does not model workload internals. It stops substrate's own goroutine and releases substrate's own container. Nothing here decides what the workload was doing.

    On the third acceptance criterion — the form of the assertion

    Deviating from both suggested forms, deliberately. Not an assertion that the queue receives no further polls: a poll happens only on a one-second wall-clock tick, so that test would depend on real elapsed time, which the house rules forbid. Not a goroutine count either: runtime.NumGoroutine() counts everything the test binary is running in parallel. The assertion is the live-poller count — ESMPollerCountForTest, the number of stop channels the plugin holds — which is deterministic and exact, since sqsPollerLoop returns on that channel unconditionally. The test also asserts that DeleteEventSourceMapping on the same UUID now answers 404, which is the record that no API call could have stopped the poller after a reset.

    The RDS container path needs Docker to have something to stop, so the plugin-level test covers the two configurations reachable without it (no executor, and an executor holding nothing) and the executor-level bookkeeping — that a stop drops the active entry — is asserted in rds_exec_test.go beside the existing Docker-gated tests.

    Fix in the PR below, together with #902.

  4. scttfrdmn commented on Sep 17, 2026

    @scttfrdmn
    OwnerAuthor

    One adjacent defect surfaced while auditing the warm pool for row 2 and is not fixed here, because it is a live-path bug with a different fix: nothing invalidates a warm container when the function's code changes either — not UpdateFunctionCode, not DeleteFunction, only Shutdown and the idle TTL — so with Docker execution enabled an update-then-invoke runs the previous code for up to WarmPoolTTL. Filed as #1035. DrainPool is the wrong granularity for it (it drops every function's container), so it needs a per-ARN eviction at the point the code changes.

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

    type: chore

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions