Skip to content

WinHttpResponseStream.ReadAsync() ignores cancellation token #17331

Closed
dotnet/corefx
#8693

Description

@davidsh

(Split from #17315 which contained multiple related bugs)

The following code demonstrates the problem. This test server will return response headers quickly. But will take 15 seconds to return response body. We cancel the ReadAsync() after 1 second. But the ReadAsync() task doesn't cancel until the 15 seconds has elapsed.

[Fact]
public async Task ResponseStreamRead_CancelUsingToken_TaskCanceledQuickly()
{
    var stopwatch = new Stopwatch();

    stopwatch.Start();
    using (var client = new HttpClient())
    using (HttpResponseMessage response =
        await client.GetAsync(s_fastHeadersSlowBodyServer, HttpCompletionOption.ResponseHeadersRead))
    {
        stopwatch.Stop();
        _output.WriteLine("Time to get headers: {0}", stopwatch.Elapsed.ToString());
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);

        var cts = new CancellationTokenSource();

        Stream stream = await response.Content.ReadAsStreamAsync();
        byte[] buffer = new byte[ResponseBodyLength];

        Task ignore = Task.Delay(new TimeSpan(0,0,1)).ContinueWith(_ =>
        {
            _output.WriteLine("Calling cts.Cancel() at: {0}", stopwatch.Elapsed.ToString());
            cts.Cancel();
        });

        stopwatch.Restart();
        await Assert.ThrowsAsync<TaskCanceledException>(
            () => stream.ReadAsync(buffer, 0, buffer.Length, cts.Token));
        stopwatch.Stop();
        _output.WriteLine("ReadAsync() completed at: {0}", stopwatch.Elapsed.ToString());
        Assert.True(stopwatch.Elapsed < new TimeSpan(0,0,3), "Elapsed time should be short");
    }
}

Activity

  1. self-assigned this
    on May 18, 2016
  2. oxfordemma commented on Aug 22, 2016

    @oxfordemma

    @davidsh is it possible that this issue is also present in .Net Framework 4.6.2 and not fixed there (not sure how similar the different code bases are)?
    I tried this out in two clients (UWP and WPF). UWP works after pulling in the latest packages, but the WPF app (targeting 4.6.2) is still unable to cancel ReadAsync calls.

    Pulling in these Net Core libs on top of Net Framework did fix the issue (but introduced a lot of unrelated side effects).

  3. davidsh commented on Aug 22, 2016

    @davidsh
    ContributorAuthor

    The codebases between .NET Framework 4.6.x is totally different than CoreFx.

    So, it is possible that this bug exists there.

  4. thewavelength commented on Oct 19, 2016

    @thewavelength

    @davidsh If I'm not totally wrong, I can reproduce this behavior in 1.0.0-preview2-003131 - is this wanted?

  5. davidsh commented on Oct 20, 2016

    @davidsh
    ContributorAuthor

    @thewavelength What is "1.0.0-preview2-003131"? This bug was fixed in .NET Core 1.0 RTM release.

    If you can repro it, please attach a repro solution so that we can investigate. If so, please open a new issue for tracking.

  6. transferred this issue fromdotnet/corefxon Jan 31, 2020
  7. added this to the 1.0.0-rtm milestone on Jan 31, 2020
  8. ghost locked as resolved and limited conversation to collaborators on Dec 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions