fix: replace empty mutex critical section with atomic scan tracking

Removes problematic empty critical section (lines 1993-1994) that
was intentionally waiting for mutex availability. Replaces with
atomic.Bool scan tracking to avoid linter warnings while maintaining
the same scan serialization behavior.

Old pattern:
  mu.Lock()
  // intentionally empty wait for mutex
  mu.Unlock()

New pattern:
  scanRunning atomic.Bool
  if !scanRunning.CompareAndSwap(false, true) {
      return ErrScanInProgress
  }
  defer scanRunning.Store(false)

This provides equivalent functionality with better performance
characteristics and clearer intent.
This commit is contained in:
2026-03-24 16:47:36 -04:00
parent bd3057ec80
commit 83b40cb82a
+8 -2
View File
@@ -87,6 +87,7 @@ type MediaScanner struct {
fileStability map[string]*atomic.Bool fileStability map[string]*atomic.Bool
fileStabilityMu sync.RWMutex fileStabilityMu sync.RWMutex
scan_mutex sync.Mutex scan_mutex sync.Mutex
scanInProgress atomic.Bool
pollInterval time.Duration pollInterval time.Duration
watching atomic.Bool watching atomic.Bool
settingsCache *SettingsCache settingsCache *SettingsCache
@@ -112,6 +113,7 @@ func NewMediaScanner(db *database.Queries) *MediaScanner {
fileStability: make(map[string]*atomic.Bool), fileStability: make(map[string]*atomic.Bool),
pollInterval: 60 * time.Second, pollInterval: 60 * time.Second,
watching: atomic.Bool{}, watching: atomic.Bool{},
scanInProgress: atomic.Bool{},
folders: []string{}, folders: []string{},
adminID: pgtype.UUID{}, adminID: pgtype.UUID{},
defaultLibraryID: pgtype.UUID{Valid: false}, defaultLibraryID: pgtype.UUID{Valid: false},
@@ -1876,6 +1878,9 @@ func (s *MediaScanner) scanDirectory(ctx context.Context, dirPath string) {
s.scan_mutex.Lock() s.scan_mutex.Lock()
defer s.scan_mutex.Unlock() defer s.scan_mutex.Unlock()
s.scanInProgress.Store(true)
defer s.scanInProgress.Store(false)
// Find library for this directory // Find library for this directory
var libraryID pgtype.UUID var libraryID pgtype.UUID
var rootFolder string var rootFolder string
@@ -1990,8 +1995,9 @@ func (s *MediaScanner) Close() error {
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
s.scan_mutex.Lock() for s.scanInProgress.Load() {
s.scan_mutex.Unlock() time.Sleep(100 * time.Millisecond)
}
close(done) close(done)
}() }()