dotnet / dotnet/aspnetcore

TestServer.CreateClient : the HttpClient created didn't support well the cancellation.

Open
#5,938 6 comments 1 reaction 0 assignees View on GitHub
affected-few area-hosting area-networking bug help wanted severity-minor
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

Hello,

I just found a nasty bug on HttpClient created from TestServer.
When I call a request which took 2 seconds if I cancel it after one second.
The client created by test server didn't throw an OperationCancelledException.

Here the code with real httpClient :

```
var httpClient = new HttpClient();
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1)))
{
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("http://localhost:4242/TimeoutTest/Action2Secs")
};
await httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead, cts.Token);
}
```

The same code with client created by the class TestServer.

```
var httpClient = new testServer.CreateClient();
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1)))
{
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("http://localhost:4242/TimeoutTest/Action2Secs")
};
await httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead, cts.Token);
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.