Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All files are deleted if download task is cancelled #125

Open
barbarian1803 opened this issue Aug 6, 2024 · 0 comments
Open

All files are deleted if download task is cancelled #125

barbarian1803 opened this issue Aug 6, 2024 · 0 comments
Assignees
Labels

Comments

@barbarian1803
Copy link

barbarian1803 commented Aug 6, 2024

Describe the bug
I have multiple task to download file and use Task.WaitAll to wait them to finished. All the download task share the same cancellation token source. The issue appear when I cancel the token and all files are deleted even though some of the task is finished already. Something must be wrong in the flush method. Below is the test case code:

using Microsoft.Extensions.Logging;
using OctaneEngine;
using OctaneEngineCore;
using Serilog;
using ILogger = Serilog.ILogger;

namespace TestOctaneEngineMain;

public static class Program
{
    private static readonly CancellationTokenSource Cts = new();
    public static void Main(string[] args)
    {
        Console.CancelKeyPress += delegate
        {
            Cts.Cancel();
            Console.WriteLine("Cancelling task");
        };
        
        Console.WriteLine("Hello, World!");
        
        ILogger log = new LoggerConfiguration()
            .Enrich.FromLogContext()
            .MinimumLevel.Verbose()
            .WriteTo.Console()
            .CreateLogger();
        
        var loggerFactory = LoggerFactory.Create(logging =>
        {
            logging.AddSerilog(log);
        });
        
        var config = new OctaneConfiguration
        {
            Parts            = 8,
            LowMemoryMode    = false,
            BufferSize       = 8196,
            ShowProgress     = false,
            NumRetries       = 3,
            BytesPerSecond   = 1,
            UseProxy         = false
        };
        var localFile = "/local/path/bigfilename.txt";
        var bigFileUrl = "http://url/bigfilename.txt";

        var smallLocalFile = "/local/path/smallfilename.txt";
        var smallFileUrl = "http://url/smallfilename.txt";

        IEngine? bigFileDownloader = EngineBuilder.Build(loggerFactory, config);
        
        bigFileDownloader.SetDoneCallback( status => Console.WriteLine(status ? "Done callback big file called" : "Error Done callback big file called"));
        
        IEngine? smallFileDownloader = EngineBuilder.Build(loggerFactory, config);
        
        smallFileDownloader.SetDoneCallback(status => Console.WriteLine(status ? "Done callback small file called" : "Error Done callback small file called"));
        
        var pt = new PauseTokenSource();

        var tasks     = new List<Task>();

        tasks.Add(
            bigFileDownloader.DownloadFile(bigFileUrl, localFile, pt, Cts)
                                .ContinueWith(_ => Console.WriteLine("Done download big file continuation")));
        tasks.Add(
            smallFileDownloader.DownloadFile(smallFileUrl, smallLocalFile, pt, Cts)
                                .ContinueWith(_ => Console.WriteLine("Done download small file continuation")));

        Task.WaitAll(tasks.ToArray());
        Console.WriteLine("Download all files finished!");
    }
}

To Reproduce
Just run the code above, change the URL to some big and small file. Wait for the small file to finish and press ctrl+c. Both file will be deleted in local folder.

Expected behavior
Finished file should be there and not deleted.

Additional context
This bug is tested in version 8.0.3 and 8.0.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants