aws-powertools / powertools-lambda-dotnet

Powertools is a developer toolkit to implement Serverless best practices and increase developer velocity.
https://docs.powertools.aws.dev/lambda/dotnet/
MIT No Attribution
152 stars 24 forks source link

Bug: If you add [Tracing] for class which implements generic method then call for SECOND generic type fails #571

Closed Kuling closed 5 months ago

Kuling commented 5 months ago

Expected Behaviour

Call of generic method shouldn't fail.

Current Behaviour

Following exception was thrown.

System.InvalidCastException HResult=0x80004002 Message=Unable to cast object of type 'System.Threading.Tasks.Task1[System.Double]' to type 'System.Threading.Tasks.Task1[System.Int32]'. Source=REDACTED StackTrace: at MyFunction.SampleService.LoadAsync[T](T query) in C:\src\REDACTED\Function.cs:line 19

This exception was originally thrown at this call stack: MyFunction.SampleService.LoadAsync(T) in Function.cs

Code snippet

using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using AWS.Lambda.Powertools.Tracing;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace MyFunction
{
    public interface ISampleService
    {
        Task<T> LoadAsync<T>(T query);
    }

    [Tracing]
    public class SampleService : ISampleService
    {
        public async Task<T> LoadAsync<T>(T query)
        {
            return default;
        }
    }

    [Tracing]
    public class Function
    {
        public async Task<APIGatewayProxyResponse> FunctionHandlerAsync(APIGatewayProxyRequest request, ILambdaContext context)
        {
            var target = new SampleService();
            await target.LoadAsync<double>(3.4);
            await target.LoadAsync<int>(5); // Bug

            return null;
        }
    }
}

Possible Solution

No response

Steps to Reproduce

Run above lambda. On the line marked with comment "// Bug" you will encounter System.InvalidCastException. This exception happens only when [Tracing] flag is added.

I believe library somehow improperly cached return type (Task) from first invocation. And then used it in second invocation.

Powertools for AWS Lambda (.NET) version

1.4.1

AWS Lambda function runtime

dotnet6

Debugging logs

No response

boring-cyborg[bot] commented 5 months ago

Thanks for opening your first issue here! We'll come back to you as soon as we can. In the meantime, check out the #dotnet channel on our Powertools for AWS Lambda Discord: Invite link

hjgraca commented 5 months ago

Hey @Kuling thanks for raising an issue. I was able to reproduce the issue and working on a fix as we speak. Will let you know once we are ready to push the fix.

hjgraca commented 5 months ago

@Kuling pull request created #572 will try to release this week. Also looking at your code above it works but please decorate the methods and not the classes.

hjgraca commented 5 months ago

@Kuling Tracing 1.4.2 released with the fix https://www.nuget.org/packages/AWS.Lambda.Powertools.Tracing/1.4.2 Let us know if it works as expected

Kuling commented 5 months ago

Good morning @hjgraca I can confirm that 1.4.2 works without issues. Thank you very much for your help.