From e9e56182b88271d8bd335ae354ed8ea7adb69763 Mon Sep 17 00:00:00 2001 From: Allamagoosa <36551512+dmatlock171@users.noreply.github.com> Date: Thu, 20 Aug 2026 13:09:42 -0700 Subject: [PATCH] Stop abort and disk full from killing the queue loop CancelAsync read CurrentProcessable, which is lazy - _currentProcessable ??= Processes.Dequeue().Invoke() - so a book past its last step threw "Queue empty." on read, and the catch threw a second time interpolating CurrentProcessable.Name. Not a narrow race: every book waiting in the bad book dialog is in that state, because ProcessOneAsync reaches GetFailureActionAsync from its finally after the processable loop has drained. The faulted task then surfaced through the abort branch's Task.WhenAll, took QueueLoop out through its outer catch, and left the remaining books running with the progress bar still on screen. Test the _currentProcessable field instead, keep the property out of the catch message, and isolate each cancellation in CancelAllAsync so one book failing cannot abandon the rest of the list. Reading the field also stops the cancelling thread dequeuing from a non-thread-safe Queue while the book's own loop reads it. Also drops the doubled / left on CancelAllAsync by an earlier edit. --- .../ProcessQueue/ProcessBookViewModel.cs | 25 ++++++++++-- .../ProcessQueue/ProcessQueueViewModel.cs | 38 ++++++++++++------- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/Source/LibationUiBase/ProcessQueue/ProcessBookViewModel.cs b/Source/LibationUiBase/ProcessQueue/ProcessBookViewModel.cs index 11fd0756..b9002a26 100644 --- a/Source/LibationUiBase/ProcessQueue/ProcessBookViewModel.cs +++ b/Source/LibationUiBase/ProcessQueue/ProcessBookViewModel.cs @@ -265,16 +265,35 @@ public class ProcessBookViewModel : ReactiveObject return result; } + /// + /// Cancels this book's running step, if it has one. Safe to call on a book that has already + /// finished processing. + /// + /// + /// Reads the field rather than . + /// The property is lazy - _currentProcessable ??= Processes.Dequeue().Invoke() - so on a + /// book that has run its last step the field is null and is empty, and + /// reading it throws "Queue empty.". That is not a narrow race: every book waiting in the bad + /// book dialog is in exactly that state, because ProcessOneAsync reaches the dialog from + /// its finally after the processable loop has drained. Reading the field also avoids + /// dequeuing from a non-thread-safe on the cancelling thread while the + /// book's own loop is reading it. + /// public async Task CancelAsync() { + // Deliberately the field, not the property. See remarks. + var processable = _currentProcessable; + if (processable is not AudioDecodable audioDecodable) + return; + try { - if (CurrentProcessable is AudioDecodable audioDecodable) - await audioDecodable.CancelAsync(); + await audioDecodable.CancelAsync(); } catch (Exception ex) { - LogError($"{CurrentProcessable.Name}: Error while cancelling", ex); + // Not CurrentProcessable.Name - that would throw a second time, out of the catch. + LogError($"{audioDecodable.Name}: Error while cancelling", ex); } } diff --git a/Source/LibationUiBase/ProcessQueue/ProcessQueueViewModel.cs b/Source/LibationUiBase/ProcessQueue/ProcessQueueViewModel.cs index 757bff0a..6c04acf9 100644 --- a/Source/LibationUiBase/ProcessQueue/ProcessQueueViewModel.cs +++ b/Source/LibationUiBase/ProcessQueue/ProcessQueueViewModel.cs @@ -578,22 +578,21 @@ public class ProcessQueueViewModel : ReactiveObject public event EventHandler? ProcessStart; public event EventHandler? ProcessEnd; - /// - /// Stops the queue and cancels everything still in flight. - /// - /// - /// only prevents new work from starting. With - /// parallel downloads there may be several books already running, and those keep going - /// until they are cancelled individually. - /// - /// - /// A book that has already finished processing and so does not need cancelling - typically - /// the one whose result triggered the abort. - /// /// /// Clears the queue and cancels every book currently downloading. Also ends a pause on the daily /// download limit, which is why both UIs call this instead of manipulating the queue directly. /// + /// + /// only prevents new work from starting. With parallel + /// downloads there may be several books already running, and those keep going until they are + /// cancelled individually. + /// + /// Each cancellation is isolated. This runs from the queue loop's abort and disk full paths, so + /// a single book throwing here would surface through , take + /// QueueLoop out through its outer catch, and leave the remaining books running + /// unsupervised with the progress bar still on screen. + /// + /// /// /// A book calling this from its own completion path (abort, disk full). It has already finished /// and must not be asked to cancel itself. @@ -608,7 +607,20 @@ public class ProcessQueueViewModel : ReactiveObject // Snapshot before cancelling: Active is mutated as each book unwinds. var inFlight = Queue.GetActive().Where(b => b != except).ToArray(); - await Task.WhenAll(inFlight.Select(b => b.CancelAsync())); + await Task.WhenAll(inFlight.Select(CancelOneAsync)); + + static async Task CancelOneAsync(ProcessBookViewModel book) + { + try + { + await book.CancelAsync(); + } + catch (Exception ex) + { + // One book failing to cancel must not abandon the rest of the list. + Serilog.Log.Logger.Error(ex, "Error while cancelling {Book}", book.LibraryBook.LogFriendly()); + } + } } private async Task QueueLoop()