Downloader icon indicating copy to clipboard operation
Downloader copied to clipboard

File is downloaded from the beginning after resuming

Open tmk907 opened this issue 3 years ago • 0 comments

Sometimes, when you pause and resume the download, file is downloaded from the beginning.

This is my code:

Task.Run(() => downloader.DownloadFileTaskAsync(url, filePath)); //start
await Task.Delay(5000);
var package = downloader.Package;
downloader.CancelAsync(); //pause
using var stream = await downloader.DownloadFileTaskAsync(package); //resume

StartDownload() from 'resume' starts executing before 'start' StartDownload() finishes.

When I resume download downloader.DownloadFileTaskAsync(pack) executes InitialDownloader(). New CancellationTokenSource is assigned to _globalCancellationTokenSource and IsCancelled is false.

Then this code from 'start' StartDownload() is executed and sets Package.Chunks = null https://github.com/bezzad/Downloader/blob/42ce0a87fe8b99c5baf9ffae30d6336090266bd6/src/Downloader/DownloadService.cs#L162-L176

And when resume method continues to Package.Chunks ??= _chunkHub.ChunkFile(Package.TotalFileSize, Options.ChunkCount, Options.RangeLow); Package.Chunks are null which means that the download starts from the beginning.

Possible fix to this is use SemaphoreSlim in StartDownload method, so that resume will not start until first call finishes.

tmk907 avatar Aug 07 '22 09:08 tmk907