5

I have a strange issue I'm trying to triage having to do with the new HttpClient on .NET Core 2.1. From this article here (https://blogs.msdn.microsoft.com/dotnet/2018/04/11/announcing-net-core-2-1-preview-2/) I know that the HttpClient has been completely re-written to use a different low level library for handling HTTP requests. I'm wondering if anyone has seen any issues with the new implementation.

What I'm seeing is a strange case where my application (.NET Core 2.1) which sends a POST request to some API periodically (every 10 seconds) a few of times every 15 min it will throw an exception with the error: An error occurred while sending the request.; The server returned an invalid or unrecognized response.

No other details are available, it's just an exception when I make a call like this:

using (var res = await _httpClient.PostAsync(uriBuilder.Uri, new StringContent(serializedRequestBody, Encoding.UTF8, "application/json")))
            {
               //Do something here
            }

The exception caught is a System.Net.Http.HttpRequestException and it has some inner exception with the above error message.

So as I mentioned this does NOT happen all time, it happens seemingly at random, or at least I can not discern any particular pattern. All I can say is these POST requests are made once every 10 seconds 24/7 and anywhere between 5% and 10% of the POST requests fail with the above exception.

So used tcdump and piped it into wireshark to examine the requests to see what's actually happening when the requests fail and what i see is the following:

On a good POST I see: my app sends the request to server, server sends response back, my app sends ACK to server and server responds with FIN,ACK. Done. Good Stuff.

On POST which gets the above exception I see the following: my app sends the request to server, and almost immediately after (like a few milliseconds after) my application sends FIN, ACK to server.

This seems consistent with what I see in my application logs, which show that the request duration is 0 before the exception is thrown.

So what it looks like to me is, my application sends the request and then immediately after closes the connection for some reason. However, I don't understand why this happens. I tried comparing the raw HTTP requests (good POST vs bad POST) to see any differences and I can not see any difference.

One last thing to mention, is that I ONLY see this in applications running on .NET Core 2.1. When I run my application on .NET 2.0 I do not see this problem. Also when I use the same library (where the HTTP call is being made) in the .NET 4.5.1 application (I use multi-targeting to compile the library targeting .net standard and net451) I also do NOT see this problem. So it seems to affect only .NET Core 2.1

Any ideas of where I can go from here? Is there something else I should look for ? How would someone go about trying to triage this type of issue ?

enter image description here

[EDIT] I added a screenshot of the wireshark output which shows the last POST request the server never does not respond before the client sends FIN,ACK

[EDIT] @Svek Pointed out something in the comments about the sequence of ACKs. I think there maybe something here, because (in the screenshot) after the very last POST there is a FIN, ACK and it shows Ack=7187, so I look back I see the previous FIN,ACK had sequence=7186. Now, I'm by far not an expect in TCP or networking so I maybe saying something completely dumb, but does that mean that the last FIN,ACK (which comes from my host to server) is essentially my host FIN,ACK'ing the previous FIN,ACK (from server to my host) and essentially closing the connection.

So since the next POST is made to the same host:port, using the same connection and yet the connection is closed (via that last FIN,ACK) that's why I never get a response back?

DKhanaf
  • 365
  • 1
  • 4
  • 18
  • This seems like something you should report as a bug, probably on github – Stilgar Oct 11 '18 at 19:17
  • Just a guess. .Net Core does not support (most) `ServicePointManager` settings. `Expect100Continue` will be in the headers. This can raise an exception (sometimes), depending on how the server answers to the POST request with this header set. – Jimi Oct 11 '18 at 22:29
  • @Jimi but the server doesn't send anything back at all before my application sends FIN,ACK. Also if that was the case then why do some POST requests to the same exact API and same exact host work while other don't. – DKhanaf Oct 11 '18 at 23:46
  • Based on your screenshot, the `ACK Seq=4754` (timestamped at `114`) then `FIN, ACK Ack=4754` (timestamped at `144`) --- are you sure this is not their server closing a stale connection after a period of timeout (ex. `144-114=30`)? – Svek Oct 12 '18 at 06:18
  • @Svek I was trying to answer your comment in the comments but it was too long so I edited the question. You may be onto something interesting here. – DKhanaf Oct 12 '18 at 20:50
  • Since using `HttpClient` in `.Net Core` with `.Net Framework` in mind & muscle memory can lead to some *misunderstandings*, these are some readings that could be useful: [Improper Instantiation - MSDN](https://learn.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation/), [HttpClient usage - ASP.NET Monsters Blog](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/), [HttpClient single instance - SO Question](https://stackoverflow.com/questions/37928543/httpclient-single-instance-with-different-authentication-headers). – Jimi Oct 13 '18 at 07:47
  • @Jimi btw I was able to fix the issue i.e. prevent the connections being closed and exceptions thrown by : AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false); which essentially turns off the new HttpClient implementation and reverts back to libcurl. So this seems quite definitive that there is an issue with the new implementation. – DKhanaf Oct 13 '18 at 23:03
  • [HttpClient with .Net Core 2.1 hangs](https://stackoverflow.com/questions/50749042/httpclient-with-net-core-2-1-hangs). HttpClient & Co. are undergoing a number of adjustments. It's a work in progress. As I wrote before, when you expect the same behaviour as in .Net 4.7.1+, prepare for disappointment. Maybe, keep this question open and link what you find out in your proceedings. Others may find this useful. – Jimi Oct 13 '18 at 23:14
  • Hi, did you solve the issue at the end? I suspect I'm having the same issue...thanks. – babaislove Jan 07 '20 at 13:31

0 Answers0