fix(scanner): add nil guards to cursor wrapping (#5139)

* fix(persistence): add nil guards to cursor wrapping in folder and mediafile repos

Prevent SIGSEGV panic when queryWithStableResults yields a zero-value
struct on the rows.Err() path (e.g., "database is locked" during
concurrent scanning). Extract cursor wrapping into wrapFolderCursor and
wrapMediaFileCursor with nil checks matching the existing pattern in
album_repository.go.

Fixes #5138

* fix(persistence): wrap original cursor error in nil guard messages

Use %w to preserve the underlying error (e.g., "database is locked")
so callers can use errors.Is/As for root cause analysis. Tests now
verify the original error is accessible via errors.Is.

* fix(persistence): add nil guards and error wrapping in album, folder, and mediafile cursor functions

Signed-off-by: Deluan <deluan@navidrome.org>

---------

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan Quintão
2026-03-03 07:58:14 -05:00
committed by GitHub
parent c885766854
commit ed4c0ef432
6 changed files with 152 additions and 15 deletions
+41
View File
@@ -1,6 +1,7 @@
package persistence
import (
"errors"
"fmt"
"time"
@@ -743,6 +744,46 @@ var _ = Describe("AlbumRepository", func() {
_, _ = albumRepo.executeSQL(squirrel.Delete("album").Where(squirrel.Eq{"id": album.ID}))
})
})
Describe("wrapAlbumCursor", func() {
It("does not panic when the cursor yields a dbAlbum with nil Album", func() {
// Simulate what queryWithStableResults does on the rows.Err() path:
// it yields a zero-value dbAlbum (where Album is nil) with an error.
dbErr := fmt.Errorf("database is locked")
cursor := func(yield func(dbAlbum, error) bool) {
var empty dbAlbum // Album pointer is nil
yield(empty, dbErr)
}
// wrapAlbumCursor should handle the nil Album without panicking
wrappedCursor := wrapAlbumCursor(cursor)
var gotErr error
Expect(func() {
for _, err := range wrappedCursor {
gotErr = err
}
}).ToNot(Panic())
Expect(gotErr).To(HaveOccurred())
Expect(gotErr.Error()).To(ContainSubstring("unexpected nil album"))
Expect(errors.Is(gotErr, dbErr)).To(BeTrue(), "should wrap the original cursor error")
})
It("yields albums from a valid cursor", func() {
album := &model.Album{ID: "a1", Name: "Test"}
cursor := func(yield func(dbAlbum, error) bool) {
yield(dbAlbum{Album: album}, nil)
}
wrappedCursor := wrapAlbumCursor(cursor)
var albums []model.Album
for a, err := range wrappedCursor {
Expect(err).ToNot(HaveOccurred())
albums = append(albums, a)
}
Expect(albums).To(HaveLen(1))
Expect(albums[0].ID).To(Equal("a1"))
})
})
})
func _p(id, name string, sortName ...string) model.Participant {