From 06939533b71112ba6a8eb3b95f6b892a5c9505a4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 4 Oct 2026 02:55:02 -0500 Subject: get rid of GetGroupInfo, return group info in Forecast --- src/slskd/Application.cs | 7 ++--- src/slskd/Transfers/Uploads/UploadQueue.cs | 46 ++++++------------------------ 2 files changed, 12 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 8de4fe53..f8e92bc2 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1749,7 +1749,7 @@ namespace slskd // note: the following uses cached user data to determine group, so if the user's data // isn't cached they may get a forecast based on the wrong group. this is a hot path though, // and we don't want to incur the massive penalties that would caching data for each request. - var forecastedPosition = Transfers.Uploads.Queue.ForecastPosition(username); + var (groupName, totalSlots, freeSlots, forecastedPosition) = Transfers.Uploads.Queue.ForecastPosition(username); Log.Debug("Sending search response with {Count} files to {Username} for query '{Query}'", results.Count(), username, query.SearchText); @@ -1957,12 +1957,11 @@ namespace slskd // we are deliberately skipping it here; if the username is watched // leech detection works and they get accurate info, if not, they won't var groupName = await Users.GetOrFetchGroupAsync(username); - var group = Transfers.Uploads.Queue.GetGroupInfo(groupName); // forecast the position at which this user would enter the queue if they were to request // a file at this moment. this will be zero if a slot is available and the transfer would // begin immediately - var forecastedPosition = Transfers.Uploads.Queue.ForecastPosition(username); + var (group, totalSlots, freeSlots, forecastedPosition) = Transfers.Uploads.Queue.ForecastPosition(username); // if i get a user's info to determine whether i want to download files from them, // i want to know how many slots they have, which gives me an idea of how fast their @@ -1971,7 +1970,7 @@ namespace slskd // revisited 3 years later: why was it important to leave this comment?? var info = new UserInfo( description: Options.Soulseek.Description, - uploadSlots: group.Slots, + uploadSlots: totalSlots, queueLength: forecastedPosition, hasFreeUploadSlot: forecastedPosition == 0, picture: pictureBytes); diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index b7db1c42..9fc48497 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -81,13 +81,6 @@ namespace slskd.Transfers.Uploads /// The filename to enqueue. void Enqueue(string username, string filename); - /// - /// Gets information about the specified . - /// - /// The name of the group. - /// The group information. - UploadGroup GetGroupInfo(string groupName); - /// /// Computes the estimated queue position of the specified for the specified . /// @@ -110,7 +103,7 @@ namespace slskd.Transfers.Uploads /// /// The estimated queue position if the user were to enqueue a file, or zero if the transfer could start immediately. /// - int ForecastPosition(string username); + (string Group, int TotalSlots, int FreeSlots, int Position) ForecastPosition(string username); } /// @@ -293,30 +286,6 @@ namespace slskd.Transfers.Uploads } } - /// - /// Gets information about the specified . - /// - /// The name of the group. - /// The group information. - public UploadGroup GetGroupInfo(string groupName) - { - if (Groups.TryGetValue(groupName, out var group)) - { - SyncRoot.Wait(); - - try - { - return group with { UsedSlots = [.. group.UsedSlots] }; - } - finally - { - SyncRoot.Release(); - } - } - - throw new NotFoundException($"A group with the name {groupName} could not be found"); - } - /// /// Computes the estimated queue position of the specified for the specified . /// @@ -422,16 +391,19 @@ namespace slskd.Transfers.Uploads /// /// The estimated queue position if the user were to enqueue a file, or zero if the transfer could start immediately. /// - public int ForecastPosition(string username) + public (string Group, int TotalSlots, int FreeSlots, int Position) ForecastPosition(string username) { var groupName = Users.GetGroup(username); // if there's a slot available, the user will enter the queue at position 0 (will start immediately) - if (Groups.TryGetValue(groupName, out var groupRecord) && groupRecord.SlotAvailable) + if (Groups.TryGetValue(groupName, out var groupRecord) && groupRecord.HasAvailableSlot) { - return 0; + return (string.Empty, 0, 0, 0); } + var totalSlots = groupRecord.Slots; + var freeSlots = totalSlots - groupRecord.UsedSlots.Count; + // the Uploads dictionary is keyed by username; gather all of the users that belong to the same group as the requested user // a user's group can change either by user changing the config or updating counts (for leech detection); resist the urge to cache this var uploadsForGroup = UploadDictionary.Where(kvp => Users.GetGroup(kvp.Key) == groupName); @@ -445,12 +417,12 @@ namespace slskd.Transfers.Uploads // rotation (worst case), the user's start position will be equal to the number of users downloading or waiting, + 1. if (groupRecord.Strategy == QueueStrategy.RoundRobin) { - return uploadsForGroup.Count() + 1; + return (groupName, totalSlots, freeSlots, uploadsForGroup.Count() + 1); } // for FIFO queues, the user will enter the queue at the very back. return the total number of uploads in progress and // enqueued, + 1. - return uploadsForGroup.Sum(kvp => kvp.Value.Count) + 1; + return (groupName, totalSlots, freeSlots, uploadsForGroup.Sum(kvp => kvp.Value.Count) + 1); } private void Configure(Options options) -- cgit v1.2.3 From 7425c704ef3e1f21efe41fbe5de6dc963f22c1ee Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 4 Oct 2026 02:55:35 -0500 Subject: back to HasAvailableSlot --- src/slskd/Transfers/Types/UploadGroup.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Transfers/Types/UploadGroup.cs b/src/slskd/Transfers/Types/UploadGroup.cs index e416dd00..0b2b617d 100644 --- a/src/slskd/Transfers/Types/UploadGroup.cs +++ b/src/slskd/Transfers/Types/UploadGroup.cs @@ -38,7 +38,7 @@ namespace slskd.Transfers { public string Name { get; init; } public int Priority { get; set; } - public bool SlotAvailable => UsedSlots.Count < Slots; + public bool HasAvailableSlot => UsedSlots.Count < Slots; public int Slots { get; set; } public QueueStrategy Strategy { get; set; } public HashSet<(string Username, string Filename)> UsedSlots { get; set; } -- cgit v1.2.3 From 1ad32bdda6f0dd234b3675ae525e77200a1be29a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 4 Oct 2026 14:38:02 -0500 Subject: run continuations async --- src/slskd/Transfers/Types/Upload.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Transfers/Types/Upload.cs b/src/slskd/Transfers/Types/Upload.cs index 2e9dc8ef..1e05a88a 100644 --- a/src/slskd/Transfers/Types/Upload.cs +++ b/src/slskd/Transfers/Types/Upload.cs @@ -42,7 +42,7 @@ namespace slskd.Transfers public string Group { get; set; } public DateTime? Ready { get; set; } = null; public DateTime? Started { get; set; } = null; - public TaskCompletionSource TaskCompletionSource { get; set; } = new TaskCompletionSource(); + public TaskCompletionSource TaskCompletionSource { get; set; } = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); public string Username { get; set; } } } \ No newline at end of file -- cgit v1.2.3 From 05d82deee08aa1788ce1d0910648cf6f942a7ba3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 4 Oct 2026 16:34:40 -0500 Subject: consider GlobalSlots when forecasting --- src/slskd/Transfers/Uploads/UploadQueue.cs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index 9fc48497..42d70194 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -99,6 +99,11 @@ namespace slskd.Transfers.Uploads /// Computes the estimated queue position of the specified if they were to enqueue a file, /// or zero if the transfer could start immediately. /// + /// + /// The returned position is relative to the user's group only. Higher priority groups are not factored in + /// because of the amount of data that would need to be processed to compute a number, and how variable it + /// would be due to the interplay of slot availability, number of higher priority users waiting, etc. + /// /// The username for which to estimate. /// /// The estimated queue position if the user were to enqueue a file, or zero if the transfer could start immediately. @@ -387,6 +392,11 @@ namespace slskd.Transfers.Uploads /// Computes the estimated queue position of the specified if they were to enqueue a file, /// or zero if the transfer could start immediately. /// + /// + /// The returned position is relative to the user's group only. Higher priority groups are not factored in + /// because of the amount of data that would need to be processed to compute a number, and how variable it + /// would be due to the interplay of slot availability, number of higher priority users waiting, etc. + /// /// The username for which to estimate. /// /// The estimated queue position if the user were to enqueue a file, or zero if the transfer could start immediately. @@ -395,24 +405,24 @@ namespace slskd.Transfers.Uploads { var groupName = Users.GetGroup(username); - // if there's a slot available, the user will enter the queue at position 0 (will start immediately) - if (Groups.TryGetValue(groupName, out var groupRecord) && groupRecord.HasAvailableSlot) + if (!Groups.TryGetValue(groupName, out var groupRecord)) { - return (string.Empty, 0, 0, 0); + throw new SlskdException($"Upload group {groupName} doesn't have an entry in upload dictionary. Please report this on GitHub: {Program.IssuesUrl}"); } - var totalSlots = groupRecord.Slots; - var freeSlots = totalSlots - groupRecord.UsedSlots.Count; + var totalSlots = Math.Min(GlobalSlots, groupRecord.Slots); + var freeSlots = Math.Max(0, totalSlots - groupRecord.UsedSlots.Count); + + // if there's a slot available, the user will enter the queue at position 0 (will start immediately) + if (freeSlots > 0) + { + return (groupName, totalSlots, freeSlots, 0); + } // the Uploads dictionary is keyed by username; gather all of the users that belong to the same group as the requested user // a user's group can change either by user changing the config or updating counts (for leech detection); resist the urge to cache this var uploadsForGroup = UploadDictionary.Where(kvp => Users.GetGroup(kvp.Key) == groupName); - if (groupRecord is null) - { - throw new SlskdException($"Upload group {groupName} doesn't have an entry in upload dictionary. Please report this on GitHub: {Program.IssuesUrl}"); - } - // assuming that the queue will be processed in a true round-robin fashion and that the user will be the last in the // rotation (worst case), the user's start position will be equal to the number of users downloading or waiting, + 1. if (groupRecord.Strategy == QueueStrategy.RoundRobin) -- cgit v1.2.3 From 67999bda3007baf735b5a14926c1b6e4c22e4a55 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 4 Oct 2026 17:45:30 -0500 Subject: fail loudly if groups desync --- src/slskd/Transfers/Uploads/UploadQueue.cs | 6 +++++- .../Transfers/Uploads/UploadQueueTests.cs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index 42d70194..ccd1f5f2 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -306,7 +306,11 @@ namespace slskd.Transfers.Uploads public int EstimatePosition(string username, string filename) { var groupName = Users.GetGroup(username); - var groupRecord = Groups.GetValueOrDefault(groupName); + + if (!Groups.TryGetValue(groupName, out var groupRecord)) + { + throw new SlskdException($"Upload group {groupName} doesn't have an entry in upload dictionary. Please report this on GitHub: {Program.IssuesUrl}"); + } // the Uploads dictionary is keyed by username; gather all of the users that belong to the same group as the requested user // a user's group can change either by user changing the config or updating counts (for leech detection); resist the urge to cache this diff --git a/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs b/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs index c063d82f..fbf9754f 100644 --- a/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs +++ b/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs @@ -714,6 +714,23 @@ namespace slskd.Tests.Unit.Transfers.Uploads Assert.IsType(ex); } + [Theory, AutoData] + public void Throws_SlskdException_If_Group_Does_Not_Exist(string username, string filename, string groupName) + { + var (queue, mocks) = GetFixture(); + + mocks.UserService.Setup(m => m.GetGroup(username)).Returns(groupName); + + var uploads = new ConcurrentDictionary>(); + uploads.TryAdd(username, CreateUploads(username, count: 1)); + + queue.SetProperty("UploadDictionary", uploads); + + var ex = Record.Exception(() => queue.EstimatePosition(username, "file0")); + + Assert.IsType(ex); + } + [Theory, AutoData] public void RoundRobin_Returns_Local_Position_If_User_Is_Alone_In_Group(string username) { -- cgit v1.2.3 From b38d9f01862b4fc27874bf99d4b24b53a6577bd0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 5 Oct 2026 09:11:44 -0500 Subject: make forecast zero-based to match estimation (and to make it more suitable for gauging queue depth) --- src/slskd/Transfers/Uploads/UploadQueue.cs | 16 ++++++------- .../Transfers/Uploads/UploadQueueTests.cs | 26 +++++++++++++--------- 2 files changed, 23 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index ccd1f5f2..cb071d25 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -97,7 +97,7 @@ namespace slskd.Transfers.Uploads /// /// Computes the estimated queue position of the specified if they were to enqueue a file, - /// or zero if the transfer could start immediately. + /// along with the slot counts for the user's group. /// /// /// The returned position is relative to the user's group only. Higher priority groups are not factored in @@ -106,7 +106,7 @@ namespace slskd.Transfers.Uploads /// /// The username for which to estimate. /// - /// The estimated queue position if the user were to enqueue a file, or zero if the transfer could start immediately. + /// The user's group, the group's total and free slots, and the estimated position if the user were to enqueue a file. /// (string Group, int TotalSlots, int FreeSlots, int Position) ForecastPosition(string username); } @@ -394,7 +394,7 @@ namespace slskd.Transfers.Uploads /// /// Computes the estimated queue position of the specified if they were to enqueue a file, - /// or zero if the transfer could start immediately. + /// along with the slot counts for the user's group. /// /// /// The returned position is relative to the user's group only. Higher priority groups are not factored in @@ -403,7 +403,7 @@ namespace slskd.Transfers.Uploads /// /// The username for which to estimate. /// - /// The estimated queue position if the user were to enqueue a file, or zero if the transfer could start immediately. + /// The user's group, the group's total and free slots, and the estimated position if the user were to enqueue a file. /// public (string Group, int TotalSlots, int FreeSlots, int Position) ForecastPosition(string username) { @@ -428,15 +428,15 @@ namespace slskd.Transfers.Uploads var uploadsForGroup = UploadDictionary.Where(kvp => Users.GetGroup(kvp.Key) == groupName); // assuming that the queue will be processed in a true round-robin fashion and that the user will be the last in the - // rotation (worst case), the user's start position will be equal to the number of users downloading or waiting, + 1. + // rotation (worst case), the user's start position will be equal to the number of users downloading or waiting. if (groupRecord.Strategy == QueueStrategy.RoundRobin) { - return (groupName, totalSlots, freeSlots, uploadsForGroup.Count() + 1); + return (groupName, totalSlots, freeSlots, uploadsForGroup.Count()); } // for FIFO queues, the user will enter the queue at the very back. return the total number of uploads in progress and - // enqueued, + 1. - return (groupName, totalSlots, freeSlots, uploadsForGroup.Sum(kvp => kvp.Value.Count) + 1); + // enqueued. + return (groupName, totalSlots, freeSlots, uploadsForGroup.Sum(kvp => kvp.Value.Count)); } private void Configure(Options options) diff --git a/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs b/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs index fbf9754f..c6fadc82 100644 --- a/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs +++ b/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs @@ -930,18 +930,22 @@ namespace slskd.Tests.Unit.Transfers.Uploads [Theory] [InlineAutoData(QueueStrategy.RoundRobin)] [InlineAutoData(QueueStrategy.FirstInFirstOut)] - public void Returns_One_If_No_Slot_Is_Available_And_Queue_Is_Empty(QueueStrategy strategy, string username) + public void Returns_Zero_Without_Free_Slot_If_No_Slot_Is_Available_And_Queue_Is_Empty(QueueStrategy strategy, string username) { var (queue, _) = GetFixture(); SetStrategy(queue, strategy); SetSlotAvailable(queue, false); - Assert.Equal(1, queue.ForecastPosition(username).Position); + var (_, _, freeSlots, position) = queue.ForecastPosition(username); + + // nothing is ahead, but the transfer can't start immediately; callers use FreeSlots to tell the difference + Assert.Equal(0, position); + Assert.Equal(0, freeSlots); } [Theory, AutoData] - public void RoundRobin_Returns_Number_Of_Users_In_Group_Plus_One(string username, string other1, string other2) + public void RoundRobin_Returns_Number_Of_Users_In_Group(string username, string other1, string other2) { var (queue, _) = GetFixture(); @@ -955,7 +959,7 @@ namespace slskd.Tests.Unit.Transfers.Uploads queue.SetProperty("UploadDictionary", uploads); // worst case; the new file is last in the rotation. file counts don't matter - Assert.Equal(3, queue.ForecastPosition(username).Position); + Assert.Equal(2, queue.ForecastPosition(username).Position); } [Theory, AutoData] @@ -972,7 +976,7 @@ namespace slskd.Tests.Unit.Transfers.Uploads queue.SetProperty("UploadDictionary", uploads); - Assert.Equal(3, queue.ForecastPosition(username).Position); + Assert.Equal(2, queue.ForecastPosition(username).Position); } [Theory, AutoData] @@ -991,11 +995,11 @@ namespace slskd.Tests.Unit.Transfers.Uploads queue.SetProperty("UploadDictionary", uploads); - Assert.Equal(2, queue.ForecastPosition(username).Position); + Assert.Equal(1, queue.ForecastPosition(username).Position); } [Theory, AutoData] - public void FirstInFirstOut_Returns_Number_Of_Uploads_In_Group_Plus_One(string username, string other1, string other2) + public void FirstInFirstOut_Returns_Number_Of_Uploads_In_Group(string username, string other1, string other2) { var (queue, _) = GetFixture(); @@ -1010,7 +1014,7 @@ namespace slskd.Tests.Unit.Transfers.Uploads queue.SetProperty("UploadDictionary", uploads); // the new file goes to the back of the queue, behind every upload in the group, including the user's own - Assert.Equal(2 + 3 + 4 + 1, queue.ForecastPosition(username).Position); + Assert.Equal(2 + 3 + 4, queue.ForecastPosition(username).Position); } [Theory, AutoData] @@ -1029,7 +1033,7 @@ namespace slskd.Tests.Unit.Transfers.Uploads queue.SetProperty("UploadDictionary", uploads); - Assert.Equal(3, queue.ForecastPosition(username).Position); + Assert.Equal(2, queue.ForecastPosition(username).Position); } [Theory, AutoData] @@ -1048,7 +1052,7 @@ namespace slskd.Tests.Unit.Transfers.Uploads queue.SetProperty("UploadDictionary", uploads); - Assert.Equal(4, queue.ForecastPosition(username).Position); + Assert.Equal(3, queue.ForecastPosition(username).Position); } [Theory] @@ -1137,7 +1141,7 @@ namespace slskd.Tests.Unit.Transfers.Uploads Assert.Equal(1, totalSlots); Assert.Equal(0, freeSlots); - Assert.Equal(1, position); + Assert.Equal(0, position); } [Theory, AutoData] -- cgit v1.2.3 From 85d99db5f390af5df211101fa83ce256bb16ba95 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 5 Oct 2026 09:12:04 -0500 Subject: run continuations async --- src/slskd/Common/TokenBucket.cs | 4 ++-- src/slskd/Transfers/Downloads/DownloadService.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Common/TokenBucket.cs b/src/slskd/Common/TokenBucket.cs index 11d64b88..440f25ae 100644 --- a/src/slskd/Common/TokenBucket.cs +++ b/src/slskd/Common/TokenBucket.cs @@ -92,7 +92,7 @@ namespace slskd /// public class TokenBucket : ITokenBucket, IDisposable { - private TaskCompletionSource waitForReset = new(); + private TaskCompletionSource waitForReset = new(TaskCreationOptions.RunContinuationsAsynchronously); /// /// Initializes a new instance of the class. @@ -243,6 +243,6 @@ namespace slskd } private void Reset() - => Interlocked.Exchange(ref waitForReset, new TaskCompletionSource()).SetResult(true); + => Interlocked.Exchange(ref waitForReset, new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously)).SetResult(true); } } diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 30f3c9b1..cb9423bc 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -509,7 +509,7 @@ namespace slskd.Transfers.Downloads add this to the dictionary before inserting the record, so we are guaranteed to have it in the right place once the transfer hits the UI */ - var enqueuedTcs = new TaskCompletionSource(); + var enqueuedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); // satisfies condition #3; CancellationTokenSource set cancelled by the user (via API call) var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); -- cgit v1.2.3 From 11767ab706c308f4a0d24738693a0f6a68271cb1 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 5 Oct 2026 09:12:40 -0500 Subject: use slots instead of inferring from position --- src/slskd/Application.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index f8e92bc2..c34cd316 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1757,7 +1757,7 @@ namespace slskd Client.Username, token, uploadSpeed: State.CurrentValue.User.Statistics.AverageSpeed, - hasFreeUploadSlot: forecastedPosition == 0, + hasFreeUploadSlot: freeSlots > 0, queueLength: forecastedPosition, fileList: results); } @@ -1972,7 +1972,7 @@ namespace slskd description: Options.Soulseek.Description, uploadSlots: totalSlots, queueLength: forecastedPosition, - hasFreeUploadSlot: forecastedPosition == 0, + hasFreeUploadSlot: freeSlots > 0, picture: pictureBytes); return info; -- cgit v1.2.3 From 11d65d328fa2e27cd856f6f11d1ce4a84cc973b3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 5 Oct 2026 09:49:19 -0500 Subject: add backstop for potential scenario where an upload is removed from the queue without its TCS being completed, causing a hang --- src/slskd/Transfers/Uploads/UploadQueue.cs | 7 +++ .../Transfers/Uploads/UploadQueueTests.cs | 54 ++++++++++++++++++++++ 2 files changed, 61 insertions(+) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index cb071d25..d10ce38b 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -233,6 +233,13 @@ namespace slskd.Transfers.Uploads } list.Remove(upload); + + if (!upload.TaskCompletionSource.Task.IsCompleted) + { + Log.Warning("Upload {File} for {User} was removed without being completed, so it has been cancelled. Please report this on GitHub: {IssuesUrl}", Path.GetFileName(upload.Filename), upload.Username, Program.IssuesUrl); + upload.TaskCompletionSource.TrySetCanceled(); + } + Log.Debug("Complete: {File} for {User} at {Time}", Path.GetFileName(upload.Filename), upload.Username, upload.Enqueued); // ensure the slot is returned to the group from which it was acquired the group may have been removed during the diff --git a/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs b/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs index c6fadc82..f8b2f183 100644 --- a/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs +++ b/tests/slskd.Tests.Unit/Transfers/Uploads/UploadQueueTests.cs @@ -550,6 +550,60 @@ namespace slskd.Tests.Unit.Transfers.Uploads Assert.Contains((other, otherFilename), groups[Application.DefaultGroup].UsedSlots); } + [Theory, AutoData] + public async Task Cancels_Task_Of_Upload_That_Is_Waiting_For_A_Slot(string username, string filename, string other, string otherFilename) + { + var (queue, _) = GetFixture(); + + var groups = queue.GetProperty>("Groups"); + groups[Application.DefaultGroup].Slots = 1; + + queue.Enqueue(other, otherFilename); + await queue.AwaitStartAsync(other, otherFilename); + + // no slot available; the caller is left waiting on this task + queue.Enqueue(username, filename); + var task = queue.AwaitStartAsync(username, filename); + + Assert.False(task.IsCompleted); + + queue.Complete(username, filename); + + // continuations run asynchronously, but the task's status is set synchronously + Assert.True(task.IsCanceled); + } + + [Theory, AutoData] + public void Cancels_Task_Of_Upload_That_Was_Never_Ready(string username, string filename) + { + var (queue, _) = GetFixture(); + + queue.Enqueue(username, filename); + + var uploads = queue.GetProperty>>("UploadDictionary"); + var task = uploads[username][0].TaskCompletionSource.Task; + + Assert.False(task.IsCompleted); + + queue.Complete(username, filename); + + Assert.True(task.IsCanceled); + } + + [Theory, AutoData] + public async Task Does_Not_Cancel_Task_Of_Upload_That_Started(string username, string filename) + { + var (queue, _) = GetFixture(); + + queue.Enqueue(username, filename); + var task = queue.AwaitStartAsync(username, filename); + await task; + + queue.Complete(username, filename); + + Assert.True(task.IsCompletedSuccessfully); + } + [Theory, AutoData] public async Task Releasing_A_Slot_Starts_The_Next_Ready_Upload(string username, string filename, string other, string otherFilename) { -- cgit v1.2.3 From b0ca0504c8c13bbe73401ac00d16e13d5a4fda70 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 5 Oct 2026 10:07:24 -0500 Subject: make cancellation a debug message for now --- src/slskd/Transfers/Uploads/UploadQueue.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadQueue.cs b/src/slskd/Transfers/Uploads/UploadQueue.cs index d10ce38b..5839fa73 100644 --- a/src/slskd/Transfers/Uploads/UploadQueue.cs +++ b/src/slskd/Transfers/Uploads/UploadQueue.cs @@ -236,7 +236,7 @@ namespace slskd.Transfers.Uploads if (!upload.TaskCompletionSource.Task.IsCompleted) { - Log.Warning("Upload {File} for {User} was removed without being completed, so it has been cancelled. Please report this on GitHub: {IssuesUrl}", Path.GetFileName(upload.Filename), upload.Username, Program.IssuesUrl); + Log.Debug("Upload {File} for {User} was removed without being completed, so it has been cancelled", Path.GetFileName(upload.Filename)); upload.TaskCompletionSource.TrySetCanceled(); } -- cgit v1.2.3