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<T> while the book's own loop reads it.

Also drops the doubled <summary>/<param> left on CancelAllAsync by an earlier edit.
This commit is contained in:
Allamagoosa
2026-08-20 13:09:42 -07:00
parent 63778135c6
commit e9e56182b8
2 changed files with 47 additions and 16 deletions

View File

@@ -578,22 +578,21 @@ public class ProcessQueueViewModel : ReactiveObject
public event EventHandler<ProcessBookViewModel>? ProcessStart;
public event EventHandler<ProcessBookViewModel>? ProcessEnd;
/// <summary>
/// Stops the queue and cancels everything still in flight.
/// </summary>
/// <remarks>
/// <see cref="TrackedQueue{T}.ClearQueue"/> 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.
/// </remarks>
/// <param name="except">
/// A book that has already finished processing and so does not need cancelling - typically
/// the one whose result triggered the abort.
/// </param>
/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// <see cref="TrackedQueue{T}.ClearQueue"/> 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.
/// <para>
/// 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 <see cref="Task.WhenAll(Task[])"/>, take
/// <c>QueueLoop</c> out through its outer catch, and leave the remaining books running
/// unsupervised with the progress bar still on screen.
/// </para>
/// </remarks>
/// <param name="except">
/// 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()