🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
fix(scanner): prevent duplicate tracks when multiple missing files match same target (#5183)
Deluan Quintão55331b5fd92c
diff --git a/scanner/phase_2_missing_tracks.go b/scanner/phase_2_missing_tracks.goindex c47565036..8c258b833 100644--- a/scanner/phase_2_missing_tracks.go+++ b/scanner/phase_2_missing_tracks.go@@ -114,6 +114,10 @@ func (p *phaseMissingTracks) stages() []ppl.Stage[*missingTracks] {func (p *phaseMissingTracks) processMissingTracks(in *missingTracks) (*missingTracks, error) {hasMatches := false+ // Track which matched entries have already been consumed, so each matched track+ // is only used once. Without this, the same matched track could be paired with+ // multiple missing tracks, creating duplicate records with the same path.+ usedMatched := make(map[string]bool, len(in.matched))for _, ms := range in.missing {var exactMatch model.MediaFile@@ -121,6 +125,9 @@ func (p *phaseMissingTracks) processMissingTracks(in *missingTracks) (*missingTr// Identify exact and equivalent matchesfor _, mt := range in.matched {+ if usedMatched[mt.ID] {+ continue+ }if ms.Equals(mt) {exactMatch = mtbreak // Prioritize exact match@@ -138,13 +145,14 @@ func (p *phaseMissingTracks) processMissingTracks(in *missingTracks) (*missingTrlog.Error(p.ctx, "Scanner: Error moving matched track", "missing", ms.Path, "movedTo", exactMatch.Path, "lib", in.lib.Name, err)return nil, err}+ usedMatched[exactMatch.ID] = truep.totalMatched.Add(1)hasMatches = truecontinue}// If there is only one missing and one matched track, consider them equivalent (same PID)- if len(in.missing) == 1 && len(in.matched) == 1 {+ if len(in.missing) == 1 && len(in.matched) == 1 && !usedMatched[in.matched[0].ID] {singleMatch := in.matched[0]log.Debug(p.ctx, "Scanner: Found track with same persistent ID in a new place", "missing", ms.Path, "movedTo", singleMatch.Path, "lib", in.lib.Name)err := p.moveMatched(singleMatch, ms)@@ -152,6 +160,7 @@ func (p *phaseMissingTracks) processMissingTracks(in *missingTracks) (*missingTrlog.Error(p.ctx, "Scanner: Error updating matched track", "missing", ms.Path, "movedTo", singleMatch.Path, "lib", in.lib.Name, err)return nil, err}+ usedMatched[singleMatch.ID] = truep.totalMatched.Add(1)hasMatches = truecontinue@@ -165,6 +174,7 @@ func (p *phaseMissingTracks) processMissingTracks(in *missingTracks) (*missingTrlog.Error(p.ctx, "Scanner: Error updating matched track", "missing", ms.Path, "movedTo", equivalentMatch.Path, "lib", in.lib.Name, err)return nil, err}+ usedMatched[equivalentMatch.ID] = truep.totalMatched.Add(1)hasMatches = true}diff --git a/scanner/phase_2_missing_tracks_test.go b/scanner/phase_2_missing_tracks_test.goindex fa6ef5724..d54ceee40 100644--- a/scanner/phase_2_missing_tracks_test.go+++ b/scanner/phase_2_missing_tracks_test.go@@ -241,6 +241,39 @@ var _ = Describe("phaseMissingTracks", func() {Expect(movedTrack.Size).To(Equal(missingTrack.Size))})+ It("should not match the same target to multiple missing tracks (prevents duplicate paths)", func() {+ // Simulate a scenario where two missing tracks from different locations have the same+ // base filename and match the same newly imported track via IsEquivalent.+ // Without deduplication, both missing tracks would be "moved" to the same target,+ // creating two non-missing records with the same path.+ missingTrack1 := model.MediaFile{ID: "1", PID: "A", Path: "old_dir1/song.mp3", Title: "title1", Size: 100}+ missingTrack2 := model.MediaFile{ID: "2", PID: "A", Path: "old_dir2/song.mp3", Title: "title1", Size: 100}+ matchedTrack := model.MediaFile{ID: "3", PID: "A", Path: "new_dir/song.mp3", Title: "title1", Size: 200}++ _ = ds.MediaFile(ctx).Put(&missingTrack1)+ _ = ds.MediaFile(ctx).Put(&missingTrack2)+ _ = ds.MediaFile(ctx).Put(&matchedTrack)++ in := &missingTracks{+ missing: []model.MediaFile{missingTrack1, missingTrack2},+ matched: []model.MediaFile{matchedTrack},+ }++ _, err := phase.processMissingTracks(in)+ Expect(err).ToNot(HaveOccurred())+ // Only one of the missing tracks should be matched+ Expect(phase.totalMatched.Load()).To(Equal(uint32(1)))+ Expect(state.changesDetected.Load()).To(BeTrue())++ // The matched track should have been consumed by the first missing track+ movedTrack, _ := ds.MediaFile(ctx).Get("1")+ Expect(movedTrack.Path).To(Equal(matchedTrack.Path))++ // The second missing track should remain unchanged+ unmatchedTrack, _ := ds.MediaFile(ctx).Get("2")+ Expect(unmatchedTrack.Path).To(Equal(missingTrack2.Path))+ })+It("should return an error when there's an error moving the matched track", func() {missingTrack := model.MediaFile{ID: "1", PID: "A", Path: "path1.mp3", Tags: model.Tags{"title": []string{"title1"}}}matchedTrack := model.MediaFile{ID: "2", PID: "A", Path: "path1.mp3", Tags: model.Tags{"title": []string{"title1"}}}