From 845f6406c079d082e4ea9683b3c1966698967c90 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 4 Oct 2025 17:59:22 -0500 Subject: delay lock acquisition --- src/slskd/Transfers/Downloads/DownloadService.cs | 20 ++++++++++---------- src/slskd/Transfers/Uploads/UploadService.cs | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index eafbf388..be9278b2 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -203,16 +203,6 @@ namespace slskd.Transfers.Downloads /// Thrown if a download matching the username and filename is already tracked by Soulseek.NET. public async Task DownloadAsync(Transfer transfer) { - var lockName = $"{nameof(DownloadAsync)}:{transfer.Username}:{transfer.Filename}"; - - if (!Locks.TryAdd(lockName, true)) - { - Log.Debug("Ignoring concurrent invocation; lock {LockName} already held", lockName); - return null; - } - - Log.Debug("Acquired lock {LockName}", lockName); - var cts = new CancellationTokenSource(); var syncRoot = new SemaphoreSlim(1, 1); @@ -230,6 +220,14 @@ namespace slskd.Transfers.Downloads } } + var lockName = $"{nameof(DownloadAsync)}:{transfer.Username}:{transfer.Filename}"; + + if (!Locks.TryAdd(lockName, true)) + { + Log.Debug("Ignoring concurrent invocation; lock {LockName} already held", lockName); + return null; + } + /* from this point forward, any exit from this method MUST result in an update to the Transfer record in the database that sets EndedAt and ensures that the State property includes the Completed flag @@ -240,6 +238,8 @@ namespace slskd.Transfers.Downloads */ try { + Log.Debug("Acquired lock {LockName}", lockName); + /* fetch an updated copy of the transfer record from the database; now that we are locked, we *should* have exclusive access to this record diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index bd167a57..89700eba 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -220,15 +220,6 @@ namespace slskd.Transfers.Uploads /// Thrown if the specified file can't be found on disk. public async Task UploadAsync(Transfer transfer) { - var lockName = $"{nameof(UploadAsync)}:{transfer.Username}:{transfer.Filename}"; - - if (!Locks.TryAdd(lockName, true)) - { - Log.Debug("Ignoring concurrent invocation; lock {LockName} already held", lockName); - return null; - } - - Log.Debug("Acquired lock {LockName}", lockName); var cts = new CancellationTokenSource(); var syncRoot = new SemaphoreSlim(1, 1); @@ -251,6 +242,14 @@ namespace slskd.Transfers.Uploads string localFilename = default; long localFileLength = default; + var lockName = $"{nameof(UploadAsync)}:{transfer.Username}:{transfer.Filename}"; + + if (!Locks.TryAdd(lockName, true)) + { + Log.Debug("Ignoring concurrent invocation; lock {LockName} already held", lockName); + return null; + } + /* from this point forward, any exit from this method MUST result in an update to the Transfer record in the database that sets EndedAt and ensures that the State property includes the Completed flag @@ -261,6 +260,8 @@ namespace slskd.Transfers.Uploads */ try { + Log.Debug("Acquired lock {LockName}", lockName); + /* fetch an updated copy of the transfer record from the database; now that we are locked, we *should* have exclusive access to this record -- cgit v1.2.3