runtime: [NetFX compat]: HttpWebRequest.GetRequestStreamAsync() hangs in desktop

When creating multiple HttpWebRequest instances and calling GetRequestStreamAsync() it hangs in desktop. There is a test class that does this and when running it in desktop it just hangs forever because all the tests create an HttpWebRequest instance and call GetRequestStreamAsync().GetAwaiter().GetResult()source code the project test is System.Net.Requests.Tests

I did this on a simple Console App targeting desktop:

static void Main(string[] args)
{
     for(int i = 0; i < 100; i++)
     {
          var request = GetRequest();
          var stream = request.GetRequestStreamAsync().GetAwaiter().GetResult();
          Console.WriteLine(stream.CanWrite);
          stream.Dispose();
          //request.Abort()
     }
}

public static HttpWebRequest GetRequest()
{
     HttpWebRequest request = HttpWebRequest.CreateHttp(new Uri("<myserverurl>"));
     request.Method = "POST";
     return request;
}

And it will also hang. If I just enable request.Abort() when I’m done using the request it will stop hanging. I don’t know if that is a good practice, but could be a workaround in the tests for desktop.

If we change the ServicePointManager.DefaultConnectionLimit to be a 100 it would also fix it but I don’t think that would be a good solution.

After talking with @stephentoub he explained there was a difference in behavior in Core:

My guess here is that disposing of the request stream doesn’t release the underlying connection that HttpWebREquest opens to the server, and that the only way to do that would be to then get the response and dispose of that. That’s why changing the connection limit allowed you to get further, because the system wasn’t throttling you to the two connections you already opened. On core, HttpWebRequest is built on top of HttpClient, and it’s not going to actually open the connection until you’ve written all your data.

That is why the test don’t hang in Core. There is a related issue to this https://github.com/dotnet/corefx/issues/11873

cc: @davidfowl @CIPop @tarekgh

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (14 by maintainers)

Most upvoted comments

@tarekgh the tests are skipped and linked to this issue.