fix(server): a background job that panics no longer stops the server (#8002)
release / go (push) Successful in 2m6s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m18s
release / android (push) Successful in 4m19s
release / Build signed APK (push) Successful in 4m56s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 30s
release / Verify release artifacts (tag releases only) (push) Skipped

Background jobs run in their own goroutines, outside the router's panic
recovery, and only five of them recovered. A panic in the scrobble,
similarity, co-play, gc, tags, compactor, notification, backup or
safety-net loop, or in a scan or rebuild started off a request, ended
the process and every stream in it.

internal/periodic now runs each job: a pass at start (or after one
interval, Delayed), then one per interval and on any WakeOn channel,
each pass under recover with the stack logged. Detached one-shot work
(scans, the lazy system-playlist build, bulk cover refetch, leveled
prerender, the backup run, the playlist catch-up) runs under
periodic.Safe. The duplicate resolver keeps its own loop for its
retry-soon timer, and its pass already recovers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 12:38:56 -05:00
co-authored by Claude Opus 5.5
parent 9b36effbaf
commit 125099ede2
21 changed files with 281 additions and 206 deletions
+15 -21
View File
@@ -30,6 +30,7 @@ import (
"git.fabledsword.com/bvandeusen/minstrel/internal/lookups"
"git.fabledsword.com/bvandeusen/minstrel/internal/mailer"
"git.fabledsword.com/bvandeusen/minstrel/internal/notifications"
"git.fabledsword.com/bvandeusen/minstrel/internal/periodic"
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
"git.fabledsword.com/bvandeusen/minstrel/internal/recsettings"
"git.fabledsword.com/bvandeusen/minstrel/internal/scrobble"
@@ -102,22 +103,15 @@ const safetyNetScanInterval = 12 * time.Hour
func runSafetyNetScans(ctx context.Context, pool *pgxpool.Pool, scanner *library.Scanner,
enricher *coverart.Enricher, logger *slog.Logger, scanCfg library.RunScanConfig,
) {
ticker := time.NewTicker(safetyNetScanInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
started, existing, err := library.TryStartScan(ctx, pool, scanner, enricher, logger, scanCfg)
if err != nil {
logger.Error("safety-net scan: try start failed", "err", err)
} else if !started && existing != nil {
logger.Info("safety-net scan skipped — prior still in flight",
"in_flight_id", existing.ID)
}
periodic.Run(ctx, logger, "safety-net scan", safetyNetScanInterval, func(ctx context.Context) {
started, existing, err := library.TryStartScan(ctx, pool, scanner, enricher, logger, scanCfg)
if err != nil {
logger.Error("safety-net scan: try start failed", "err", err)
} else if !started && existing != nil {
logger.Info("safety-net scan skipped — prior still in flight",
"in_flight_id", existing.ID)
}
}
}, periodic.Delayed())
}
func run() error {
@@ -257,7 +251,7 @@ func run() error {
// closed first, so it doesn't read as a scan still going.
library.RecoverScanRuns(ctx, pool, logger.With("component", "scan_run"))
if cfg.Library.ScanOnStartup && len(roots) > 0 {
go func() {
go periodic.Safe(ctx, logger, "startup scan", func(ctx context.Context) {
if _, err := library.RunScan(ctx, pool, scanner, coverEnricher,
logger.With("component", "scan_run"),
library.RunScanConfig{
@@ -269,11 +263,11 @@ func run() error {
); err != nil {
logger.Warn("startup scan run failed", "err", err)
}
}()
})
} else {
// No startup file walk, but still run backfill + enrich so cover
// art keeps arriving with scan-on-startup off.
go func() {
go periodic.Safe(ctx, logger, "boot scan", func(ctx context.Context) {
if _, err := library.RunScan(ctx, pool, nil, coverEnricher,
logger.With("component", "scan_run"),
library.RunScanConfig{
@@ -285,7 +279,7 @@ func run() error {
); err != nil {
logger.Warn("boot-only scan run failed", "err", err)
}
}()
})
}
// ListenBrainz scrobble worker: every 30s, drains up to 50 pending rows.
@@ -447,11 +441,11 @@ func run() error {
// fsnotify, scanning just the affected paths + enriching their albums
// inline. Replaces the removed configurable scan scheduler.
watcher := library.NewWatcher(scanner, coverEnricher, logger.With("component", "watcher"))
go func() {
go periodic.Safe(ctx, logger, "library watcher", func(ctx context.Context) {
if werr := watcher.Run(ctx); werr != nil {
logger.Warn("library watcher exited", "err", werr)
}
}()
})
// Safety-net delta walk backstops the watcher (inotify limits / dropped
// events) at a fixed low frequency. No operator config.
go runSafetyNetScans(ctx, pool, scanner, coverEnricher,