aws-sdk-net: Dotnet Core 2.1 - AmazonSQSClient AmazonServiceException when call ReceiveMessageAsync

Hi. When I updated dotnet core (2.0.6 -> 2.1) and deployed on server, we met some error about credentials in AmazonSQSClient. I think it is from dotnet core 2.1 HttpClientHandler update. So I rollback dotnet core version (2.1 -> 2.0.6), then there is no exception anymore.

Expected Behavior

Whenever we call Amazon.SQS.AmazonSQSClient.ReceiveMessageAsync(ReceiveMessageRequest request, CancellationToken cancellationToken = default(CancellationToken)), there should be no exception.

Current Behavior

The function throws exception very frequently(3 minutes in 6 hour average) like below.

Amazon.Runtime.AmazonServiceException: Failed to retrieve credentials from EC2 Instance Metadata Service.
   at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentials()
   at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentialsAsync()
   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)

Context

I want to get the network performance provided by dotnet core 2.1, with AWS features.

Your Environment

  • AWSSDK.Core version used: 3.3.24.2
  • AWSSDK.SQS version used: 3.3.3.10
  • Operating System and version: Amazon Linux AMI 2017.09.k x86_64 ECS HVM GP2
  • Dotnet Build Docker Image: microsoft/dotnet:2.1-sdk
  • Dotnet Runtime Docker Image: microsoft/dotnet:2.1-runtime

.NET Core Info

  • .NET Core version used for development: 2.1
  • .NET Core version installed in the environment where application runs: microsoft/dotnet:2.1-runtime (docker image)
  • Output of dotnet --info:
Host (useful for support):
  Version: 2.1.2
  Commit:  811c3ce6c0

.NET Core SDKs installed:
  No SDKs were found.

.NET Core runtimes installed:
  Microsoft.NETCore.App 2.1.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 4
  • Comments: 25 (4 by maintainers)

Commits related to this issue

Most upvoted comments

We have a web api on .NET core 2.1 and publishing messages to SNS. We have seen that same error call-stack in our logs:

Amazon.Runtime.AmazonServiceException: Failed to retrieve credentials from EC2 Instance Metadata Service.
   at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentials()
   at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentialsAsync()

We have the problem seen at the same time that sometimes a machine’s CPU goes up to 100%, requests fail, and about half the time it recovers, and half the time the resulting healthcheck failure causes it to be terminated. This severe issue means that we are not rolling out .NET core into production any further.

Since something is going wrong in the class DefaultInstanceProfileAWSCredentials, we therefore raised this PR for discussion: #1010

My questions related to this class are:

  • Why do you use Instance?.logger.Error() on line 155? Instance?. - with a null check - is not done anywhere else. if Instance is null, it will fail in other places, including the line that immediately follows. I don’t think that Instance can be null anywhere in RenewCredentials().

  • The Instance getter uses a lock to make sure that the _instance is not null. But the lock is acquired every time the property is accessed. This appears to be an ideal candidate for double-checked locking to reduce the amount of locking here drastically.

  • I would expect Instance.lastRetrievedCredentials = null on line 159 to come before the logging that preceeds it, since getting into the desired state is more important and faster than the logging. If logging takes a while or blocks, then other threads have more chance to access the state that you have decided is unwanted.

  • As pointed out by @arishlabroo, is it necessary to set the creds to null at all? Is keeping the previous creds not a better option?

  • Are you aware of the issues with spinlocks under load? This looks a bit like the CPU spikes that we see. 5 seconds (line 42) is a very long time to wait on a cpu-intensive spinlock.

  • Where the Spinlock is used, Is any lock necessary at all? It is around assignments and reads. .NET reference assignment is an atomic operation anyway. There is no halfway state that other threads need to be locked away from, inside a statement of the form a = b;. I think this is also true of statements like a.b.c = d;.

If you’re worried about things changing during your slightly more complex read expression, then you can do

var mine = lastRetrievedCredentials; // atomic assign
 credentials = mine?.Copy();

But if the IL for that is the same as before, then there is no need for even a temp var.

  • Have you considered not using Instance in RenewCredentials() since the Instance getter can potentially call RenewCredentials() again. There may be no actual potential for recursion, but it gives me a bad feeling to call the property that called you, to essentially get this with repeated checks and locks. You can use _instance, or even just use this implicitly, which should be the same object as Instance. If they’re not the same not, you have other issues.

The way that EC2InstanceMetadata.IAMSecurityCredentials is sync but actually also async inside AsyncHelpers is a deeper issue that requires a more comprehensive fix, out of scope today.

also, looking at the implementation here

https://github.com/aws/aws-sdk-net/blob/master/sdk/src/Core/Amazon.Runtime/Credentials/_bcl+coreclr/DefaultInstanceProfileAWSCredentials.cs

Looks like even if there is a failure to get the credentials, null credentials are cached for 2 minutes. All subsequent calls fail with default FailedToGetCredentialsMessage. This is a major issue we are running into production without any seeming workaround.

Is there a workaround 🤔 you can recommend till you look into this more?

setting the DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER didnt get rid of it for us.

@Reasonably Have you tried updating .NETCore to version 2.1.402 and Microsoft.AspNetCore.All to version 2.1.4 ? It fixes this issue: https://github.com/aspnet/KestrelHttpServer/issues/2694 And in our experience so far, we’re not seeing CPU spikes afterwards.

We found our issue, it’s a quirky behavior of the IIS docker image:

  • Docker container environment variables are set as process variables for the entrypoint (not machine environment variables)
  • servicemonitor.exe sets/propagates the process-level environment variables to IIS but only for the default app pool
  • we were using custom app-pools so the docker-injected environment variables weren’t set
  • AWS SDK looks for the AWS_CONTAINER_CREDENTIALS_RELATIVE_URI environment variable, uses the ECS Task IAM Role/Metadata behavior if its set
  • Since it wasn’t set, the AWS SDK was using the default 169.254.169.254 address to try and retrieve metadata

I’m clearing out of this thread but wanted to document this a bit here. Thanks for the help/discussion.