I'm running into an issue with .net 472 that tcp connections get stuck in Close_Wait state using a shared HttpClient.
This occurs when I execute several calls in concurrently then keep the HttpClient active by sending a get request every 10s.
Once i stop activity for 2 minutes then the connections close.
Upgrading to .net5.0+ solves this issue, but unfortunately I'm stuck on net472 for now so thats not an option ;(
Any guidance will be greatly appreciated.
Many thanks.
ServicePointManager.DefaultConnectionLimit = 10;
var client = new HttpClient();
//build up connections - mimic concurrent calls
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Creating concurrent socket.");
client.GetAsync(url);
}
//keep activity
for (int i = 0; i < 100000; i++)
{
Console.WriteLine("Keep activity.");
var response = await client.GetAsync(url);
await Task.Delay(10000);
}
Netstat -an shows
TCP 10.81.1.150:65392 CLOSE_WAIT
TCP 10.81.1.150:65393 CLOSE_WAIT
TCP 10.81.1.150:65394 CLOSE_WAIT
TCP 10.81.1.150:65395 CLOSE_WAIT
TCP 10.81.1.150:65396 ESTABLISHED
TCP 10.81.1.150:65397 CLOSE_WAIT