Skip to content

S3: deleting a "prefix/" marker object strands every key beneath it, and the next delete panics #534

Closed

Description

@scttfrdmn

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 nil

So 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:

  1. Don't mirror marker objects into the filesystem at all — a key ending in / is a zero-byte
    object, so Remove has nothing to do. Skipping both the create and the remove for such keys
    keeps the mirror a strict tree.
  2. 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
    after PUT 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

  1. added
    type: bug
    area: s3
    priority: high
    on Aug 3, 2026
  2. added this to the v0.89.0 milestone on Aug 3, 2026
  3. scttfrdmn commented on Aug 3, 2026

    @scttfrdmn
    OwnerAuthor

    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:

    Nothing in v0.88.0 changed the analysis: the coupling is in the afero.MemMapFs mirror, which
    that 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.

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: s3priority: hightype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions