fix: prevent duplicate downloads and lock release bug

- Track lock state to prevent double-release in finally block
- Fixes exception when download is already in progress
- Prevents duplicate file downloads with (1), (2), (3) suffixes
- Ensures proper lock management during concurrent download requests
This commit is contained in:
2026-02-09 12:26:08 -05:00
parent b03a4b85c9
commit 6c14fc299c

View File

@@ -264,6 +264,7 @@ public abstract class BaseDownloadService : IDownloadService
// Acquire lock BEFORE checking existence to prevent race conditions with concurrent requests
await DownloadLock.WaitAsync(cancellationToken);
var lockHeld = true;
try
{
@@ -288,6 +289,7 @@ public abstract class BaseDownloadService : IDownloadService
Logger.LogDebug("Download already in progress for {SongId}, waiting for completion...", songId);
// Release lock while waiting
DownloadLock.Release();
lockHeld = false;
// Wait for download to complete, checking every 100ms (faster than 500ms)
// Also respect cancellation token so client timeouts are handled immediately
@@ -443,10 +445,13 @@ public abstract class BaseDownloadService : IDownloadService
throw;
}
finally
{
if (lockHeld)
{
DownloadLock.Release();
}
}
}
protected async Task DownloadRemainingAlbumTracksAsync(string albumExternalId, string excludeTrackExternalId)
{