aspnetcore: System.IO.Pipelines.PipeReader.CopyToAsyncCore: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'start')

Description

Getting this exception in ASP.NET Core Runtime 5.0.11 Occurs occasionaly (quite small %). I checked and request seems to be not in cancelled state after it throws.

context?.HttpContext?.RequestAborted.IsCancellationRequested == false

Exception:

An unhandled exception was thrown by the application. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'start')
   at System.IO.Pipelines.PipeReader.CopyToAsyncCore[TStream](TStream destination, Func`4 writeAsync, CancellationToken cancellationToken)
   at MP.Sdk.Edge.Web.Formatters.InputFormatterBase.ToBytesAsync(Stream stream) in /opt/teamcity/work/af8a0ab010c87174/src/Sdk/MP.Sdk.Edge.Web/Formatters/InputFormatterBase.cs:line 65
using (MemoryStream memoryStream = new MemoryStream())
{
    using (stream)
    {
        // below throws
        await stream.CopyToAsync(memoryStream);
    }

    return memoryStream.ToArray();
}

Reproduction Steps

Reproes in AWS with load balancer. No luck reproducing locally so far.

Expected behavior

shouldn’t throw exception

Actual behavior

Exception:

An unhandled exception was thrown by the application. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'start')
   at System.IO.Pipelines.PipeReader.CopyToAsyncCore[TStream](TStream destination, Func`4 writeAsync, CancellationToken cancellationToken)
   at MP.Sdk.Edge.Web.Formatters.InputFormatterBase.ToBytesAsync(Stream stream) in /opt/teamcity/work/af8a0ab010c87174/src/Sdk/MP.Sdk.Edge.Web/Formatters/InputFormatterBase.cs:line 65

Regression?

was okay in netcoreapp2.1

Known Workarounds

none so far

Configuration

No response

Other information

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Reproduced!!!

!!! In .netcoreapp2.1 CopyToAsync returned empty byte array !!! In .net5.0 it throws exception

Some requests seem to have incorrect content-type. Reproes 100%. Data is json string.

that works: curl -X POST "http://%ip%:3611/events" -d %data% --header "Content-Type: application/json; charset=utf-8"

that doesn’t: curl -X POST "http://%ip%:3611/events" -d %data%

This might be an issue: Content-Type',value='application/x-www-form-urlencoded

