0

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
Spuddy
  • 1
  • Does the behavior change if you use Dispose response? – Crowcoder Dec 17 '21 at 13:27
  • You could try using HttpClientFactory. That should take of it all for you – sr28 Dec 17 '21 at 14:56
  • @Crowcoder I've tried disposing everything, request, response, client with static messagehandler – Spuddy Dec 18 '21 at 15:26
  • @sr28 I think HttpClientFactory was only introduced in .net core which unfortunately isn't available to me just yet. – Spuddy Dec 18 '21 at 15:27
  • HttpClientFactory isn't core only as discussed here: https://stackoverflow.com/questions/51498896/use-httpclientfactory-from-net-4-6-2 – sr28 Dec 21 '21 at 08:56

0 Answers0