Skip to content
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

HttpWebRequest creates many connections #59207

Closed
MhdTlb opened this issue Sep 16, 2021 · 8 comments
Closed

HttpWebRequest creates many connections #59207

MhdTlb opened this issue Sep 16, 2021 · 8 comments
Milestone

Comments

@MhdTlb
Copy link

MhdTlb commented Sep 16, 2021

Steps to reproduce

  1. Create a Class Library project and make target frameworks net462 and netstandard2.0.
  2. Add a new class to the project with following code:
        public class Httpwebrequesttest
	{
              public static void Run()
              {
                  for (int i = 0; i < 1000; i++)
                  {
                      var client = WebRequest.CreateHttp("http://localhost:80");
                      using (var res = client.GetResponse())
                      {
      
                      }
                      Console.WriteLine($"{i}");
                  }
                  Console.WriteLine($"press enter then exit");
                  Console.ReadLine();
              }
          }
  1. Add Console project to the solution and make target framework as netcoreapp3.1
  2. Add the Class library project as dependency to the Console App and call the Httpwebrequesttest.Run method from the Console App
  3. Open a CMD and run this command netstat -na | find "80"

Expected behavior

I was expecting to see few TCP connections but there are hundreds of them with the TIME_WAIT status:
... TCP [::1]:63056 [::1]:80 TIME_WAIT ...

If I change the target framework of the Console Application to net462 and I run the netstat command, i can see few TCP connections with status ESTABLISHED

Notes

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Net untriaged New issue has not been triaged by the area owner labels Sep 16, 2021
@ghost
Copy link

ghost commented Sep 16, 2021

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

Issue Details

Steps to reproduce

  1. Create a Class Library project and make target frameworks net462 and netstandard2.0.
  2. Add a new class to the project with following code:
        public class Httpwebrequesttest
	{
              public static void Run()
              {
                  for (int i = 0; i < 1000; i++)
                  {
                      var client = WebRequest.CreateHttp("http://localhost:80");
                      using (var res = client.GetResponse())
                      {
      
                      }
                      Console.WriteLine($"{i}");
                  }
                  Console.WriteLine($"press enter then exit");
                  Console.ReadLine();
              }
          }
  1. Add Console project to the solution and make target framework as netcoreapp3.1
  2. Add the Class library project as dependency to the Console App and call the Httpwebrequesttest.Run method from the Console App
  3. Open a CMD and run this command netstat -na | find "80"

Expected behavior

I was expecting to see few TCP connections but there are hundreds of them with the TIME_WAIT status:
... TCP [::1]:63056 [::1]:80 TIME_WAIT ...

If I change the target framework of the Console Application to net462 and I run the netstat command, i can see few TCP connections with status ESTABLISHED

Notes

Author: MhdTlb
Assignees: -
Labels:

area-System.Net, untriaged

Milestone: -

@ManickaP
Copy link
Member

ManickaP commented Sep 16, 2021

Should be fixed in dotnet/corefx#41462 which went into 5.0. If you switch to .NET 5.0 do you still see this problem?

In .NET Core WebRequest is based on HttpClient, so that's why it has such a different behavior from .NET Framework. We do recommend to switch from WebRequest to HttpClient if you plan to target .NET Core and .NET 5.0+ runtimes.

@MhdTlb
Copy link
Author

MhdTlb commented Sep 16, 2021

@ManickaP .Net5.0 does not have the issue

@ManickaP
Copy link
Member

@MhdTlb thanks for the confirmation.

How much is the issue impacting you? And would you be able to upgrade to .NET 5.0 (or 6.0 which is RC1 now)?

@MhdTlb MhdTlb changed the title HttpWebRequest create many connections HttpWebRequest creates many connections Sep 17, 2021
@karelz
Copy link
Member

karelz commented Sep 21, 2021

Triage: Addressed in 5.0+. Closing.

@karelz karelz closed this as completed Sep 21, 2021
@karelz karelz added this to the 7.0.0 milestone Sep 21, 2021
@MhdTlb
Copy link
Author

MhdTlb commented Sep 30, 2021

@ManickaP Unfortunately, we can not upgrade to .NET 5.0 right now. Is there a workaround to keep using 3.1?

@karelz
Copy link
Member

karelz commented Sep 30, 2021

@MhdTlb there is no workaround to our best knowledge. Why cannot you upgrade to .NET 5.0 now? What limits you?
If you need LTS, did you consider .NET 6.0? (it is now in RC1 which has go-live license, with GA following soon in November)

@ManickaP
Copy link
Member

ManickaP commented Oct 1, 2021

@MhdTlb you'd need use HttpClient and cache it yourself.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 3, 2021
@karelz karelz removed the untriaged New issue has not been triaged by the area owner label Oct 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants