fix(library): a scan cut off by a restart no longer blocks saving library folders

A restart mid-scan left its scan_runs row unfinished, and saving library
folders refused ("A library scan is running") until a later scan reaped it,
up to 12 hours on. Startup now closes such rows with the reason before the
boot scan, as backups already do. Saving folders and the duplicate sweep
share one check that treats a run past StuckScanThreshold as dead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 22:02:00 -05:00
co-authored by Claude Opus 5.5
parent 1ff4bc8258
commit 785b15ab5f
7 changed files with 123 additions and 10 deletions
+57
View File
@@ -0,0 +1,57 @@
package library
import (
"context"
"errors"
"testing"
"github.com/jackc/pgx/v5/pgtype"
)
// A scan the last shutdown cut off is closed at startup with the reason, so
// it no longer reads as a scan in progress.
func TestRecoverScanRuns_ClosesARunTheLastShutdownCutOff_Integration(t *testing.T) {
pool := triggerTestPool(t)
ctx := context.Background()
var id pgtype.UUID
if err := pool.QueryRow(ctx,
`INSERT INTO scan_runs (started_at) VALUES (now() - interval '5 minutes') RETURNING id`).Scan(&id); err != nil {
t.Fatal(err)
}
RecoverScanRuns(ctx, pool, quietLogger())
var finished bool
var reason *string
if err := pool.QueryRow(ctx,
`SELECT finished_at IS NOT NULL, error_message FROM scan_runs WHERE id = $1`, id).Scan(&finished, &reason); err != nil {
t.Fatal(err)
}
if !finished || reason == nil || *reason == "" {
t.Errorf("finished %v, reason %v; want the run closed with why", finished, reason)
}
}
// Saving library folders waits for a scan that is running, but not for an
// unfinished run older than StuckScanThreshold: that one is dead.
func TestSetRoots_OnlyALiveScanHoldsUpSavingFolders_Integration(t *testing.T) {
pool := triggerTestPool(t)
clearSavedRoots(t, pool)
ctx := context.Background()
s := New(pool, quietLogger(), nil, nil)
if _, err := pool.Exec(ctx, `INSERT INTO scan_runs (started_at) VALUES (now() - interval '2 hours')`); err != nil {
t.Fatal(err)
}
if _, err := s.SetRoots(ctx, []RootChange{{Path: t.TempDir()}}); err != nil {
t.Errorf("a dead scan blocked saving folders: %v", err)
}
if _, err := pool.Exec(ctx, `INSERT INTO scan_runs (started_at) VALUES (now())`); err != nil {
t.Fatal(err)
}
var re *RootsError
if _, err := s.SetRoots(ctx, []RootChange{{Path: t.TempDir()}}); !errors.As(err, &re) {
t.Errorf("err %v; want a refusal while a scan is running", err)
}
}