fix(scanner): handle nil mainCtx in Watcher to prevent panic

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan
2026-03-01 10:50:24 -05:00
parent 2471bb9cf6
commit 3476be01f7
2 changed files with 15 additions and 0 deletions
+6
View File
@@ -145,6 +145,12 @@ func (w *watcher) Watch(ctx context.Context, lib *model.Library) error {
w.mu.Lock() w.mu.Lock()
defer w.mu.Unlock() defer w.mu.Unlock()
// If Run() hasn't been called yet, mainCtx will be nil - skip watching
if w.mainCtx == nil {
log.Debug(ctx, "Watcher not started yet, skipping watch for library", "libraryID", lib.ID, "name", lib.Name)
return nil
}
// Stop existing watcher if any // Stop existing watcher if any
if existingInstance, exists := w.libraryWatchers[lib.ID]; exists { if existingInstance, exists := w.libraryWatchers[lib.ID]; exists {
log.Debug(ctx, "Stopping existing watcher before starting new one", "libraryID", lib.ID, "name", lib.Name) log.Debug(ctx, "Stopping existing watcher before starting new one", "libraryID", lib.ID, "name", lib.Name)
+9
View File
@@ -54,6 +54,15 @@ var _ = Describe("Watcher", func() {
} }
}) })
Describe("Watch before Run", func() {
It("returns nil and does not panic when mainCtx is nil", func() {
w.mainCtx = nil
err := w.Watch(ctx, lib)
Expect(err).ToNot(HaveOccurred())
Expect(w.libraryWatchers).To(BeEmpty())
})
})
Describe("Target Collection and Deduplication", func() { Describe("Target Collection and Deduplication", func() {
BeforeEach(func() { BeforeEach(func() {
// Start watcher in background // Start watcher in background