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 // Acquire lock BEFORE checking existence to prevent race conditions with concurrent requests
await DownloadLock.WaitAsync(cancellationToken); await DownloadLock.WaitAsync(cancellationToken);
var lockHeld = true;
try try
{ {
@@ -288,6 +289,7 @@ public abstract class BaseDownloadService : IDownloadService
Logger.LogDebug("Download already in progress for {SongId}, waiting for completion...", songId); Logger.LogDebug("Download already in progress for {SongId}, waiting for completion...", songId);
// Release lock while waiting // Release lock while waiting
DownloadLock.Release(); DownloadLock.Release();
lockHeld = false;
// Wait for download to complete, checking every 100ms (faster than 500ms) // Wait for download to complete, checking every 100ms (faster than 500ms)
// Also respect cancellation token so client timeouts are handled immediately // Also respect cancellation token so client timeouts are handled immediately
@@ -444,7 +446,10 @@ public abstract class BaseDownloadService : IDownloadService
} }
finally finally
{ {
DownloadLock.Release(); if (lockHeld)
{
DownloadLock.Release();
}
} }
} }