From 2297455923eae325c76bc0e5abf3aeeb09688d84 Mon Sep 17 00:00:00 2001 From: Josh Patra Date: Fri, 30 Jan 2026 13:38:36 -0500 Subject: [PATCH] fix: prevent duplicate downloads by registering before releasing lock - Race condition fixed where multiple threads could download same song - RegisterDownloadedSongAsync now called before lock release - Second thread finds registered mapping and skips download - Eliminates duplicate files with (1), (2) suffixes --- allstarr/Services/Common/BaseDownloadService.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/allstarr/Services/Common/BaseDownloadService.cs b/allstarr/Services/Common/BaseDownloadService.cs index 4c2ae84..26f014a 100644 --- a/allstarr/Services/Common/BaseDownloadService.cs +++ b/allstarr/Services/Common/BaseDownloadService.cs @@ -298,6 +298,12 @@ public abstract class BaseDownloadService : IDownloadService song.LocalPath = localPath; + // Register BEFORE releasing lock to prevent race conditions + if (!isCache) + { + await LocalLibraryService.RegisterDownloadedSongAsync(song, localPath); + } + // Check if this track belongs to a playlist and update M3U if (PlaylistSyncService != null) { @@ -316,11 +322,9 @@ public abstract class BaseDownloadService : IDownloadService } } - // Only register and scan if NOT in cache mode + // Trigger library scan and album download AFTER releasing lock if (!isCache) { - await LocalLibraryService.RegisterDownloadedSongAsync(song, localPath); - // Trigger a Subsonic library rescan (with debounce) _ = Task.Run(async () => {