ResponseCachingMiddleware: exception "Cannot allocate more than 4096 bytes in a single buffer" when a client disconnects
See original GitHub issueWhen we upgraded from .NET Core 3.1 to .NET 5.0 (5.0.3), we started getting rare random exception from ResponseCachingMiddleware:
System.ArgumentOutOfRangeException: Cannot allocate more than 4096 bytes in a single buffer (Parameter 'size')
at System.Buffers.MemoryPoolThrowHelper.ThrowArgumentOutOfRangeException_BufferRequestTooLarge(Int32 maxSize)
at System.Buffers.SlabMemoryPool.Rent(Int32 size)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1OutputProducer.GetSpan(Int32 sizeHint)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.GetSpan(Int32 sizeHint)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponsePipeWriter.GetSpan(Int32 sizeHint)
at Microsoft.AspNetCore.ResponseCaching.CachedResponseBody.CopyToAsync(PipeWriter destination, CancellationToken cancellationToken)
at Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware.TryServeCachedResponseAsync(ResponseCachingContext context, IResponseCacheEntry cache
Entry)
at Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware.TryServeFromCacheAsync(ResponseCachingContext context)
at Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware.Invoke(HttpContext httpContext)
at ResponseCachingMiddleware.DotNet5.Bug.RequestLoggingMiddleware.Invoke(HttpContext httpContext) in C:\Projects\ResponseCachingMiddleware.DotNet5.Bug\
ResponseCachingMiddleware.DotNet5.Bug\RequestLoggingMiddleware.cs:line 22
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext] (IHttpApplication`1 application)
We get this exception once in 3 days, very random and very rare. I discovered it is likely happening when a client disconnects in the middle of downloading a large cached response. Here is a demo project with a clean Web API project to replicate it: https://github.com/xhafan/ResponseCachingMiddleware.DotNet5.Bug
I can replicate it on Amazon Linux (amzn2-ami-ecs-hvm-2.0.20190614-x86_64-ebs) and on Windows 7 SP1 with latest .NET 5.0.3 . This issues was not happening on .NET Core 3.1
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Memory allocation errors on 6.1.1 / Kernel & Hardware ...
ghc: mmap 4096 bytes at (nil): Cannot allocate memory ghc: Try specifying an address with +RTS -xm<addr> -RTS Segmentation fault (core ...
Read more >mysql Fatal error: cannot allocate memory for the buffer pool
The "diminishing returns" means that if you need more swap space than twice your RAM size, you'd better add more RAM as Hard...
Read more >tar - "Cannot allocate memory" when reading from SCSI tape
The solution is to use another program to read from the tape - one that can have the read buffer size set to...
Read more >Running any command returns "Cannot allocate memory ...
Solution. As it says in the error messages, your machine has run out of memory. This can be for a number of reasons,...
Read more >Fatal error: cannot allocate memory for the buffer pool
MySQL buffer size is bigger than free RAM available on the server. Resolution. Contact system administrator to allocate more RAM to the server....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Since I reported this issue, we got around 1400 exceptions like this in production, so it’s not a
rareissue really.It looks like Http1/2/3OutputProducer all have a bug in GetFakeMemory where it fails with “sizeHints” (should be named “minSize” but isn’t for historical reasons) greater than 4KB. We should do the same thing as StreamPipeWriter and fall back to the ArrayPool for large “sizeHints” or just new up a byte array like Http1OutputProducer.AddSegment does.