Skip to content

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

Closed
@Kuling

Description

@Kuling

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/tracingCore tracing utilitybugUnexpected, reproducible and unintended software behaviourreleasedFix or implementation already in main and released

    Type

    No type

    Projects

    Status

    📋 Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions