fix(hub): flock gc against backup so a sweep can't strand a backup mid-copy 🔒💾
Some checks are pending
CI / vet-build-test (push) Waiting to run
Some checks are pending
CI / vet-build-test (push) Waiting to run
`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 <data>/.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.
This commit is contained in:
parent
d367e0ca8f
commit
f51e69839a
4 changed files with 106 additions and 2 deletions
9
cmd/devbox-hub/lock_other.go
Normal file
9
cmd/devbox-hub/lock_other.go
Normal file
|
|
@ -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 }
|
||||||
39
cmd/devbox-hub/lock_test.go
Normal file
39
cmd/devbox-hub/lock_test.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
32
cmd/devbox-hub/lock_unix.go
Normal file
32
cmd/devbox-hub/lock_unix.go
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
//go:build unix
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
// dataLock takes an exclusive advisory lock on <data>/.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
|
||||||
|
}
|
||||||
|
|
@ -136,7 +136,13 @@ func serveCmd() *cobra.Command {
|
||||||
t := time.NewTicker(gcEvery)
|
t := time.NewTicker(gcEvery)
|
||||||
defer t.Stop()
|
defer t.Stop()
|
||||||
for range t.C {
|
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)
|
snaps, chunks, err := runGC(db, store, gcKeep, 0, false)
|
||||||
|
release()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(out, "gc sweep error: %v\n", err)
|
fmt.Fprintf(out, "gc sweep error: %v\n", err)
|
||||||
continue
|
continue
|
||||||
|
|
@ -422,6 +428,15 @@ func gcCmd() *cobra.Command {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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)
|
snaps, chunks, err := runGC(db, store, keep, keepDays, dryRun)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -658,6 +673,15 @@ func backupCmd() *cobra.Command {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer db.Close()
|
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 {
|
if err := db.BackupTo(filepath.Join(destDir, "devbox-hub.db")); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue