runtime icon indicating copy to clipboard operation
runtime copied to clipboard

The server returned an invalid or unrecognized response" error when using System.Net.Http.HttpClient

Open Petermarcu opened this issue 9 years ago • 30 comments

Getting this bug into the right place: https://github.com/dotnet/core/issues/321

@corinas Hello,

I am facing a problem when making concurrent requests to a web api when using dotnet core. Sometimes i'm getting the below error; the error is not happening all the time (usually it happens once or twice in 600 concurrent calls).

System.Net.Http.WinHttpException: The server returned an invalid or unrecognized response  
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

Here's the test method I use for testing:

    [Fact]
    public void PostToStardogParallelTaskWithSendAsync()
    {
        int x = 600;
        Task[] allTasks = new Task[x];

        using (var httpClient = new HttpClient())
        {
            var byteArray = Encoding.ASCII.GetBytes($"user:password");                

            for (int k = 0; k < 1; k++)
            {
                for (int i = 0; i < x; i++)
                {
                    System.Diagnostics.Debug.WriteLine($"Starting tsk {i}...");
                    allTasks[i] = Task.Factory.StartNew(async (ii) =>
                    {
                        var d = DateTime.Now;
                        var query = $"INSERT DATA {{ <urn:subj> <urn:pred> <urn:obj>. }}";
                        var content = new StringContent(query, Encoding.UTF8, "application/sparql-update");

                        HttpRequestMessage rm = new HttpRequestMessage(HttpMethod.Post, "MyStardogWebApiUrl");
                        rm.Headers.TryAddWithoutValidation("accept", "text/boolean");
                        rm.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                        rm.Content = content;                            
                        try
                        {
                            var response = await httpClient.SendAsync(rm);
                        }
                        catch (Exception e)
                        {
                            System.Diagnostics.Debug.WriteLine($"\nThe following exception have been thrown: {e}");                            
                        }

                        System.Diagnostics.Debug.WriteLine($"Task {ii} ended in: {(DateTime.Now - d).TotalMilliseconds}.");

                    }, i).Unwrap();
                }
                try
                {
                    Task.WaitAll(allTasks);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"nThe following exception have been thrown: {e}");                       
                }
            }
        }
    }

When this error occurs, the response looks like this: {StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: , Headers:{}}

Calling the same method from a testing project that uses dotnet framework 4.5 does not cause any issue.

Could you please advise? Thanks!

Corina

Petermarcu avatar Jan 05 '17 18:01 Petermarcu

cc: @karelz

The repro code posted above is a just a piece of the total repro. What server is this talking to? In order to repro the problem, we need a complete solution. The error being reported is about parsing the server response. So, it is important to use the same server to debug this issue.

davidsh avatar Mar 27 '17 20:03 davidsh

@corinas can you please provide more details here? Thanks!

karelz avatar Mar 27 '17 23:03 karelz

We've also encountered this issue. We only seem to get the unrecognized response when making HEAD requests to a server which serves a redirect.

System.AggregateException: One or more errors occurred. (An error occurred while sending the request.) 
---> System.Net.Http.HttpRequestException: An error occurred while sending the request. 
---> System.Net.Http.WinHttpException: The server returned an invalid or unrecognized response
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()
   --- End of inner exception stack trace ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at ConsoleApp3.Program.Main() in d:\projects\ConsoleApp3\ConsoleApp3\Program.cs:line 23