Service is running. Press CTRL+C to stop...
2021/12/01 09:37:22.864||squad=Platformers|mp_environment_label=|AWSAccount=|pod=|Silo=|mpApp=|mpDGid=|MP.Sdk.Edge.Web.Formatters.DebugInformationLogger|16|mpEventLevel=W|Repeated=|ecsLocation=@|pkrupets::DebugInformationLogger::IHeaderDictionary::key='Content-Type',value='application/x-www-form-urlencoded'
2021/12/01 09:37:22.864||squad=Platformers|mp_environment_label=|AWSAccount=|pod=|Silo=|mpApp=|mpDGid=|MP.Sdk.Edge.Web.Formatters.DebugInformationLogger|16|mpEventLevel=W|Repeated=|ecsLocation=@|pkrupets::DebugInformationLogger::IHeaderDictionary::key='Accept',value='*/*'
2021/12/01 09:37:22.864||squad=Platformers|mp_environment_label=|AWSAccount=|pod=|Silo=|mpApp=|mpDGid=|MP.Sdk.Edge.Web.Formatters.DebugInformationLogger|16|mpEventLevel=W|Repeated=|ecsLocation=@|pkrupets::DebugInformationLogger::IHeaderDictionary::key='Host',value='10.173.0.108:3611'
2021/12/01 09:37:22.864||squad=Platformers|mp_environment_label=|AWSAccount=|pod=|Silo=|mpApp=|mpDGid=|MP.Sdk.Edge.Web.Formatters.DebugInformationLogger|16|mpEventLevel=W|Repeated=|ecsLocation=@|pkrupets::DebugInformationLogger::IHeaderDictionary::key='User-Agent',value='curl/7.55.1'
2021/12/01 09:37:22.864||squad=Platformers|mp_environment_label=|AWSAccount=|pod=|Silo=|mpApp=|mpDGid=|MP.Sdk.Edge.Web.Formatters.DebugInformationLogger|16|mpEventLevel=W|Repeated=|ecsLocation=@|pkrupets::DebugInformationLogger::IHeaderDictionary::key='Content-Length',value='397'
2021/12/01 09:37:22.864||squad=Platformers|mp_environment_label=|AWSAccount=|pod=|Silo=|mpApp=|mpDGid=|MP.Sdk.Edge.Web.Formatters.DebugInformationLogger|16|mpEventLevel=W|Repeated=|ecsLocation=@|pkrupets::DebugInformationLogger::ContentLength='397'
2021/12/01 09:37:22.864||squad=Platformers|mp_environment_label=|AWSAccount=|pod=|Silo=|mpApp=|mpDGid=|MP.Sdk.Edge.Web.Formatters.DebugInformationLogger|16|mpEventLevel=W|Repeated=|ecsLocation=@|pkrupets::DebugInformationLogger::Protocol='HTTP/1.1'
2021/12/01 09:37:22.864||squad=Platformers|mp_environment_label=|AWSAccount=|pod=|Silo=|mpApp=|mpDGid=|MP.Sdk.Edge.Web.Formatters.DebugInformationLogger|16|mpEventLevel=W|Repeated=|ecsLocation=@|pkrupets::DebugInformationLogger::RequestAborted='False'
2021/12/01 09:37:22.872||squad=Platformers|mp_environment_label=|AWSAccount=|pod=|Silo=|mpApp=|mpDGid=|MP.Sdk.Edge.Web.Formatters.DebugInformationLogger|16|mpEventLevel=W|Repeated=|ecsLocation=@|pkrupets::DebugInformationLogger::BodyReaderType='Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestPipeReader'
2021/12/01 09:37:22.872||squad=Platformers|mp_environment_label=|AWSAccount=|pod=|Silo=|mpApp=|mpDGid=|MP.Sdk.Edge.Web.Formatters.DebugInformationLogger|16|mpEventLevel=W|Repeated=|ecsLocation=@|pkrupets::DebugInformationLogger::BodyType='Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream'
2021/12/01 09:37:22.942||squad=Platformers|mp_environment_label=|AWSAccount=|pod=|Silo=|mpApp=|mpDGid=|Microsoft.AspNetCore.Server.Kestrel|16|mpEventLevel=E|Repeated=|ecsLocation=@|Connection id "0HMDKU4NSO314", Request id "0HMDKU4NSO314:00000002": An unhandled exception was thrown by the application. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'start')
   at System.Buffers.ReadOnlySequence`1.TryGet(SequencePosition& position, ReadOnlyMemory`1& memory, Boolean advance)
   at System.IO.Pipelines.PipeReader.CopyToAsyncCore[TStream](TStream destination, Func`4 writeAsync, CancellationToken cancellationToken)
   at MP.Sdk.Edge.Web.Formatters.InputFormatterBase.ToBytesAsync(Stream stream) in C:\dev\mPServer.sdkEdgeNet5\src\Sdk\MP.Sdk.Edge.Web\Formatters\InputFormatterBase.cs:line 60
   at MP.Sdk.Edge.Web.Formatters.SdkWebContextInputFormatter.ReadRequestBodyAsync(InputFormatterContext context) in C:\dev\mPServer.sdkEdgeNet5\src\Sdk\MP.Sdk.Edge.Web\Formatters\SdkWebContextInputFormatter.cs:line 45
   at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder.BindModelAsync(ModelBindingContext bindingContext)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value, Object container)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<<CreateBinderDelegate>g__Bind|0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ExceptionContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeNextResourceFilter()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

trying to create simple repro application, but porting to .net6.0 didn’t fix the issue