fix(library): a scan and a duplicate-resolver pass never touch the files at once (#8197)
release / go (push) Successful in 2m17s
release / web (push) Successful in 1m29s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m28s
release / android (push) Successful in 4m25s
release / Build signed APK (push) Successful in 5m8s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 15s
release / Verify release artifacts (tag releases only) (push) Skipped
release / go (push) Successful in 2m17s
release / web (push) Successful in 1m29s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m28s
release / android (push) Successful in 4m25s
release / Build signed APK (push) Successful in 5m8s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 15s
release / Verify release artifacts (tag releases only) (push) Skipped
The resolver checked for a running scan once, at the start of a pass. At boot that check could run before the startup scan recorded its run, and a scan that began mid-pass was never seen, so the pass removed copies the scan had listed (23 "no such file" errors on dev, 2026-11-28). A gate now keeps the two apart for their whole length: a full scan and each watcher batch hold it, a scan that meets a pass waits for it (up to 30 minutes, then gives up with a named error), and a pass that meets a scan is put off and tried again a minute later. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -593,9 +593,8 @@ func (w *DuplicateResolveWorker) tickOnce(ctx context.Context) (waitForScan bool
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
// A scan in flight has listed the files it will reconcile: a copy removed
|
// A pass waits out a whole scan run, whose later stages re-read files
|
||||||
// now is marked present again when it does. A run past StuckScanThreshold
|
// too. A run past StuckScanThreshold is a dead one.
|
||||||
// is a dead one.
|
|
||||||
running, err := scanRunning(ctx, q)
|
running, err := scanRunning(ctx, q)
|
||||||
switch {
|
switch {
|
||||||
case err != nil:
|
case err != nil:
|
||||||
@@ -606,6 +605,13 @@ func (w *DuplicateResolveWorker) tickOnce(ctx context.Context) (waitForScan bool
|
|||||||
case running:
|
case running:
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
// A run can begin after the check above, and a watcher batch has no run
|
||||||
|
// at all: filesGate keeps either from reading files while this pass
|
||||||
|
// removes them.
|
||||||
|
if !filesGate.tryAcquire() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
defer filesGate.release()
|
||||||
res, err := ResolveDuplicatesWith(ctx, w.pool, w.logger, w.dataDir, w.settings.Get().AutoResolve, w.src)
|
res, err := ResolveDuplicatesWith(ctx, w.pool, w.logger, w.dataDir, w.settings.Get().AutoResolve, w.src)
|
||||||
if err != nil && ctx.Err() == nil {
|
if err != nil && ctx.Err() == nil {
|
||||||
w.logger.Warn("duplicate resolve: pass failed", "err", err)
|
w.logger.Warn("duplicate resolve: pass failed", "err", err)
|
||||||
|
|||||||
@@ -578,7 +578,9 @@ func TestResolveDuplicates_AgreeingLookupsDecideAnAcousticGroup_Integration(t *t
|
|||||||
|
|
||||||
// A pass while a scan is walking the library waits for it: a copy removed
|
// A pass while a scan is walking the library waits for it: a copy removed
|
||||||
// under the walk is marked present again when the scan reconciles. A run
|
// under the walk is marked present again when the scan reconciles. A run
|
||||||
// stuck past StuckScanThreshold does not hold the resolver up.
|
// stuck past StuckScanThreshold does not hold the resolver up. Files being
|
||||||
|
// read with no run recorded (a scan whose run row hasn't landed yet, a
|
||||||
|
// watcher batch) hold it up as well.
|
||||||
func TestDuplicateResolveWorker_WaitsForARunningScan_Integration(t *testing.T) {
|
func TestDuplicateResolveWorker_WaitsForARunningScan_Integration(t *testing.T) {
|
||||||
f := seedTrimmedAlbum(t)
|
f := seedTrimmedAlbum(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@@ -598,6 +600,21 @@ func TestDuplicateResolveWorker_WaitsForARunningScan_Integration(t *testing.T) {
|
|||||||
if w.tickOnce(ctx) {
|
if w.tickOnce(ctx) {
|
||||||
t.Error("a scan stuck for two hours held the resolver up")
|
t.Error("a scan stuck for two hours held the resolver up")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, err := f.pool.Exec(ctx, `DELETE FROM scan_runs`); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !filesGate.tryAcquire() {
|
||||||
|
t.Fatal("the files gate was already held")
|
||||||
|
}
|
||||||
|
waited := w.tickOnce(ctx)
|
||||||
|
filesGate.release()
|
||||||
|
if !waited {
|
||||||
|
t.Error("a pass ran while files were being read, with no scan run recorded")
|
||||||
|
}
|
||||||
|
if w.tickOnce(ctx) {
|
||||||
|
t.Error("a pass was put off with nothing reading files")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AcoustID knows the recording as no song of the album: every copy goes. With
|
// AcoustID knows the recording as no song of the album: every copy goes. With
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package library
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// filesGate keeps reading the library's files apart from removing them. A
|
||||||
|
// full scan holds it for its whole length and the watcher for each batch;
|
||||||
|
// a duplicate-resolver pass, which removes the copies it merges, runs only
|
||||||
|
// while no one else holds it. Without it, a file a scan has listed could be
|
||||||
|
// removed under the scan, and a file read just before its removal would put
|
||||||
|
// back a track whose file is gone.
|
||||||
|
//
|
||||||
|
// A scan that starts during a resolver pass waits for the pass to finish,
|
||||||
|
// up to filesWaitLimit. A resolver pass that finds the gate taken is put off
|
||||||
|
// and tried again (DuplicateResolveWorker).
|
||||||
|
var filesGate = make(gate, 1)
|
||||||
|
|
||||||
|
// filesWaitLimit is how long a scan waits for a resolver pass to let go of
|
||||||
|
// the files. A pass is capped at resolveMergeCap merges and takes minutes;
|
||||||
|
// one still holding them after this long is stuck, and the scan gives up
|
||||||
|
// rather than wait forever.
|
||||||
|
const filesWaitLimit = 30 * time.Minute
|
||||||
|
|
||||||
|
// errFilesBusy is a scan giving up on a resolver pass that held the files
|
||||||
|
// past filesWaitLimit.
|
||||||
|
var errFilesBusy = errors.New("library files held by a duplicate-resolver pass past the wait limit")
|
||||||
|
|
||||||
|
// gate is a lock that a waiter can give up on.
|
||||||
|
type gate chan struct{}
|
||||||
|
|
||||||
|
// acquire takes the gate, or returns ctx's error when ctx ends first and
|
||||||
|
// errFilesBusy once limit has passed.
|
||||||
|
func (g gate) acquire(ctx context.Context, limit time.Duration) error {
|
||||||
|
t := time.NewTimer(limit)
|
||||||
|
defer t.Stop()
|
||||||
|
select {
|
||||||
|
case g <- struct{}{}:
|
||||||
|
return nil
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
case <-t.C:
|
||||||
|
return errFilesBusy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// tryAcquire takes the gate if no one holds it, and reports whether it did.
|
||||||
|
func (g gate) tryAcquire() bool {
|
||||||
|
select {
|
||||||
|
case g <- struct{}{}:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// release lets go of a gate acquire or tryAcquire took.
|
||||||
|
func (g gate) release() { <-g }
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
package library
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A gate is taken by one holder at a time; a waiter gets it once the holder
|
||||||
|
// lets go, and gives up when its context ends or its limit passes.
|
||||||
|
func TestGate_OneHolderAndAWaiterThatCanGiveUp(t *testing.T) {
|
||||||
|
g := make(gate, 1)
|
||||||
|
if !g.tryAcquire() {
|
||||||
|
t.Fatal("an empty gate could not be taken")
|
||||||
|
}
|
||||||
|
if g.tryAcquire() {
|
||||||
|
t.Fatal("a held gate was taken a second time")
|
||||||
|
}
|
||||||
|
if err := g.acquire(context.Background(), 20*time.Millisecond); !errors.Is(err, errFilesBusy) {
|
||||||
|
t.Errorf("waiting past the limit: %v, want errFilesBusy", err)
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
if err := g.acquire(ctx, time.Minute); !errors.Is(err, context.Canceled) {
|
||||||
|
t.Errorf("waiting with an ended context: %v, want context.Canceled", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got := make(chan error, 1)
|
||||||
|
go func() { got <- g.acquire(context.Background(), time.Minute) }()
|
||||||
|
select {
|
||||||
|
case err := <-got:
|
||||||
|
t.Fatalf("a waiter got a held gate: %v", err)
|
||||||
|
case <-time.After(50 * time.Millisecond):
|
||||||
|
}
|
||||||
|
g.release()
|
||||||
|
select {
|
||||||
|
case err := <-got:
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("a waiter after release: %v", err)
|
||||||
|
}
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
t.Fatal("a waiter did not get the gate once it was released")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A scan that starts while a duplicate-resolver pass holds the files reads
|
||||||
|
// none of them until the pass lets go, so no file it lists is removed under
|
||||||
|
// it.
|
||||||
|
func TestScanner_WaitsForAPassHoldingTheFiles_Integration(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
pool := dbtest.Open(t)
|
||||||
|
if _, err := pool.Exec(ctx, "TRUNCATE tracks, albums, artists RESTART IDENTITY CASCADE"); err != nil {
|
||||||
|
t.Fatalf("truncate: %v", err)
|
||||||
|
}
|
||||||
|
root := t.TempDir()
|
||||||
|
writeTestMP3(t, filepath.Join(root, "Tin Orchard/Dusk/01.mp3"), map[string]string{
|
||||||
|
"TIT2": "Dusk", "TPE1": "Tin Orchard", "TALB": "Dusk", "TRCK": "1",
|
||||||
|
})
|
||||||
|
scanner := New(pool, slog.New(slog.NewTextHandler(io.Discard, nil)), []string{root}, nil)
|
||||||
|
|
||||||
|
if !filesGate.tryAcquire() {
|
||||||
|
t.Fatal("the files gate was already held")
|
||||||
|
}
|
||||||
|
type result struct {
|
||||||
|
stats Stats
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
done := make(chan result, 1)
|
||||||
|
go func() {
|
||||||
|
stats, err := scanner.Scan(ctx, nil)
|
||||||
|
done <- result{stats, err}
|
||||||
|
}()
|
||||||
|
select {
|
||||||
|
case r := <-done:
|
||||||
|
filesGate.release()
|
||||||
|
t.Fatalf("the scan ran while a pass held the files: %+v, %v", r.stats, r.err)
|
||||||
|
case <-time.After(200 * time.Millisecond):
|
||||||
|
}
|
||||||
|
filesGate.release()
|
||||||
|
select {
|
||||||
|
case r := <-done:
|
||||||
|
if r.err != nil || r.stats.Added != 1 {
|
||||||
|
t.Errorf("scan after the pass: %+v, %v; want one song added", r.stats, r.err)
|
||||||
|
}
|
||||||
|
case <-time.After(30 * time.Second):
|
||||||
|
t.Fatal("the scan did not run once the pass let go of the files")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -146,6 +146,10 @@ func New(pool *pgxpool.Pool, logger *slog.Logger, paths []string, settings *Fing
|
|||||||
// processed file. Used by the orchestrator to drive partial-tally writes.
|
// processed file. Used by the orchestrator to drive partial-tally writes.
|
||||||
func (s *Scanner) Scan(ctx context.Context, progressCb func(Stats)) (Stats, error) {
|
func (s *Scanner) Scan(ctx context.Context, progressCb func(Stats)) (Stats, error) {
|
||||||
var stats Stats
|
var stats Stats
|
||||||
|
if err := filesGate.acquire(ctx, filesWaitLimit); err != nil {
|
||||||
|
return stats, fmt.Errorf("library scan: %w", err)
|
||||||
|
}
|
||||||
|
defer filesGate.release()
|
||||||
q := dbq.New(s.pool)
|
q := dbq.New(s.pool)
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
@@ -564,6 +568,10 @@ func (s *Scanner) scanFile(
|
|||||||
// album IDs whose tracks were added or updated, so the caller can enrich
|
// album IDs whose tracks were added or updated, so the caller can enrich
|
||||||
// just those albums inline rather than waiting for a batch pass.
|
// just those albums inline rather than waiting for a batch pass.
|
||||||
func (s *Scanner) ScanFiles(ctx context.Context, paths []string) ([]pgtype.UUID, error) {
|
func (s *Scanner) ScanFiles(ctx context.Context, paths []string) ([]pgtype.UUID, error) {
|
||||||
|
if err := filesGate.acquire(ctx, filesWaitLimit); err != nil {
|
||||||
|
return nil, fmt.Errorf("library watch: %w", err)
|
||||||
|
}
|
||||||
|
defer filesGate.release()
|
||||||
q := dbq.New(s.pool)
|
q := dbq.New(s.pool)
|
||||||
seen := make(map[[16]byte]struct{})
|
seen := make(map[[16]byte]struct{})
|
||||||
changed := make([]pgtype.UUID, 0)
|
changed := make([]pgtype.UUID, 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user