KestrelHttpServer icon indicating copy to clipboard operation
KestrelHttpServer copied to clipboard

CPU usage spikes with new Sockets transport

Open cwe1ss opened this issue 8 years ago • 33 comments

I'm having a weird issue with the new "Sockets" transport and I hope you might have some ideas!

I'm running the latest .NET Core 2.1.1 / ASP.NET Core 2.1.1. I have a single-node Service Fabric cluster for development purposes on Azure (Windows, 2 cores, 16GB RAM) and it runs ~ 60 .NET Core processes (self-contained, win10-x64). About half of them are ASP.NET Core applications, the rest is regular console applications with background services. The applications are communicating via the Service Fabric HTTP reverse proxy on localhost (using HttpClient).

As it's a dev/demo system, there's almost no load on it. However, when using the new "Sockets" transport, I get high CPU usage (50% - 100%) on the ASP.NET core apps for quite a few seconds every few minutes which results in a very unstable system.

As you can see in the overall CPU usage of the machine, the system became much more stable after I switched back to Libuv. I afterwards tried Sockets() with one app and CPU usage became unstable again. cpu-overall

I've been running PerfView and it shows that the high CPU usage happens around these calls:

module Microsoft.AspNetCore.Server.Kestrel.Core <<Microsoft.AspNetCore.Server.Kestrel.Core!Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol+<ProcessRequests>d__186`1[Microsoft.AspNetCore.Hosting.Internal.HostingApplication+Context].MoveNext()>>
module system.io.pipelines <<system.io.pipelines!System.IO.Pipelines.Pipe+DefaultPipeReader.ReadAsync(value class System.Threading.CancellationToken)>>
module system.io.pipelines <<system.io.pipelines!System.IO.Pipelines.Pipe+DefaultPipeReader.AdvanceTo(value class System.SequencePosition,value class System.SequencePosition)>>

Here's some screenshots. Calls by name: callsbyname

Call tree: calltree-1 calltree-2 calltree-3

Process CPU usage with libuv: As I've said there's almost no load on the app/system. cpu-usage-libuv

Process CPU usage with Sockets transport: Some spikes just take 1-2 seconds, others take a lot longer. cpu-usage-sockets

Any ideas on a possible cause?

PS: I don't know much about PerfView so I hope that what I've done is not misleading.

cwe1ss avatar Jun 29 '18 13:06 cwe1ss

Pinging @pakrym just because the high exclusive sample sizes from DefaultPipeReader.ReadAsync and DefaultPipeReader.Advance inside ProcessRequests.

My guess is that the client is sending request data slowly enough that Kestrel tries to repeatedly reparse small segments of the request start line and headers, and for some reason, the libuv transport does a better job of batching data in order to reduce calls to the socket receive callback and thereby also the http parser.

@cwe1ss You could verify this guess by doing a tracing instead of a sampling based profile and looking at the invocation count of DefaultPipeReader.ReadAsync with the libuv vs socket transport given the similar load where the socket transport is performing worse. I don't know if PerfView can collect method invocation counts, but this is easy with dotTrace.

Alternatively, if you could give us sample server and client apps that repro these Socket transport CPU usage spikes, that would be even better.

halter73 avatar Jun 29 '18 18:06 halter73

@halter73 thank you very much for your response!

I did find a few exceptions in our logs regarding slow reading of the request body. However, there have only been ~ 15 exceptions in total in the last 7 days, so much less than the number of CPU spikes we've had (every few minutes per app). So it's hard to say if those timeouts were a potential cause or just a sign of high CPU load during these spikes.

Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException
Reading the request body timed out due to data arriving too slowly. See MinRequestBodyDataRate.

Also, I did not get any of these exceptions since I switched back to Libuv. Everything runs quite smoothly since the switch: response-duration

Note that I currently also have another issue with Application Insights that results in slowly increasing CPU usage / a memory leak - hence the small rise in response duration at the end. (Using the latest and greatest can be pretty tough some times...) However, I've disabled Application Insights for one of my apps and I've had the same CPU spikes with Sockets so I don't think they're related.

I guess it will be really hard to reproduce this in a sample app. 😞 I do have a dotTrace license so I could do some profiling with it, however, since I'm using Service Fabric I can't easily launch my apps with dotTrace on the server. I quickly tried remote profiling but it only showed full .NET applications. My experience with dotTrace is very limited unfortunately. Do you know of an easy way to do this with Service Fabric / .NET Core?

Your help is much appreciated.

cwe1ss avatar Jul 02 '18 09:07 cwe1ss

Having same issue on Ubunu 16.04. Local Windows test passed without any problems.

After many concurrent requests we see that 1 or 2 of 8 cores fully utilizing, even if request already processed. Utilizing CPU completes only if we see in logs:

Connection id "0HLF6G2D3H2RB" sending FIN. dbug: Microsoft.AspNetCore.Server.Kestrel[2] Connection id "0HLF6G2D3H2RB" stopped.

All tracing data related to reading from Pipes and AsyncTaskBuilder.

At 100 rps we see:

image

ZOXEXIVO avatar Jul 10 '18 16:07 ZOXEXIVO

I've also ran into this issue.
For our application, the requests should all be fairly quick because they're all within a single AWS region, not over the internet.

The application only handles a couple different request types, and only handles probably around 5 requests per second.

It's running in a Debian 9 container in ECS.

This CPU graph is averaged between 3 containers. Container CPU Graph. It's going above 100% cpu because it's allowed to burst over the CPU reservation we have assigned to it.

I tried adjusting the MinRequestBodyDataRate to 1 byte per second, with a 10 second grace period and it didn't have any noticeable change. Switching to libuv does appear to fix the problem.

Here's a flamegraph sampiling over 45 seconds. Flame Graph

jamiegs avatar Jul 20 '18 23:07 jamiegs

Also have this problem. cpu usage is stable after switch back to Transport.Libuv. image This image shows the cpu usage(%) 2.1.1 default transport and libuv.

lenew avatar Jul 21 '18 14:07 lenew

@pakrym are you reproduce it ?

ZOXEXIVO avatar Aug 01 '18 10:08 ZOXEXIVO

Also having this problem in Azure App Services. Is the workaround for now to .UseLibuv() from program main?

kspearrin avatar Aug 06 '18 19:08 kspearrin

Calling .UseLibuv() is a workaround for this issue.

halter73 avatar Aug 06 '18 21:08 halter73

I have attempted to bring in the .UseLibuv() workaround to fix problems we might be able to attribute to this. Upon bringing the nuget package in and applying the workaround, deploying to an azure web app, and letting it go we see almost full 100% cpu utilization on the app service plan. The same utilization is not seen running it locally.

.NET Core 2.1.2 all latest Microsoft.AspNetCore packages all latest Microsoft.ApplicationInsights packages

nforysinski avatar Aug 11 '18 16:08 nforysinski

For AppInights are you using version 2.4.0? https://github.com/Microsoft/ApplicationInsights-aspnetcore/issues/690#issuecomment-411968077

benaadams avatar Aug 11 '18 17:08 benaadams

We have a fix for this in our next patch release (2.1.4, September.)

muratg avatar Aug 24 '18 19:08 muratg

@muratg, @pakrym any commit/text description of problem ?

ZOXEXIVO avatar Aug 25 '18 01:08 ZOXEXIVO

I am having this same issue with the Sockets transport in Debian9 with sdk 2.1.4, .net core 2.1.2 Fixed by switching to libuv.

angelMachin avatar Sep 05 '18 14:09 angelMachin

There was a bug in System.IO.Pipelines that caused ReadAsync calls not to "block" in some cases resulting in a tight loop and CPU spike until more data was written by the client.

pakrym avatar Sep 11 '18 17:09 pakrym

Thanks for looking into this @pakrym! Is this fixed with the most recent updates (ASP.NET 2.1.4, System.IO.Pipelines 4.5.1) or will this be included in a future release?

cwe1ss avatar Sep 12 '18 07:09 cwe1ss

@cwe1ss yes, the fix is in 2.1.4/4.5.1.

pakrym avatar Sep 12 '18 15:09 pakrym

Fixed in 2.1.4.

muratg avatar Sep 12 '18 20:09 muratg

Whew! Stuck on this for two weeks, thanks for the update! Problems went away after upgrading, see https://stackoverflow.com/questions/52561063

calebickler avatar Oct 01 '18 22:10 calebickler

This may sound dumb, but fixed in version 2.1.4 of what, exactly? The .net core runtime? sdk? some other library?

MatthewLymer avatar Oct 09 '18 19:10 MatthewLymer

@MatthewLymer The fix is in the ASP.NET Core runtime 2.1.4. You can get it via installing the latest 2.1 SDK or install the .NET Core and ASP.NET Core runtimes directly.

muratg avatar Oct 09 '18 19:10 muratg