Repository navigation
Description
Summary
Deleting an object whose key ends in / makes every key under that prefix undeletable. The next
DeleteObject for such a key panics inside the emulator (recovered by chi's Recoverer, so the
client sees a 500).
In S3 a key is an opaque string: dir/ and dir/a.txt are two unrelated objects, and deleting the
first has no bearing on the second. The emulator stores objects in an afero.MemMapFs, where dir/
is the directory node that dir/a.txt hangs off — so removing it detaches the child.
Reproduction
PUT /bucket/dir/ (zero bytes)
PUT /bucket/dir/a.txt
DELETE /bucket/dir/ -> 204
DELETE /bucket/dir/a.txt -> 500
Server log:
parent of /bucket/dir/a.txt is nil
panic: parent of /bucket/dir/a.txt is nil
github.com/spf13/afero.(*MemMapFs).unRegisterWithParent memmap.go:73
github.com/spf13/afero.(*MemMapFs).Remove memmap.go:286
github.com/scttfrdmn/substrate/emulator.(*S3Plugin).deleteObject s3_plugin.go:858
Mechanism
deleteObject (emulator/s3_plugin.go:858) does p.fs.Remove("/" + bucket + "/" + key). For
key = "dir/" that path normalizes to /bucket/dir, which is the MemMapFs directory holding
/bucket/dir/a.txt. afero removes it and leaves the child in the data map with no parent; the next
Remove of the child calls unRegisterWithParent, findParent returns nil, and afero's response to
a nil parent is log.Panic (memmap.go:70-73).
Confirmed against afero v1.15.0 alone, with no substrate involved:
fs := afero.NewMemMapFs()
for _, p := range []string{"/b/project/", "/b/project/a.txt"} {
f, _ := fs.Create(p)
_ = f.Close()
}
fs.Remove("/b/project/") // ok
fs.Remove("/b/project/a.txt") // panic: parent of /b/project/a.txt is nilSo the object-state side (p.state.Delete) is correct and it is the filesystem mirror that carries
the coupling.
Why it matters downstream
ObjectFS represents a directory as a zero-byte marker object at prefix/ — that is what mkdir
writes, and it is also what aws s3api put-object --key dir/ and the S3 console's "create folder"
produce. So any operation that empties a directory hits this: rmdir of a tree, and the delete half
of a directory rename. Against real S3 both are unremarkable.
The failure also names the wrong culprit. The client sees a DeleteObject failure, so the test
reports "rename copied the object but could not delete the source" — a plausible-looking bug in the
code under test, in an operation whose interesting failure mode is exactly that.
Suggested fix
Two options, either sufficient:
- Don't mirror marker objects into the filesystem at all — a key ending in
/is a zero-byte
object, soRemovehas nothing to do. Skipping both the create and the remove for such keys
keeps the mirror a strict tree. - Store object bodies under a name that cannot collide with a directory node (e.g. append a suffix,
or use a flat map keyed by the raw key). This also fixes the mirror image of the bug:PUT dir/a.txt
afterPUT dir/currently depends on afero's implicit-parent behaviour.
Option 1 is the smaller change; option 2 removes the whole class, including any case where a key is a
strict prefix of another with / between them.
Worth checking whether deleteObjects (the multi-object POST /<bucket>?delete) reaches the same
p.fs.Remove — if so it has the same behaviour with several keys in one request.
Workaround in place
ObjectFS's harness probes for this at runtime (internal/testaws, RequireDirectoryMarkerDelete) and
skips the affected tests with a message pointing here, rather than asserting the emulator's behaviour
as expected. The marker path is covered against real S3 by the live integration suite.
Found against substrate v0.85.0.
Activity
v0.11.0 phase A: mv works, and the README says how far short of POSIX…
scttfrdmn commentedon Aug 3, 2026
Milestoned to v0.89.0 and labelled
priority: high.Two reasons it belongs there rather than staying unscheduled with the rest of the S3 backlog:
Recovererturns it into a 500, so a consumer sees a server error on aDeleteObjectthatis correct S3 usage, and the failure names the wrong culprit — the delete that fails is not
the delete that broke it.
DeleteBucketleaves every sub-resource behind) and CloudFormation: DeleteStack does not delete the stack's resources #518(
DeleteStackdoes not delete its resources). All three are the S3/stack delete path, andCloudFormation: DeleteStack does not delete the stack's resources #518's resource sweep will drive
DeleteObjectover exactly theprefix/-marker shape thattriggers this — so a sweep landing before this fix would hit the panic from inside
CloudFormation teardown, where it is far harder to attribute.
Nothing in v0.88.0 changed the analysis: the coupling is in the
afero.MemMapFsmirror, whichthat release did not touch. The reproduction against afero v1.15.0 with no substrate involved
still stands, so the fix is substrate's key-to-path mapping rather than anything upstream.
fix(s3): a directory marker must not touch the object mirror (#534) (#…