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
This commit is contained in:
2026-01-30 13:38:36 -05:00
parent c2f843eabd
commit 2297455923

View File

@@ -298,6 +298,12 @@ public abstract class BaseDownloadService : IDownloadService
song.LocalPath = localPath; 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 // Check if this track belongs to a playlist and update M3U
if (PlaylistSyncService != null) 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) if (!isCache)
{ {
await LocalLibraryService.RegisterDownloadedSongAsync(song, localPath);
// Trigger a Subsonic library rescan (with debounce) // Trigger a Subsonic library rescan (with debounce)
_ = Task.Run(async () => _ = Task.Run(async () =>
{ {