aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-11-14 18:38:56 -0600
committerGitHub <noreply@github.com>2026-11-14 18:38:56 -0600
commita9a12166305b846538b4e5076e1f01daf6070d49 (patch)
treed0f05c9aab9a7f56bdaec97b5788b5b43efb0238
parent1f4754f40005af6e1110bd00c7a7705a7203287a (diff)
parent34cc2546e3dae7d402e2c017f69e05756914ce2d (diff)
downloadslskd-a9a12166305b846538b4e5076e1f01daf6070d49.tar.gz
Merge pull request #1923 from slskd/object-disposed
Ensure cancellation tokens are disposed when a download fails
-rw-r--r--src/slskd/Transfers/Downloads/DownloadService.cs5
-rw-r--r--tests/slskd.Tests.Unit/Transfers/Downloads/DownloadService/EnqueueAsyncTests.cs50
2 files changed, 55 insertions, 0 deletions
diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs
index 43a9d17c..32744fb2 100644
--- a/src/slskd/Transfers/Downloads/DownloadService.cs
+++ b/src/slskd/Transfers/Downloads/DownloadService.cs
@@ -1100,6 +1100,11 @@ namespace slskd.Transfers.Downloads
Log.Error(ex, "Failed to update database: {Message}", ex.Message);
return false;
}
+ finally
+ {
+ CancellationTokens.TryRemove(id, out var cts);
+ cts?.TryDispose();
+ }
}
/// <summary>
diff --git a/tests/slskd.Tests.Unit/Transfers/Downloads/DownloadService/EnqueueAsyncTests.cs b/tests/slskd.Tests.Unit/Transfers/Downloads/DownloadService/EnqueueAsyncTests.cs
index 4d5f47ea..11edf035 100644
--- a/tests/slskd.Tests.Unit/Transfers/Downloads/DownloadService/EnqueueAsyncTests.cs
+++ b/tests/slskd.Tests.Unit/Transfers/Downloads/DownloadService/EnqueueAsyncTests.cs
@@ -780,6 +780,56 @@ public partial class DownloadServiceTests
await Task.Delay(100);
}
+ [Fact]
+ public async Task Cancellation_Token_Is_Released_When_DownloadAsync_Throws_Before_Starting()
+ {
+ using var fixture = new Fixture();
+
+ fixture.ClientDownloads = () =>
+ {
+ // the first call is EnqueueAsync() checking for duplicates (no record yet); once the record exists, DownloadAsync()
+ // makes the same check, and reporting a duplicate makes it throw DuplicateTransferException before its try block
+ var record = fixture.Service.Find(t => t.Filename == Filename);
+ return record is null ? [] : [SoulseekTransfer(Username, Filename, TransferStates.Queued | TransferStates.Remotely)];
+ };
+
+ var transfer = await fixture.EnqueueAsync();
+
+ var record = await fixture.WaitForCompletedAsync(transfer.Id);
+ Assert.Equal(TransferStates.Completed | TransferStates.Errored, record.State);
+ Assert.Empty(fixture.Downloads);
+
+ await Task.Delay(100);
+ Assert.False(fixture.CancellationTokens.ContainsKey(transfer.Id));
+ }
+
+ [Fact]
+ public async Task Cancellation_Token_Is_Released_When_Batch_Is_Cancelled_Mid_Loop()
+ {
+ using var fixture = new Fixture();
+ using var cts = new CancellationTokenSource();
+ var inCriticalSection = false;
+
+ fixture.OnCreateDbContext = () => inCriticalSection = true;
+ fixture.ClientDownloads = () =>
+ {
+ if (inCriticalSection)
+ {
+ cts.Cancel();
+ }
+
+ return [];
+ };
+
+ await Record.ExceptionAsync(() => fixture.Service.EnqueueAsync(Username, [(Filename, Size), (OtherFilename, Size)], cancellationToken: cts.Token));
+
+ var record = await fixture.WaitForCompletedAsync(Assert.Single(fixture.Records()).Id);
+ Assert.Equal(TransferStates.Completed | TransferStates.Cancelled, record.State);
+
+ await Task.Delay(100);
+ Assert.False(fixture.CancellationTokens.ContainsKey(record.Id));
+ }
+
private const string OtherFilename = "@alice\\Music\\track2.flac";
private static List<(string Filename, long Size)> Files(int count)