---> (Inner Exception #0) System.Net.Http.HttpRequestException: An error occurred while sending the request. 
---> System.Net.Http.WinHttpException: The server returned an invalid or unrecognized response
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()
   --- End of inner exception stack trace ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()<---

We use WebListener from "Microsoft.AspNetCore.Server.WebListener": "1.1.1",. For the purpose of reproducing the we've created the following simple web application: Program.cs

namespace WebApplication1
{
	public class Program
	{
		public static void Main(string[] args)
		{
			var host = new WebHostBuilder()
				.UseWebListener()
				.UseContentRoot(Directory.GetCurrentDirectory())
				.UseUrls("http://test.com:5000")
				.UseStartup<Startup>()
				.Build();

			host.Run();
		}
	}
}

Startup.cs

namespace WebApplication1
{
	public class Startup
	{
		public void Configure(IApplicationBuilder app)
		{
			app.Run(context =>
			{
				if (context.Request.GetDisplayUrl().EndsWith("redirect.html"))
				{
					context.Response.StatusCode = 200;
					return Task.CompletedTask;
				}

				context.Response.Redirect("/redirect.html");

				return Task.CompletedTask;
			});
		}
	}
}

And created a console application which makes the requests. Program.cs

namespace ConsoleApp3
{
	class Program
	{
		static void Main()
		{
			for (var i = 0; i < 10000; i++)
			{
				using (var httpClient = new HttpClient())
				{
					var request = new HttpRequestMessage(HttpMethod.Head, new Uri("http://test.com:5000"));
					var response = httpClient.SendAsync(request).Result;
				}
			}
		}
	}
}

JosVerburg avatar Mar 29 '17 14:03 JosVerburg

@JosVerburg how is your repro and callstack related to the original one here? If it is not confirmed it is dupe, please file a new issue to keep things separate, thank you!

karelz avatar Mar 29 '17 15:03 karelz

@JosVerburg Please put together a complete, simple, repro with a Visual Studio 2015 or 2017 solution. You can simply zip up the solution and post it here. That would be the best way for us to investigate.

davidsh avatar Mar 29 '17 17:03 davidsh

And the reason for a complete repro is that I tried your sample code above and was unable to put together a complete solution that would launch WebListener. Latest version of ASP.NET Core uses a different API for the Startup.Configure:

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

davidsh avatar Mar 29 '17 18:03 davidsh

@davidsh I've setup a repo with the solution at https://github.com/JosVerburg/ReproduceUnrecognizedResponse.

@karelz To me it seems like the same issue, if you still can't reproduce it with the example to confirm I'll make a separate issue.

Additional information: We've also inspected the response and request using fiddler Request:

HEAD http://test.com:5000/ HTTP/1.1
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Host: test.com:5000

Response:

HTTP/1.1 302 Found
Transfer-Encoding: chunked
Location: /redirect.html
Server: Microsoft-HTTPAPI/2.0
Date: Thu, 30 Mar 2017 07:45:19 GMT

0

JosVerburg avatar Mar 30 '17 07:03 JosVerburg

@JosVerburg why do you think it is the same issue? What is similar? (the callstack looks different to me)

karelz avatar Mar 30 '17 14:03 karelz

OK, I updated the callstacks in top post and latest reply and now I see the similarities. he inner exception in interesting in your case. Sorry for the confusion ...

karelz avatar Mar 30 '17 14:03 karelz

@JosVerburg

Thanks for posting a complete repro!

I build and ran the solution. However, I got the following exception: image

Your client code is incorrect. HttpClient design only allows an HttpRequestMessage to be sent ONCE. It's lifetime is over after one request. Due to this exception, I was unable to repro the "The server returned an invalid or unrecognized response" exception.

Can you revise your repro so that it doesn't try to reuse the same HttpRequestMessage? Perhaps that is the actual problem?

davidsh avatar Apr 01 '17 21:04 davidsh

Actually, after looking at your code again, you are creating a new HttpRequestMessage each time! So, this is very strange because that exception should not be returned.

I ran the repro again and was able to generate the exception we're investigating. image

The exception happened after 906 iterations. image

We will investigate. Thx.

davidsh avatar Apr 01 '17 21:04 davidsh

I've seen this issue consistently on Windows Server 2012 R2 running on .net core 1.0.

I'm using ASP.NET Core Proxy to proxy a number of requests to another application running on WebListener. Proxy internally uses HttpClient.

From the looks of it (applications set up and stacktrack), it the same issue as reported by @JosVerburg.

Is there anything I can contribute in order to get a diagnosis of this issue?

devatwork avatar Apr 03 '17 08:04 devatwork

We've also tried to reproduce the issue with wget. wget did not indicate an invalid response.

Fiddler also doesn't indicate an invalid response.

JosVerburg avatar Apr 03 '17 09:04 JosVerburg

I've written a test client for full framework, see https://github.com/devatwork/ReproduceUnrecognizedResponse. Interestingly enough full framework also thinks this is an issue on the server side as well. Stack trace:

System.Net.WebException: The server committed a protocol violation. Section=ResponseStatusLine
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)

This seems to suggest that the server application returns an invalid response. In both Jos's case and my case the server application is using WebListener/HttpSysServer. The HTTP response messages are identical according to a Fiddler trace I've run.

devatwork avatar Apr 03 '17 09:04 devatwork

