Skip to content

Commit e8eee4b

Browse files
feat: fixing graceful cancellation when large files are still being hashed
1 parent 7736465 commit e8eee4b

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/Arius.Core/Features/Commands/Archive/ArchiveCommandHandler.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ internal async ValueTask<Result<ArchiveCommandResult>> Handle(HandlerContext han
101101
["UploadSmallFilesTask"] = CreateUploadSmallFilesTarArchiveTask(handlerContext, errorCancellationToken)
102102
};
103103

104+
foreach (var task in tasks.Values)
105+
{
106+
task.ContinueWith(t =>
107+
{
108+
if (t.IsFaulted && !errorCancellationTokenSource.IsCancellationRequested)
109+
{
110+
errorCancellationTokenSource.Cancel();
111+
}
112+
});
113+
}
114+
104115
try
105116
{
106117
await Task.WhenAll(tasks.Values);
@@ -235,24 +246,27 @@ private Task CreateIndexTask(HandlerContext handlerContext, CancellationToken ca
235246
await indexedFilesChannel.Writer.WriteAsync(FilePair.FromBinaryFileFileEntry(fp), cancellationToken);
236247
}
237248

238-
indexedFilesChannel.Writer.Complete();
239-
240249
logger.LogInformation("File indexing completed: found {FileCount} files in {LocalRoot}", totalLocalFiles, handlerContext.Request.LocalRoot);
241250
handlerContext.Request.ProgressReporter?.Report(new TaskProgressUpdate("Indexing files...", 100, $"Found {totalLocalFiles} files"));
242251
}
243252
catch (OperationCanceledException)
244253
{
254+
handlerContext.Request.ProgressReporter?.Report(new TaskProgressUpdate("Indexing files...", -1, $"Cancelled"));
245255
logger.LogDebug("File indexing cancelled");
246-
indexedFilesChannel.Writer.Complete();
247256
throw;
248257
}
249258
catch (Exception e) // TODO Align with approach of HashTask where we skip the file and log a warning instead of failing the entire task, write test Error_IndexTaskFails_ShouldSkipProblematicFileAndContinue
250259
{
251260
logger.LogError(e, "File indexing failed with exception");
252-
indexedFilesChannel.Writer.Complete();
261+
handlerContext.Request.ProgressReporter?.Report(new TaskProgressUpdate("Indexing files...", -1, $"Error"));
253262
throw;
254263
}
255-
}, cancellationToken);
264+
finally
265+
{
266+
indexedFilesChannel.Writer.Complete();
267+
logger.LogDebug("Index channel completed");
268+
}
269+
}); // No cancellation token passed to Task.Run, this allows proper catch/finally execution
256270

257271
private Task CreateHashTask(HandlerContext handlerContext, CancellationToken cancellationToken)
258272
{
@@ -301,6 +315,7 @@ private Task CreateHashTask(HandlerContext handlerContext, CancellationToken can
301315
}
302316
catch (OperationCanceledException)
303317
{
318+
handlerContext.Request.ProgressReporter?.Report(new FileProgressUpdate(filePair.FullName, -1, "Cancelled..."));
304319
throw;
305320
}
306321
catch (Exception e)
@@ -360,9 +375,9 @@ private Task CreateUploadSmallFilesTarArchiveTask(HandlerContext handlerContext,
360375
logger.LogError(e, "Small files TAR archive task failed");
361376
throw;
362377
}
363-
}, cancellationToken);
378+
}); // No cancellation token passed to Task.Run, this allows proper catch/finally execution
379+
364380

365-
366381
// --- HELPERS
367382

368383

0 commit comments

Comments
 (0)