Thanks to visit codestin.com
Credit goes to github.com

Skip to content

HttpClient async methods are blocking the first time #115301

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

Open
IS4Code opened this issue May 5, 2025 · 2 comments
Open

HttpClient async methods are blocking the first time #115301

IS4Code opened this issue May 5, 2025 · 2 comments
Labels
area-System.Net.Http untriaged New issue has not been triaged by the area owner

Comments

@IS4Code
Copy link

IS4Code commented May 5, 2025

Description

Task-returning methods on HttpClient are blocking for up to a few seconds when called for the first time, despite their documentation:

This operation doesn't block.

Reproduction Steps

var stopwatch = Stopwatch.StartNew();
Log("Start");
var task = TestHttpClient();
Log("Waiting for result...");
var result = task.Result;
Log("Length: " + result.Length);

async Task<string> TestHttpClient()
{
    using var client = new HttpClient();
    Log("Sending request...");
    using var response = await client.GetAsync("http://example.org/");
    Log("Response received.");
    return await response.Content.ReadAsStringAsync();
}

void Log(string str)
{
    Console.WriteLine($"{stopwatch.Elapsed.TotalSeconds:0.00} {str}");
}

Expected behavior

Waiting for result... should be displayed shortly after Sending request... when the asynchronous operation starts.

Actual behavior

0.00 Start
0.07 Sending request...
6.64 Waiting for result...
6.96 Response received.
6.96 Length: 1256

Regression?

Also tested on .NET 6 and .NET Framework 4.8 with the same results.

Known Workarounds

Using Task.Run or loading HttpClient.DefaultProxy are the only options to get rid of the blocking.

Configuration

.NET 9.0.100

Other information

Debugging reveals that the blocking operation happens in HttpConnectionPoolManager, when HttpClient.DefaultProxy is requested.

Retrieving HttpClient.DefaultProxy eventually leads to WinHttpGetIEProxyConfigForCurrentUser where the majority of the blocking comes from (even if no system-wide proxy is configured). This operation is executed non-asynchronously, hence it blocks the construction of the Task.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 5, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

@MihaZupan
Copy link
Member

MihaZupan commented May 5, 2025

eventually leads to WinHttpGetIEProxyConfigForCurrentUser

Do you see this on multiple machines, or does it only reproduce on one?

Do you mind also trying the workaround mentioned in #30025 (comment) to see if that helps?

6 seconds is long, so I could see this impacting calling code (e.g. UI that becomes unresponsive for several seconds at startup).
Given it's only a problem during the first access to the property, I think it'd be fine to hide it behind Task.Run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Net.Http untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants