From 60520cdb16f8a01ed753ee1905053d9ec99a9950 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 27 Nov 2025 19:14:20 -0600 Subject: add missing interface method --- src/slskd/Transfers/Downloads/DownloadService.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src') diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 9d0a8ed9..bb4c5417 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -107,6 +107,20 @@ namespace slskd.Transfers.Downloads /// A value indicating whether the download was successfully cancelled. bool TryCancel(Guid id); + /// + /// Fails the download matching the specified with the specified , + /// and sets the final state accordingly. + /// + /// + /// This method is designed to be idempotent, meaning subsequent calls for a given transfer shouldn't change + /// the EndedAt or Exception properties if they have already been set. If the transfer State already includes + /// the terminal Completed flag, it is unchanged. + /// + /// The unique identifier for the download. + /// The exception that caused the failure. + /// A value indicating whether the download was successfully failed. + bool TryFail(Guid id, Exception exception); + /// /// Updates the specified . /// -- cgit v1.2.3 From cd5def0cc84adfa03ba50cddb07e5ed29108ca34 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 27 Nov 2025 19:14:30 -0600 Subject: add TryFail --- src/slskd/Transfers/Uploads/UploadService.cs | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 4b35bffb..4a3a44a5 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -125,6 +125,20 @@ namespace slskd.Transfers.Uploads /// A value indicating whether the upload was successfully cancelled. bool TryCancel(Guid id); + /// + /// Fails the upload matching the specified with the specified , + /// and sets the final state accordingly. + /// + /// + /// This method is designed to be idempotent, meaning subsequent calls for a given transfer shouldn't change + /// the EndedAt or Exception properties if they have already been set. If the transfer State already includes + /// the terminal Completed flag, it is unchanged. + /// + /// The unique identifier for the upload. + /// The exception that caused the failure. + /// A value indicating whether the upload was successfully failed. + bool TryFail(Guid id, Exception exception); + /// /// Synchronously updates the specified . /// @@ -898,6 +912,44 @@ namespace slskd.Transfers.Uploads return false; } + /// + /// Fails the upload matching the specified with the specified , + /// and sets the final state accordingly. + /// + /// + /// This method is designed to be idempotent, meaning subsequent calls for a given transfer shouldn't change + /// the EndedAt or Exception properties if they have already been set. If the transfer State already includes + /// the terminal Completed flag, it is unchanged. + /// + /// The unique identifier for the upload. + /// The exception that caused the failure. + /// A value indicating whether the upload was successfully failed. + public bool TryFail(Guid id, Exception exception) + { + var t = Find(t => t.Id == id); + + if (t is not null) + { + t.EndedAt ??= DateTime.UtcNow; + t.Exception ??= exception.Message; + + if (!t.State.HasFlag(TransferStates.Completed)) + { + t.State = TransferStates.Completed | exception switch + { + OperationCanceledException => TransferStates.Cancelled, + TimeoutException => TransferStates.TimedOut, + _ => TransferStates.Errored, + }; + } + + Update(t); + return true; + } + + return false; + } + /// /// Synchronously updates the specified . /// -- cgit v1.2.3 From b5d67774af02ad25579f6552c468d42e9a588135 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 27 Nov 2025 19:35:28 -0600 Subject: add private TryFail overload --- src/slskd/Transfers/Uploads/UploadService.cs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 4a3a44a5..3e85bc8c 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -925,22 +925,30 @@ namespace slskd.Transfers.Uploads /// The exception that caused the failure. /// A value indicating whether the upload was successfully failed. public bool TryFail(Guid id, Exception exception) + { + var state = exception switch + { + NotFoundException => TransferStates.Aborted, + OperationCanceledException => TransferStates.Cancelled, + TimeoutException => TransferStates.TimedOut, + _ => TransferStates.Errored, + }; + + return TryFail(id, exception.Message, state); + } + + private bool TryFail(Guid id, string exception, TransferStates state) { var t = Find(t => t.Id == id); if (t is not null) { t.EndedAt ??= DateTime.UtcNow; - t.Exception ??= exception.Message; + t.Exception ??= exception; if (!t.State.HasFlag(TransferStates.Completed)) { - t.State = TransferStates.Completed | exception switch - { - OperationCanceledException => TransferStates.Cancelled, - TimeoutException => TransferStates.TimedOut, - _ => TransferStates.Errored, - }; + t.State = TransferStates.Completed | state; } Update(t); -- cgit v1.2.3 From 8e3ddab64d24434b15ab6150b6f0118152878849 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 27 Nov 2025 19:36:04 -0600 Subject: use TryFail when enqueueing --- src/slskd/Transfers/Uploads/UploadService.cs | 35 ++++------------------------ 1 file changed, 4 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 3e85bc8c..bd771501 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -665,23 +665,9 @@ namespace slskd.Transfers.Uploads */ Log.Error(task.Exception, "Task for upload of {Filename} to {Username} did not complete successfully: {Error}", filename, username, task.Exception.Message); - try - { - var transfer = Find(t => t.Id == id); - - if (transfer is not null) - { - transfer.EndedAt ??= DateTime.UtcNow; - transfer.Exception ??= task.Exception.Message; - transfer.State = TransferStates.Completed | transfer.State; - - Update(transfer); - } - } - catch (Exception ex) + if (!TryFail(id, task.Exception)) { - Log.Error(ex, "Failed to clean up transfer {Id} after failed execution: {Message}", id, ex.Message); - throw; + Log.Error(task.Exception, "Failed to clean up transfer {Id} after failed execution: {Message}", id, task.Exception.Message); } }); @@ -691,22 +677,9 @@ namespace slskd.Transfers.Uploads { Log.Error(ex, "Failed to enqueue upload of {Filename} to {Username}: {Message}", filename, username, ex.Message); - try - { - var transfer = Find(t => t.Id == id); - - if (transfer is not null) - { - transfer.EndedAt ??= DateTime.UtcNow; - transfer.Exception ??= ex.Message; - transfer.State = TransferStates.Completed | transfer.State; - - Update(transfer); - } - } - catch (Exception innerEx) + if (!TryFail(id, exception: ex)) { - Log.Error(innerEx, "Failed to clean up transfer {Id} after failed enqueue: {Message}", id, innerEx.Message); + Log.Error(ex, "Failed to clean up transfer {Id} after failed execution: {Message}", id, ex.Message); throw; } -- cgit v1.2.3 From c3c58b0dcc025c493df7e3631af6dd1d64705fbd Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 27 Nov 2025 19:36:38 -0600 Subject: use TryFail when failing an upload --- src/slskd/Transfers/Uploads/UploadService.cs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index bd771501..800e6395 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -432,28 +432,22 @@ namespace slskd.Transfers.Uploads return transfer; } - catch (NotFoundException) + catch (NotFoundException ex) { - transfer.EndedAt = DateTime.Now; - transfer.Exception = "File could not be found"; - transfer.State = TransferStates.Completed | TransferStates.Aborted; + Log.Error(ex, "Upload of {Filename} to user {Username} failed: {Message}", transfer.Filename, transfer.Username, ex.Message); + + TryFail(transfer.Id, "File could not be found", TransferStates.Completed | TransferStates.Aborted); + // todo: broadcast SynchronizedUpdate(transfer, semaphore: syncRoot, cancellationToken: CancellationToken.None); throw; } catch (Exception ex) when (ex is OperationCanceledException || ex is TimeoutException) { - transfer.EndedAt = DateTime.UtcNow; - transfer.Exception = ex.Message; - transfer.State = TransferStates.Completed; + Log.Error(ex, "Upload of {Filename} to user {Username} failed: {Message}", transfer.Filename, transfer.Username, ex.Message); - transfer.State |= ex switch - { - OperationCanceledException => TransferStates.Cancelled, - TimeoutException => TransferStates.TimedOut, - _ => TransferStates.Errored, - }; + TryFail(transfer.Id, exception: ex); // todo: broadcast SynchronizedUpdate(transfer, semaphore: syncRoot, cancellationToken: CancellationToken.None); @@ -464,9 +458,7 @@ namespace slskd.Transfers.Uploads { Log.Error(ex, "Upload of {Filename} to user {Username} failed: {Message}", transfer.Filename, transfer.Username, ex.Message); - transfer.EndedAt = DateTime.UtcNow; - transfer.Exception = ex.Message; - transfer.State = TransferStates.Completed | TransferStates.Errored; + TryFail(transfer.Id, exception: ex); // todo: broadcast SynchronizedUpdate(transfer, semaphore: syncRoot, cancellationToken: CancellationToken.None); -- cgit v1.2.3 From 4af80023a1611eb68b6c66b777e8791507a157ca Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 28 Nov 2025 15:53:52 -0600 Subject: fail gracefully on exceptions --- src/slskd/Transfers/Downloads/DownloadService.cs | 34 ++++++++++------ src/slskd/Transfers/Uploads/UploadService.cs | 50 ++++++++++++++---------- 2 files changed, 50 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index bb4c5417..d906688a 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -799,26 +799,34 @@ namespace slskd.Transfers.Downloads { var t = Find(t => t.Id == id); - if (t is not null) + if (t is null) { - t.EndedAt ??= DateTime.UtcNow; - t.Exception ??= exception.Message; + return false; + } + + t.EndedAt ??= DateTime.UtcNow; + t.Exception ??= exception.Message; - if (!t.State.HasFlag(TransferStates.Completed)) + if (!t.State.HasFlag(TransferStates.Completed)) + { + t.State = TransferStates.Completed | exception switch { - t.State = TransferStates.Completed | exception switch - { - OperationCanceledException => TransferStates.Cancelled, - TimeoutException => TransferStates.TimedOut, - _ => TransferStates.Errored, - }; - } + OperationCanceledException => TransferStates.Cancelled, + TimeoutException => TransferStates.TimedOut, + _ => TransferStates.Errored, + }; + } + try + { Update(t); return true; } - - return false; + catch (Exception ex) + { + Log.Error(ex, "Failed to update database: {Message}", ex.Message); + return false; + } } /// diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 800e6395..5d0749b2 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -902,27 +902,6 @@ namespace slskd.Transfers.Uploads return TryFail(id, exception.Message, state); } - private bool TryFail(Guid id, string exception, TransferStates state) - { - var t = Find(t => t.Id == id); - - if (t is not null) - { - t.EndedAt ??= DateTime.UtcNow; - t.Exception ??= exception; - - if (!t.State.HasFlag(TransferStates.Completed)) - { - t.State = TransferStates.Completed | state; - } - - Update(t); - return true; - } - - return false; - } - /// /// Synchronously updates the specified . /// @@ -935,6 +914,35 @@ namespace slskd.Transfers.Uploads context.SaveChanges(); } + private bool TryFail(Guid id, string exception, TransferStates state) + { + var t = Find(t => t.Id == id); + + if (t is null) + { + return false; + } + + t.EndedAt ??= DateTime.UtcNow; + t.Exception ??= exception; + + if (!t.State.HasFlag(TransferStates.Completed)) + { + t.State = TransferStates.Completed | state; + } + + try + { + Update(t); + return true; + } + catch (Exception ex) + { + Log.Error(ex, "Failed to update database: {Message}", ex.Message); + return false; + } + } + /// /// Resolves a remote file's location and size from shares and potentially disk. /// -- cgit v1.2.3 From 5e0ad381e1c4ca35883245e9d172382e0ec58ff0 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 28 Nov 2025 17:17:05 -0600 Subject: don't throw in a finally --- src/slskd/Transfers/Uploads/UploadService.cs | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 5d0749b2..fa2e6aca 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -494,7 +494,6 @@ namespace slskd.Transfers.Uploads catch (Exception ex) { Log.Error(ex, "Failed to finalize upload of {Filename} to {Username}: {Message}", transfer.Filename, transfer.Username, ex.Message); - throw; } } } -- cgit v1.2.3 From b0efa281523b825c1272c83f8ced3b71d07f3ab2 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 28 Nov 2025 17:17:16 -0600 Subject: tweak message --- src/slskd/Transfers/Uploads/UploadService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index fa2e6aca..c541d4f0 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -670,7 +670,7 @@ namespace slskd.Transfers.Uploads if (!TryFail(id, exception: ex)) { - Log.Error(ex, "Failed to clean up transfer {Id} after failed execution: {Message}", id, ex.Message); + Log.Error(ex, "Failed to clean up transfer {Id} after failed enqueue: {Message}", id, ex.Message); throw; } -- cgit v1.2.3