Closed
Description
When 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