Files
navidrome/core/storage/local/watcher.go
T
Deluan 0790f66627 fix(scanner): increase watcher channel buffers to prevent dropped filesystem events
When files were moved between libraries, the small channel buffers (size 1)
throughout the watcher pipeline caused backpressure that led to dropped
filesystem events. This meant only some of the affected folders were scanned,
preventing cross-library move detection from working correctly.

Increase all watcher channel buffers to 500 and switch to blocking sends
to ensure no filesystem events are silently dropped.
2026-03-12 17:07:34 -04:00

58 lines
1.5 KiB
Go

package local
import (
"context"
"errors"
"path/filepath"
"strings"
"github.com/navidrome/navidrome/log"
"github.com/rjeczalik/notify"
)
// Start starts a watcher on the whole FS and returns a channel to send detected changes.
// It uses `notify` to detect changes in the filesystem, so it may not work on all platforms/use-cases.
// Notoriously, it does not work on some networked mounts and Windows with WSL2.
func (s *localStorage) Start(ctx context.Context) (<-chan string, error) {
if !s.watching.CompareAndSwap(false, true) {
return nil, errors.New("watcher already started")
}
input := make(chan notify.EventInfo, 500)
output := make(chan string, 500)
started := make(chan struct{})
go func() {
defer close(input)
defer close(output)
libPath := filepath.Join(s.u.Path, "...")
log.Debug(ctx, "Starting watcher", "lib", libPath)
err := notify.Watch(libPath, input, WatchEvents)
if err != nil {
log.Error("Error starting watcher", "lib", libPath, err)
return
}
defer notify.Stop(input)
close(started) // signals the main goroutine we have started
for {
select {
case event := <-input:
log.Trace(ctx, "Detected change", "event", event, "lib", s.u.Path)
name := event.Path()
name = strings.Replace(name, s.resolvedPath, s.u.Path, 1)
output <- name
case <-ctx.Done():
log.Debug(ctx, "Stopping watcher", "path", s.u.Path)
s.watching.Store(false)
return
}
}
}()
select {
case <-started:
case <-ctx.Done():
}
return output, nil
}