Skip to content

NET 10: Breaking change in behaviour when reading HTTP response content #121810

@ign3u5

Description

@ign3u5

Description

HttpContent.LoadIntoBufferAsync now uses LimitArrayPoolWriteStream (here) where it used to use LimitMemoryStream (here) in .NET 9.

LimitArrayPoolWriteStream.WriteAsync ignores the CancellationToken and simply returns Task.CompletedTask (here) where LimitMemoryStream.WriteAsync calls the base functionality (here) which returns a cancelled task (here).

This change in behaviour means that where, in .NET 9, a TaskCanceledException would be thrown; in .NET 10 the completed task is returned successfully.

Reproduction Steps

var httpClient = new HttpClient(new SomeHandler());

var cts = new CancellationTokenSource();

cts.Cancel();

_ = await httpClient.GetAsync("https://example.com", cts.Token);

public class SomeHandler : HttpMessageHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        return Task.FromResult(new HttpResponseMessage()
        {
            Content = new StringContent("Hello from SomeHandler")
        });
    }
}

Expected behavior

Behaviour in .NET 9 - TaskCanceledException is thrown.

Actual behavior

Behaviour in .NET 10 - No exception is thrown.

Regression?

This demonstrates a regression / breaking change between the versions.

Known Workarounds

No response

Configuration

  • Used .NET 9 and 10

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions