devbox/cmd/devbox-hub/gc_crossshare_test.go
shoemoney 9ad2a9da74 fix(dataloss): make hub GC safe against cross-share refcount undercount 🩸🗑️
Snapshots are content-addressed with a GLOBAL id, so two shares with identical
content collide on one snapshots row and the idempotent push skips the refcount
bump — a chunk two share heads both need ends up refcount=1. The old GC then
pruned the 'old' snapshot for one share and freed chunks/manifests the OTHER
share's live head still pointed at. Permanent cross-share data loss.

Rework runGC as mark-and-sweep from every share's live snapshots as roots:
nothing reachable from a kept snapshot is ever deleted, whatever the refcounts
say. Never prune a snapshot that is any share's head. Plus hardening:
- DeleteSnapshot floors refcount at 0 and only decrements if the row existed
  (no negative counts from a double-delete).
- DeleteChunkRow is now conditional (WHERE refcount<=0) and reports if it
  deleted, so a push that re-references a chunk mid-GC isn't corrupted.
Cross-share repro test + existing single-share GC test both green.

Deferred to v2 (documented): proper per-(share,id) refcount accounting — needs
a schema migration of the live hub; the ground-truth GC makes it safe meanwhile.
2026-06-23 02:35:43 -05:00

97 lines
3.1 KiB
Go

package main
import (
"crypto/ed25519"
"crypto/rand"
"net/http/httptest"
"testing"
"time"
"git.shoemoney.ai/shoemoney/devbox/internal/hub"
"git.shoemoney.ai/shoemoney/devbox/internal/hub/blobstore"
"git.shoemoney.ai/shoemoney/devbox/internal/hub/meta"
"git.shoemoney.ai/shoemoney/devbox/internal/secret"
"git.shoemoney.ai/shoemoney/devbox/internal/syncer"
"git.shoemoney.ai/shoemoney/devbox/internal/transport"
)
// 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.
// GC must NOT delete the chunks/manifest that head needs. Regression for the
// critical cross-share refcount/GC data-loss finding.
func TestGCKeepsChunksSharedAcrossShares(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, _, _ := ed25519.GenerateKey(rand.Reader)
if _, err := c.Join("tok", "dev", pub); 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.
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 should collide: a=%s b=%s", sa.Snapshot, sb.Snapshot)
}
shared := manifestChunksOrFail(t, store, sb.Snapshot) // b's head chunks
if len(shared) == 0 {
t.Fatal("expected at least one chunk")
}
// Advance share a so S becomes deletable FOR A (b's head stays S).
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)
}
// GC keeping only each share's head.
if _, _, err := runGC(db, store, 1); err != nil {
t.Fatalf("gc: %v", err)
}
// b's head, its manifest blob, and its chunks must ALL survive.
if head, _, _ := db.ShareHead("b"); head != sb.Snapshot {
t.Fatalf("share b head = %q after gc, want %q (clobbered!)", head, sb.Snapshot)
}
if has, _ := store.Has(sb.Snapshot); !has {
t.Fatal("b's head manifest blob was deleted by gc (cross-share data loss)")
}
for _, h := range shared {
if has, _ := store.Has(h); !has {
t.Fatalf("chunk %s referenced by b's live head was deleted by gc (data loss)", h)
}
}
}