aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-10-04 17:59:22 -0500
committerJP Dillingham <jp@dillingham.me>2025-10-04 17:59:22 -0500
commit845f6406c079d082e4ea9683b3c1966698967c90 (patch)
treee58eb6b2b7fb0094d26a76d415324373b695b84a
parentc8e43f739b967470900072530ca7138c3b18b106 (diff)
downloadslskd-845f6406c079d082e4ea9683b3c1966698967c90.tar.gz
delay lock acquisition
-rw-r--r--src/slskd/Transfers/Downloads/DownloadService.cs20
-rw-r--r--src/slskd/Transfers/Uploads/UploadService.cs19
2 files changed, 20 insertions, 19 deletions
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
/// <exception cref="DuplicateTransferException">Thrown if a download matching the username and filename is already tracked by Soulseek.NET.</exception>
public async Task<Transfer> 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
/// <exception cref="NotFoundException">Thrown if the specified file can't be found on disk.</exception>
public async Task<Transfer> 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