From 67c4e249570c1928f3559a694427a6ce34adda67 Mon Sep 17 00:00:00 2001 From: Deluan Date: Fri, 21 Nov 2025 15:26:30 -0500 Subject: [PATCH] fix(scanner): defer artwork PreCache calls until after transaction commits The CacheWarmer was failing with data not found errors because PreCache was being called inside the database transaction before the data was committed. The CacheWarmer runs in a separate goroutine with its own database context and could not access the uncommitted data due to transaction isolation. Changed the persistChanges method in phase_1_folders.go to collect artwork IDs during the transaction and only call PreCache after the transaction successfully commits. This ensures the artwork data is visible to the CacheWarmer when it attempts to retrieve and cache the images. The fix eliminates the data not found errors and allows the cache warmer to properly pre-cache album and artist artwork during library scanning. Signed-off-by: Deluan --- scanner/phase_1_folders.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/scanner/phase_1_folders.go b/scanner/phase_1_folders.go index 2f6b62b2d..329029951 100644 --- a/scanner/phase_1_folders.go +++ b/scanner/phase_1_folders.go @@ -324,6 +324,9 @@ func (p *phaseFolders) persistChanges(entry *folderEntry) (*folderEntry, error) defer p.measure(entry)() p.state.changesDetected.Store(true) + // Collect artwork IDs to pre-cache after the transaction commits + var artworkIDs []model.ArtworkID + err := p.ds.WithTx(func(tx model.DataStore) error { // Instantiate all repositories just once per folder folderRepo := tx.Folder(p.ctx) @@ -362,7 +365,7 @@ func (p *phaseFolders) persistChanges(entry *folderEntry) (*folderEntry, error) return err } if entry.artists[i].Name != consts.UnknownArtist && entry.artists[i].Name != consts.VariousArtists { - entry.job.cw.PreCache(entry.artists[i].CoverArtID()) + artworkIDs = append(artworkIDs, entry.artists[i].CoverArtID()) } } @@ -374,7 +377,7 @@ func (p *phaseFolders) persistChanges(entry *folderEntry) (*folderEntry, error) return err } if entry.albums[i].Name != consts.UnknownAlbum { - entry.job.cw.PreCache(entry.albums[i].CoverArtID()) + artworkIDs = append(artworkIDs, entry.albums[i].CoverArtID()) } } @@ -411,6 +414,14 @@ func (p *phaseFolders) persistChanges(entry *folderEntry) (*folderEntry, error) if err != nil { log.Error(p.ctx, "Scanner: Error persisting changes to DB", "folder", entry.path, err) } + + // Pre-cache artwork after the transaction commits successfully + if err == nil { + for _, artID := range artworkIDs { + entry.job.cw.PreCache(artID) + } + } + return entry, err }