aws-sdk-net: Sporadic IndexOutOfRangeException when publishing to an AWS queue after updating to .NET 6 from .NET Core 3.1.

Describe the bug

After migrating to .NET 6 we started seeing the error from time to time when publishing a message to our AWS queue using the AmazonSimpleNotificationServiceClient sdk. Could it be because we are calling this in a loop ? Didn’t see any issues like this before the .NET update.

The request makes it to our queue but it fails trying to read the response.

Expected Behavior

Should be able to publish and get the response back successfully

Current Behavior

System.IndexOutOfRangeException: Index was outside the bounds of the array.     
at System.Net.Http.Headers.HttpHeaders.ReadStoreValues[T](Span`1 values, Object storeValue, HttpHeaderParser parser, Int32& currentIndex)
     at System.Net.Http.Headers.HttpHeaders.GetStoreValuesAsStringOrStringArray(HeaderDescriptor descriptor, Object sourceValues, String& singleValue, String[]& multiValue)
     at System.Net.Http.Headers.HttpHeaders.GetEnumeratorCore()+MoveNext()     at Amazon.Runtime.Internal.Transform.HttpClientResponseData.CopyHeaderValues(HttpResponseMessage response)
     at Amazon.Runtime.Internal.Transform.HttpClientResponseData..ctor(HttpResponseMessage response, HttpClient httpClient, Boolean disposeClient)
     at Amazon.Runtime.HttpWebRequestMessage.GetResponseAsync(CancellationToken cancellationToken)
     at Amazon.Runtime.Internal.HttpHandler`1.InvokeAsync[T](IExecutionContext executionContext)
     at Amazon.Runtime.Internal.Unmarshaller.InvokeAsync[T](IExecutionContext executionContext)
     at Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)
     at Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)
     at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
     at Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)
     at Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)
     at Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext executionContext)
     at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
     at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
     at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
     at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
     at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
     at Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)

Reproduction Steps

foreach (var request in requests)
{
      var response = await _snsClient.PublishAsync(request);
}

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

<PackageReference Include="AWSSDK.Core" Version="3.7.12.5" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.2" />
<PackageReference Include="AWSSDK.SimpleNotificationService" Version="3.7.3.77" />
<PackageReference Include="AWSSDK.SQS" Version="3.7.2.74" />

Targeted .NET Platform

.NET 6

Operating System and version

Windows 10

About this issue

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

Most upvoted comments

For anyone suddenly experiencing new errors on AWS SDK when retargeting an application from .NET Core 3.1 to .NET 6, check if you’re using an older version of the NewRelic agent on your application.

I had a similar issue with a container based .NET 6 Web API that started throwing the following exceptions as soon as it was retargeted to the newer version of the framework:

  • IndexOutOfRangeException
  • NullReferenceException
  • InvalidOperationException (this one was being thrown much less often than the other 2)

In my case the application was throwing these exceptions when calling PutObjectAsync of IAmazonS3, but I’ve seen similar reports on other AWS services as well (SQS, DynamoDB, etc), the stack trace is always identical.

The docker image for my application was changed into this:

ARG newrelicversion=10.2.0
 
# Install New Relic .NET agent
RUN apt-get update && apt-get install -y wget ca-certificates gnupg \
&& echo 'deb http://apt.newrelic.com/debian/ newrelic non-free' | tee /etc/apt/sources.list.d/newrelic.list \
&& wget https://download.newrelic.com/548C16BF.gpg \
&& apt-key add 548C16BF.gpg \
&& apt-get update \
&& apt-get install -y newrelic-dotnet-agent=$newrelicversion \
&& rm -rf /var/lib/apt/lists/*

# Enable New Relic .NET agent
ENV CORECLR_ENABLE_PROFILING=1 \
CORECLR_PROFILER={36032161-FFC0-4B61-B559-F6C5D41BAE5A} \
CORECLR_NEWRELIC_HOME=/usr/local/newrelic-dotnet-agent \
CORECLR_PROFILER_PATH=/usr/local/newrelic-dotnet-agent/libNewRelicProfiler.so \
NEW_RELIC_LICENSE_KEY=FEED_ME_FROM_CD \
NEW_RELIC_APP_NAME=FEED_ME_FROM_CDa

Previously it was using version 8.37.0 instead of the latest (at the time being) 10.2.0, and the name of the package being used was the older newrelic-netcore20-agent instead of the more recent newrelic-dotnet-agent. I imagine that somewhere in between version 8.37.0 and 10.2.0 this issue was fixed.

It is particularly tricky to pinpoint the New Relic agent as the root cause of the problem because the integration with New Relic is not affected in any way. The impact of running a .NET 6 application with an older New Relic agent could only be seen on HTTP calls to AWS in the form of exceptions, and even those point to the dotnet runtime, so there’s a lot of indirection here.

@ashishdhingra thanks for your response. Looked at the PR you mentioned and it seems like it should fix the issue. I’ll make sure we are on the latest version of .NET 6, but from what I’ve seen it looks like .NET 7 should fix this. So we’ll probably have to wait for that unless there is a patch release for .NET 6.

In the meantime we can look for and catch this specific error since I know the stack trace always ends with:

System.IndexOutOfRangeException: Index was outside the bounds of the array. at System.Net.Http.Headers.HttpHeaders.ReadStoreValues

Not ideal but this error occurs when trying to read the HTTP response so I know the request made it to the queue. In my case I don’t really need any information from the response. But might be worth exploring if there is a way to handle this on the aws-sdk side ? Other developers might experience similar issues. Just a though. Again, appreciate your response.

@jantranikianTAG Thanks for your response. This needs to be reviewed with the team for the workaround.