RFC 2626 and RFC 2731 states that a head request MUST NOT return a response body. See also the last paragraph of https://tools.ietf.org/html/rfc2616#section-4.3.

Now give the HTTP request/response pair reported by @JosVerburg:

Request:
HEAD http://test.com:5000/ HTTP/1.1
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Host: test.com:5000

Response:
HTTP/1.1 302 Found
Transfer-Encoding: chunked
Location: /redirect.html
Server: Microsoft-HTTPAPI/2.0
Date: Thu, 30 Mar 2017 07:45:19 GMT

0

As to my current understanding of the HTTP 1.1 spec, the response is indeed invalid. If that is indeed the case, the problem is server-side either in http.sys, HttpSysServer or in ASP.NET Core and not client-side.

Any thoughts or flaws in my reasoning?

devatwork avatar Apr 03 '17 10:04 devatwork

Any thoughts or flaws in my reasoning?

This is interesting. I tried out the revised repro. The one using .NET Framework for client size seems to throw the PtotocolViolationException right away (after the first iteration). The .NET Core client repro runs for a few hundred itereations and then throws the WinHTTP "The server returned an invalid or unrecognized response" error.

I have sent the trace logs to the WinHTTP team for analysis. Is it possible that the server is buggy and only sometimes sends content body payload in the response on the HEAD request?

davidsh avatar Apr 03 '17 21:04 davidsh

The one using .NET Framework for client size seems to throw the PtotocolViolationException right away (after the first iteration). The .NET Core client repro runs for a few hundred itereations and then throws the WinHTTP "The server returned an invalid or unrecognized response" error.

I've observed the same behavior. The .NET Core client seems to throw after a seemingly random number of iterations. The Fiddler logs did not show any difference in response. It looks like a timing issue.

Is it possible that the server is buggy and only sometimes sends content body payload in the response on the HEAD request?

I think there is also a server bug, see RFC 2616 section 9.4 and the HTTP observed by @JosVerburg and me. If you look at the application code of the server application, there is very little room for error.

Is you change the repro WebApp to .UseKestrel() instead of .UseWebListener(), both client applications run fine. So there is likely to be a bug somewhere in WebListeren/HttSysListener, right?

Also, this does not explain why the .NET Core HttpClient throws after some time. If the response is indeed invalid, the client should have thrown right away, just like the desktop behavior, right?

devatwork avatar Apr 04 '17 06:04 devatwork

We have finished the analysis of this issue. The problem is due to the server violating the RFC by returning an entity-body. It is sending a 0-byte chunk. The response to a HEAD request should be to send absolutely no bytes of data for the response body.

The reason for the difference in behavior between .NET Framework and .NET Core is due to race conditions and architectural differences.

.NET Framework will fail right away after the first request. It fails, by-design, with a ProtocolViolationException.

.NET Core (on Windows) uses native WinHTTP. This is the explanation from the WinHTTP team about why it is failing later on (due to race condition).

The server is sending entity-body in response to a HEAD request which is illegal. This results in a race between a request picking up the connection and the connection being aborted for having unexpected data. To be clear the illegal body is the 0 length chunk. There should be nothing after the headers in response to a HEAD. The WinHTTP stack was initially built to support pipelining, so any data after the headers is processed after the current request since by RFC this data is not part of the current response. This means the current request will complete, and then the subsequent data will be processed asynchronously. When it is processed there is no active request, so the connection is terminated. This races with the connection being picked up out of the pool.

We are closing this issue. Please open up a new issue with the ASP.NET team regarding WebListener.

davidsh avatar Apr 04 '17 20:04 davidsh

I've reported this to the ASP.NET Core team, see https://github.com/aspnet/HttpSysServer/issues/339. Hopefully they will be able to address the issue soon.

@davidsh: I don't think we've confirmed that the original issue reported by @corinas and @Petermarcu is actually caused by the same issue, so we may want to wait for their input?

devatwork avatar Apr 05 '17 06:04 devatwork

I ran the .net core test client on Linux (1.1.1-runtime docker image) connecting to the web application running on Windows and it does not fail at all. So the behavior is different on each platform/runtime combination.

Although the root cause is on the server side, IMHO the clients should consistently report that the server returned a malformed HTTP 1.1 response. I'm interested to understand whether behavior differences are considered bugs or not.

devatwork avatar Apr 05 '17 08:04 devatwork