Repository navigation
Description
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) callsSnapshotableStateManager.Resetand 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 throughResettablePlugin(replay: PluginRegistry has no reset hook, so plugin-held counters survive resetState #886), andS3Plugin.ResetForRundeliberately does not touchfs.
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/PutObjectsequence 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 consultsfsbefore the state manager (aHeadObjectfast 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.ResetForRunremove 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
fswith a freshafero.NewMemMapFs()only when the plugin defaulted it, and leave an injected filesystem untouched (recording that decision inInitialize); - leave it, and document on
handleStateResetandTestServer.ResetStatethat 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
aferodirectly.
Activity
scttfrdmn commentedon Sep 17, 2026
Decision: option 2, in an emptying-in-place form — and only for a filesystem the plugin owns
Taking the issue's second option:
S3Plugin.ResetForRunempties 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.fswithout holding a lock, andResetForRunis documented (#886) as safe to call while requests are in flight, so assigning a freshNewMemMapFs()to the field would race with a request already in it.Initializerecords ownership in a newownsFSfield, 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 aMemMapFs,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 byRemoveAll("/"+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 anOsFsrooted 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 bothhandleStateResetandTestServer.ResetStatenow 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 theTestServerthis issue is about — is covered, and the exception is reachable only by an in-process embedder. Every in-tree injector passes aNewMemMapFs()of its own.On the third acceptance criterion — "through the wire, not through
aferodirectly"The write is through the wire (a real
CreateBucket+PutBucketVersioning+ twoPutObjects, 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 newPayloadPathsForTestaccessor inexport_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:
PutObject/ResetState/PutObjecton one key does not read stale bytes:putObjectwrites unconditionally whenevers3ObjectHasBody(key), and an empty body truncates;a/bthena) does not error:MemMapFspermitsWriteFileover 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.
fix(reset): release what a plugin started, not only what it minted (#902