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
- Merge pull request #1004 from aws/normj/pr-3190 — committed to aws/aws-sdk-net by normj 3 years ago
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:
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: #1010My 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. ifInstanceis null, it will fail in other places, including the line that immediately follows. I don’t think that Instance can be null anywhere inRenewCredentials().The
Instancegetter uses a lock to make sure that the_instanceis 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 = nullon 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
nullat 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 likea.b.c = d;.If you’re worried about things changing during your slightly more complex read expression, then you can do
But if the IL for that is the same as before, then there is no need for even a temp var.
InstanceinRenewCredentials()since theInstancegetter can potentially callRenewCredentials()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 getthiswith repeated checks and locks. You can use_instance, or even just usethisimplicitly, which should be the same object asInstance. If they’re not the same not, you have other issues.The way that
EC2InstanceMetadata.IAMSecurityCredentialsis sync but actually also async insideAsyncHelpersis 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_USESOCKETSHTTPHANDLERdidnt get rid of it for us.@Reasonably Have you tried updating .NETCore to version
2.1.402andMicrosoft.AspNetCore.Allto version2.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:
AWS_CONTAINER_CREDENTIALS_RELATIVE_URIenvironment variable, uses the ECS Task IAM Role/Metadata behavior if its setI’m clearing out of this thread but wanted to document this a bit here. Thanks for the help/discussion.