fix(hub): gc enumerates prunable manifest chunks before deleting blobs (cross-share double-prune leak)
Some checks are pending
CI / vet-build-test (push) Waiting to run
Some checks are pending
CI / vet-build-test (push) Waiting to run
Content-addressed snapshots can appear as prunable in two shares simultaneously. The old inline manifestChunks call inside the prune loop read the blob after the first share's iteration had already deleted it, returning ErrNotFound → nil hashes → DeleteSnapshot decremented nothing → chunk stayed at refcount 1 and leaked forever. Pre-enumerate all chunk lists once before the deletion loop so both (share,id) decrements always fire with the correct hash list. No change to dry-run path: it never deletes blobs so ErrNotFound cannot occur there.
This commit is contained in:
parent
c807ea8c91
commit
df33b5facd
2 changed files with 110 additions and 4 deletions
|
|
@ -15,6 +15,92 @@ import (
|
|||
"github.com/shoemoney/devbox/internal/transport"
|
||||
)
|
||||
|
||||
// TestGCBothSharesPruneSameSnapshot is the missing case: BOTH shares advance past
|
||||
// the shared snapshot S, so S is a prunable entry in BOTH shares simultaneously.
|
||||
// The pre-fix code reads the manifest blob inline per prunable entry — the first
|
||||
// deletion removes the blob so the second entry gets ErrNotFound → nil hashes →
|
||||
// chunk refcount never decremented → permanent storage leak.
|
||||
// After the fix the chunk list is read once up-front, so both decrements happen
|
||||
// correctly and the chunk drops to refcount 0 and is deleted.
|
||||
func TestGCBothSharesPruneSameSnapshot(t *testing.T) {
|
||||
db, err := meta.Open(":memory:")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
store, err := blobstore.NewDisk(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
srv := httptest.NewServer(hub.NewServer(db, store).Handler())
|
||||
defer srv.Close()
|
||||
|
||||
if err := db.CreateToken(hub.HashToken("tok"), time.Now().Unix()+3600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c := transport.New(srv.URL)
|
||||
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
|
||||
if _, err := c.Join("tok", "dev", pub, priv); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := c.Publish("a"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := c.Publish("b"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
guard, _ := secret.New(nil)
|
||||
|
||||
// Push byte-identical content to BOTH shares → same snapshot id S, chunks refcount 2.
|
||||
rootA, rootB := t.TempDir(), t.TempDir()
|
||||
igA, _ := syncer.LoadIgnore(rootA)
|
||||
igB, _ := syncer.LoadIgnore(rootB)
|
||||
writeFile(t, rootA, "f.txt", "identical bytes\n")
|
||||
writeFile(t, rootB, "f.txt", "identical bytes\n")
|
||||
sa, err := syncer.Push(c, rootA, "a", "", igA, guard, "", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("push a: %v", err)
|
||||
}
|
||||
sb, err := syncer.Push(c, rootB, "b", "", igB, guard, "", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("push b: %v", err)
|
||||
}
|
||||
if sa.Snapshot != sb.Snapshot {
|
||||
t.Fatalf("identical content must collide: a=%s b=%s", sa.Snapshot, sb.Snapshot)
|
||||
}
|
||||
sharedChunks := manifestChunksOrFail(t, store, sa.Snapshot)
|
||||
if len(sharedChunks) == 0 {
|
||||
t.Fatal("expected at least one chunk")
|
||||
}
|
||||
|
||||
// Advance BOTH shares past S → S is prunable in BOTH, not the live head of either.
|
||||
writeFile(t, rootA, "f.txt", "changed on a\n")
|
||||
if _, err := syncer.Push(c, rootA, "a", "", igA, guard, sa.Head, nil); err != nil {
|
||||
t.Fatalf("advance a: %v", err)
|
||||
}
|
||||
writeFile(t, rootB, "f.txt", "changed on b\n")
|
||||
if _, err := syncer.Push(c, rootB, "b", "", igB, guard, sb.Head, nil); err != nil {
|
||||
t.Fatalf("advance b: %v", err)
|
||||
}
|
||||
|
||||
if _, _, err := runGC(db, store, 1, 0, false); err != nil {
|
||||
t.Fatalf("gc: %v", err)
|
||||
}
|
||||
|
||||
// S's manifest blob and ALL its chunks must be gone — no share's live head is S.
|
||||
if has, _ := store.Has(sa.Snapshot); has {
|
||||
t.Fatal("snapshot S manifest blob should be deleted (no share references it)")
|
||||
}
|
||||
for _, h := range sharedChunks {
|
||||
if has, _ := store.Has(h); has {
|
||||
t.Fatalf("chunk %s leaked: still in blobstore after both shares advanced past S", h)
|
||||
}
|
||||
if rc, _ := db.ChunkRefcount(h); rc != 0 {
|
||||
t.Fatalf("chunk %s refcount = %d, want 0 (double-prune leak)", h, rc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Two shares with byte-identical content collide on the same content-addressed
|
||||
// snapshot id (the idempotent push skips the refcount bump). Advancing one share
|
||||
// makes that id "deletable" for it — but it's still the OTHER share's live head.
|
||||
|
|
|
|||
|
|
@ -579,6 +579,29 @@ func runGC(db *meta.DB, store blobstore.Store, keep, keepDays int, dryRun bool)
|
|||
return snaps, len(del), nil
|
||||
}
|
||||
|
||||
// Pre-enumerate chunk hashes for every distinct prunable id before any blob is
|
||||
// deleted. Content is content-addressed: the same id may appear as a prunable
|
||||
// entry in two different shares (both advanced past it). Reading the manifest
|
||||
// inline — after the first share's iteration has already deleted the blob —
|
||||
// returns ErrNotFound, so the second share's DeleteSnapshot gets nil hashes and
|
||||
// the shared chunks' refcounts are never decremented, causing a permanent leak.
|
||||
// Reading each id exactly once up-front avoids the ordering hazard entirely.
|
||||
// ponytail: single pass over distinct IDs; nil slice on ErrNotFound matches the old tolerated-error path.
|
||||
chunksByID := make(map[string][]string, len(prunable))
|
||||
for _, p := range prunable {
|
||||
if keptSnaps[p.id] {
|
||||
continue
|
||||
}
|
||||
if _, seen := chunksByID[p.id]; seen {
|
||||
continue
|
||||
}
|
||||
hashes, err := manifestChunks(store, p.id)
|
||||
if err != nil && err != blobstore.ErrNotFound {
|
||||
return 0, 0, err
|
||||
}
|
||||
chunksByID[p.id] = hashes // nil on ErrNotFound is fine; DeleteSnapshot handles it
|
||||
}
|
||||
|
||||
// Prune each (share, id): delete that share's row and decrement its chunks. The
|
||||
// same content id in another share keeps its own row and counts. Delete the
|
||||
// shared manifest blob at most once, and only when it's unreachable from any
|
||||
|
|
@ -589,10 +612,7 @@ func runGC(db *meta.DB, store blobstore.Store, keep, keepDays int, dryRun bool)
|
|||
if keptSnaps[p.id] {
|
||||
continue // a live/kept snapshot in another share still needs this content
|
||||
}
|
||||
hashes, err := manifestChunks(store, p.id)
|
||||
if err != nil && err != blobstore.ErrNotFound {
|
||||
return 0, 0, err
|
||||
}
|
||||
hashes := chunksByID[p.id]
|
||||
if err := db.DeleteSnapshot(p.share, p.id, hashes); err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue