Skip to content

S3: a state reset leaves every object payload in the plugin filesystem #902

Closed

Description

@scttfrdmn

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

S3Plugin.fs (emulator/s3_plugin.go:52) holds every object payload the emulator has ever been given: afero.NewMemMapFs() by default, or a caller-supplied afero.Fs from Options["filesystem"]. Object metadata lives in the StateManager; object bytes do not.

Both reset paths therefore leave the bytes behind:

  • POST /v1/state/reset (emulator/server.go:507) calls SnapshotableStateManager.Reset and clears fault rules. Its doc comment says it "wipes all emulator state".
  • TestServer.ResetState (emulator/testing.go:366) is a POST to that endpoint, and is documented as what a test calls "between test cases that share a single [TestServer] instance to avoid state leaking across cases".
  • ReplayEngine.resetState (emulator/replay.go) now also resets plugin-held state through ResettablePlugin (replay: PluginRegistry has no reset hook, so plugin-held counters survive resetState #886), and S3Plugin.ResetForRun deliberately does not touch fs.

Why #886 left it alone

A stale payload cannot change a minted identifier, which is what #886 was about: the bucket and key metadata are gone from the state manager, so no request can reach the bytes, and a replayed PutObject overwrites them. The leak is memory growth and a theoretical read-through, not a divergence.

Why it is still worth deciding

  • A long-lived process that resets between many test cases accumulates every payload it was ever sent, with no way to release them.
  • A PutObject/ResetState/PutObject sequence on the same key writes over a body that a correct emulator would have had to create from nothing — so any future code path that consults fs before the state manager (a HeadObject fast path, a checksum recompute, a multipart assembler) would silently read a previous run's bytes.

What needs deciding

The plugin does not own fs when the caller injected one, so "wipe it" is not obviously correct. Options:

  • have S3Plugin.ResetForRun remove only paths the plugin itself created (/<bucket>/... and /<bucket>/.versions/...) for buckets it knows about — but after a state reset it no longer knows about any;
  • replace fs with a fresh afero.NewMemMapFs() only when the plugin defaulted it, and leave an injected filesystem untouched (recording that decision in Initialize);
  • leave it, and document on handleStateReset and TestServer.ResetState that object payloads survive a reset.

Acceptance criteria

  • A decision recorded here on which of the above (or another) applies, with the reasoning.
  • The chosen behaviour implemented, or the two doc comments corrected to stop claiming a reset wipes everything.
  • A test that writes an object, resets, and asserts the chosen behaviour of the bytes — through the wire, not through afero directly.

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: option 2, in an emptying-in-place form — and only for a filesystem the plugin owns

    Taking the issue's second option: S3Plugin.ResetForRun empties its payload filesystem when it created that filesystem, and leaves an injected one alone. Two departures from the wording, both with a measured reason.

    It empties in place rather than replacing the field. Handlers read p.fs without holding a lock, and ResetForRun is documented (#886) as safe to call while requests are in flight, so assigning a fresh NewMemMapFs() to the field would race with a request already in it. Initialize records ownership in a new ownsFS field, as the issue suggests, but the reset acts on the filesystem rather than on the reference.

    It removes each root entry, not RemoveAll("/"). That was measured before the implementation was written, and the obvious call is a trap: on a MemMapFs, RemoveAll("/") reports success, leaves every file beneath the root still readable, and destroys the root directory entry so nothing can be enumerated afterwards. It is a silent no-op on the payloads that also breaks the filesystem. Readdirnames(-1) on / followed by RemoveAll("/"+name) per entry removes everything and leaves the filesystem writable. The test asserts the filesystem is usable after the reset for exactly this reason — an assertion that only checked the paths were gone passes against a filesystem that kept all of them.

    Option 1 is not implementable as the issue already notes: after a state reset the plugin knows about no buckets, so there is nothing to enumerate from.

    The injected-filesystem exception, and why it does not weaken the fix

    An Options["filesystem"] may be an OsFs rooted at a directory holding fixtures the caller means to keep, and the caller holds the only reference that knows what is safe to remove. So substrate does not empty it, and both handleStateReset and TestServer.ResetState now name that as the one exception. It costs nothing here: nothing can inject a filesystem through the HTTP server, so every server-hosted plugin — including the TestServer this issue is about — is covered, and the exception is reachable only by an in-process embedder. Every in-tree injector passes a NewMemMapFs() of its own.

    On the third acceptance criterion — "through the wire, not through afero directly"

    The write is through the wire (a real CreateBucket + PutBucketVersioning + two PutObjects, so #765's rule is satisfied). The absence cannot be: every read of the filesystem is gated on object metadata the reset just deleted, so no request can distinguish an emptied filesystem from one still holding the previous run's bytes. That is precisely why the leak went unnoticed. The assertion therefore goes through a new PayloadPathsForTest accessor in export_test.go, whose doc comment records why.

    Two candidate wire-visible consequences were tested rather than asserted, and both failed, so the issue's framing stands as written and is not overstated:

    • a PutObject/ResetState/PutObject on one key does not read stale bytes: putObject writes unconditionally whenever s3ObjectHasBody(key), and an empty body truncates;
    • a key-shape collision across a reset (a/b then a) does not error: MemMapFs permits WriteFile over an existing directory path, and the child stays readable.

    So the defect is memory growth plus the latent trap the issue describes, exactly. The counterfactual is verified by construction: with the emptying stubbed out, the test fails with the three surviving paths listed, and with RemoveAll("/") in its place it fails on the walk.

    Fix in the PR below, together with #903 — both amend the same three reset doc comments and state one rule.

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