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()