From 6c14fc299c833603ef103e75e2fd8a6a4d5eb4c4 Mon Sep 17 00:00:00 2001 From: Josh Patra Date: Mon, 9 Feb 2026 12:26:08 -0500 Subject: [PATCH] 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 --- allstarr/Services/Common/BaseDownloadService.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/allstarr/Services/Common/BaseDownloadService.cs b/allstarr/Services/Common/BaseDownloadService.cs index 9ccfad8..0b2e55f 100644 --- a/allstarr/Services/Common/BaseDownloadService.cs +++ b/allstarr/Services/Common/BaseDownloadService.cs @@ -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 @@ -444,7 +446,10 @@ public abstract class BaseDownloadService : IDownloadService } finally { - DownloadLock.Release(); + if (lockHeld) + { + DownloadLock.Release(); + } } }