From f51e69839a756e78dc0ce81af303d15528780a1d Mon Sep 17 00:00:00 2001 From: shoemoney Date: Tue, 30 Jun 2026 17:27:00 -0500 Subject: [PATCH] =?UTF-8?q?fix(hub):=20flock=20gc=20against=20backup=20so?= =?UTF-8?q?=20a=20sweep=20can't=20strand=20a=20backup=20mid-copy=20?= =?UTF-8?q?=F0=9F=94=92=F0=9F=92=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `backup` does VACUUM INTO (DB snapshot) then copies the blob tree; a concurrent gc sweep could delete a blob the snapshot references between those steps, producing a backup that's internally inconsistent — a dangling ref you'd only discover on restore. Now gc sweeps (the --gc-every ticker AND `gc`) and `backup` take an exclusive flock on /.lock, so they can't overlap. Dry-run gc skips the lock (read-only, must not block a long backup). unix flock (auto-released on crash); no-op stub on non-unix (the hub ships linux/darwin). Test: TestDataLockMutualExclusion. Cross-builds clean for windows + linux/arm64. --- cmd/devbox-hub/lock_other.go | 9 +++++++++ cmd/devbox-hub/lock_test.go | 39 ++++++++++++++++++++++++++++++++++++ cmd/devbox-hub/lock_unix.go | 32 +++++++++++++++++++++++++++++ cmd/devbox-hub/main.go | 28 ++++++++++++++++++++++++-- 4 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 cmd/devbox-hub/lock_other.go create mode 100644 cmd/devbox-hub/lock_test.go create mode 100644 cmd/devbox-hub/lock_unix.go diff --git a/cmd/devbox-hub/lock_other.go b/cmd/devbox-hub/lock_other.go new file mode 100644 index 0000000..5a50743 --- /dev/null +++ b/cmd/devbox-hub/lock_other.go @@ -0,0 +1,9 @@ +//go:build !unix + +package main + +// dataLock is a no-op on non-unix platforms (the hub ships for linux/darwin via +// Docker/systemd; flock isn't available on Windows/plan9). gc/backup overlap is +// only a concern on a live server, which is unix. ponytail: add winio LockFileEx +// if a Windows hub ever ships. +func dataLock(_ string) (func(), error) { return func() {}, nil } diff --git a/cmd/devbox-hub/lock_test.go b/cmd/devbox-hub/lock_test.go new file mode 100644 index 0000000..090a721 --- /dev/null +++ b/cmd/devbox-hub/lock_test.go @@ -0,0 +1,39 @@ +//go:build unix + +package main + +import ( + "os" + "path/filepath" + "syscall" + "testing" +) + +// TestDataLockMutualExclusion proves dataLock actually excludes a concurrent +// holder — the property gc/backup serialization depends on. +func TestDataLockMutualExclusion(t *testing.T) { + data := t.TempDir() + release, err := dataLock(data) + if err != nil { + t.Fatalf("dataLock: %v", err) + } + + // A second exclusive, non-blocking flock must FAIL while the lock is held. + f, err := os.OpenFile(filepath.Join(data, ".lock"), os.O_RDWR, 0o600) + if err != nil { + t.Fatal(err) + } + defer f.Close() + if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err == nil { + syscall.Flock(int(f.Fd()), syscall.LOCK_UN) + t.Fatal("second flock acquired while the lock was held — no mutual exclusion") + } + + release() + + // After release, the lock is acquirable again. + if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil { + t.Fatalf("flock should succeed after release: %v", err) + } + _ = syscall.Flock(int(f.Fd()), syscall.LOCK_UN) +} diff --git a/cmd/devbox-hub/lock_unix.go b/cmd/devbox-hub/lock_unix.go new file mode 100644 index 0000000..567551c --- /dev/null +++ b/cmd/devbox-hub/lock_unix.go @@ -0,0 +1,32 @@ +//go:build unix + +package main + +import ( + "os" + "path/filepath" + "syscall" +) + +// dataLock takes an exclusive advisory lock on /.lock so the gc sweep and +// `backup` can never run at the same time — otherwise gc could delete a blob +// mid-backup, leaving the backup's DB referencing a blob its copy missed. flock +// is released automatically if the process dies, so a crash can't wedge it. +// Returns a release func; call it (idempotent enough) when the critical section ends. +func dataLock(data string) (func(), error) { + if err := os.MkdirAll(data, 0o700); err != nil { + return nil, err + } + f, err := os.OpenFile(filepath.Join(data, ".lock"), os.O_CREATE|os.O_RDWR, 0o600) + if err != nil { + return nil, err + } + if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil { + f.Close() + return nil, err + } + return func() { + syscall.Flock(int(f.Fd()), syscall.LOCK_UN) + f.Close() + }, nil +} diff --git a/cmd/devbox-hub/main.go b/cmd/devbox-hub/main.go index c344da1..bfee547 100644 --- a/cmd/devbox-hub/main.go +++ b/cmd/devbox-hub/main.go @@ -136,7 +136,13 @@ func serveCmd() *cobra.Command { t := time.NewTicker(gcEvery) defer t.Stop() for range t.C { + release, lerr := dataLock(data) // serialize with `backup` so a sweep can't strand a backup mid-copy + if lerr != nil { + fmt.Fprintf(out, "gc sweep lock error: %v\n", lerr) + continue + } snaps, chunks, err := runGC(db, store, gcKeep, 0, false) + release() if err != nil { fmt.Fprintf(out, "gc sweep error: %v\n", err) continue @@ -422,6 +428,15 @@ func gcCmd() *cobra.Command { if err != nil { return err } + // A real sweep deletes blobs, so serialize with `backup` (dry-run is + // read-only and must not block on a long backup, so it skips the lock). + if !dryRun { + release, lerr := dataLock(data) + if lerr != nil { + return lerr + } + defer release() + } snaps, chunks, err := runGC(db, store, keep, keepDays, dryRun) if err != nil { return err @@ -658,6 +673,15 @@ func backupCmd() *cobra.Command { return err } defer db.Close() + // Hold the data lock across BOTH the DB snapshot and the blob copy so a + // concurrent gc sweep can't delete a blob the snapshot references mid-copy, + // leaving an internally-inconsistent backup (a dangling ref you'd only + // discover on restore). DB-first then blobs is the correct order. + release, lerr := dataLock(data) + if lerr != nil { + return lerr + } + defer release() if err := db.BackupTo(filepath.Join(destDir, "devbox-hub.db")); err != nil { return err } @@ -815,8 +839,8 @@ func fsckCmd() *cobra.Command { if asJSON { type result struct { - Scanned int `json:"scanned"` - Corrupt []corruptEntry `json:"corrupt"` + Scanned int `json:"scanned"` + Corrupt []corruptEntry `json:"corrupt"` Dangling []danglingEntry `json:"dangling"` } enc := json.NewEncoder(cmd.OutOrStdout())