Skip to content

DownloadFileTaskAsync not throwing an OperationCanceledException when CancellationToken is canceled #203

Closed

Description

@Otiel

When passing a CancellationToken to DownloadFileTaskAsync, I'm expecting the method to throw an OperationCanceledException if the cancellation has been requested and the download is cancelled.

This would allow me to manage the cancellation properly later in my code.

This does not seem to be the case today: this test should not fail:

[Test]
public void RequestingCancellation_Throws_OperationCanceledException()
{
    // Arrange
    const string fileUrl = "https://www.examplefile.com/file-download/202"; // 1 GB txt file
    var cts = new CancellationTokenSource();
    cts.CancelAfter(500);

    Assert.ThrowsAsync<OperationCanceledException>(async () =>
    {
        // Act
        await _sut.DownloadFileTaskAsync(fileUrl, cts.Token);
    });
}

Activity

  1. bezzad commented on Feb 8, 2026

    @bezzad
    Owner

    Hi @Otiel — sorry for the late reply, I’ve been offline for a while due to recent issues in Iran.

    In this library, cancellation is reported via the OnDownloadFileCompleted completion event (download complete callback). As noted in the documentation, errors and cancellations are surfaced there rather than by throwing from DownloadFileTaskAsync().

    So when you cancel the CancellationToken, the download completes and the completion event will indicate cancellation (e.g. e.Cancelled == true). Please handle cancellation in that event handler.

  2. self-assigned this
    on Feb 8, 2026
  3. Otiel commented on Feb 8, 2026

    @Otiel
    Author

    Hi @bezzad, got it, thanks for the reply.

    Good luck with the situation in your country, I hope you can stay safe. ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions