Task Canceled Exception while invoking AWS Lambda

0 votes

I am hitting an unhandled TaskCanceledException every time my code invokes an AWS Lambda. The code runs on an Android device. (It's written in C# with Xamarin.Android and references AWSSDK

.Core, AWSSDK.Lambda).

  1. Why is the task timing out? [Update: this has been figured out]
  2. Why isn't the exception handled?
  3. Why can't I see any diagnostics from AWS SDK for .NET in the logs?

Code:

public class SomeActivity: Activity
{
    private AmazaonLambdaClient mAWSLambdaClient;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(...);
        FindViewById(...).Click += ButtonClickAsync;

        // System.Diagnostics.Trace redirects to Log.Debug with TAG="System.Diagnostics.Trace"
        System.Diagnostics.Trace.Listeners.Add(new MyAndroidTraceListener("System.Diagnostics.Trace"));
        System.Diagnostics.Trace.TraceInformation("Android trace listener installed");

        // AWS logs to System.Diagnostics
        AWSConfigs.LoggingConfig.LogTo = LoggingOptions.SystemDiagnostics;
        AWSConfigs.LoggingConfig.LogResponses = ResponseLoggingOption.Always;
    }

    protected override void OnStart()
    {
        base.OnStart();

        var idToken = ...
        var awsCredentials = new CognitoAWSCredentials("IdentityPoolID", AWSConfig.RegionEndpoint);
        awsCredentials.AddLogin("accounts.google.com", idToken);

        mAWSLambdaClient = new AmazonLambdaClient(awsCredentials, AWSConfig.RegionEndpoint);
    }

    protected override void OnStop()
    {
        base.OnStop();

        mAWSLambdaClient.Dispose();
        mAWSLambdaClient = null;
    }

    private async void ButtonClickAsync(object sender, System.EventArgs e)
    {
         await DoSomethingAsync();
    }

    private async Task DoSomethingAsync()
    {
        var lambdaRequest = ...
        try
        {
            var lambdaInvokeTask = mAWSLambdaClient.InvokeAsync(lambdaRequest);
            invokeResponse = await lambdaInvokeTask; <= VS breaks here after ~30 to 60 seconds
        }
        catch (TaskCanceledException e) // also tried catching Exception, no luck
        {
            Log.Error(TAG, "Lambda Task Canceled: {0}, {1}", e.Message, e.InnerException);
            return;
        }
    }
}

Visual Studio breaks on the await line, telling me I have an unhandled TaskCanceledException: a task was canceled. Weird I do handle that exception. After the unhandled exception, I check the Device Log in Visual Studio. I filter by TAG="System.Diagnostics.Trace" and all I find is:

base apk Information 0:
Android trace listener installed

Where is the AWS SDK log I should have gotten according to logging-with-the-aws-sdk-for-net?

UPDATE:

I've figured out question 1, why it times out. It was due to a lambdaRequest with a bad PayloadStream set to a MemoryStream whose position had not been reset to 0 after JSON serializing an object to the stream.

I have not figured out why 2, the exception wasn't handled by the try/catch, and 3, why AWS SDK did not log as requested.

Sep 19, 2018 in AWS by bug_seeker
• 15,520 points
2,150 views

1 answer to this question.

0 votes
I'm guessing either the TaskCanceledException instance is not from the same namespace your code is expecting in the catch statement, or it is being thrown from the line just above your try-catch, i.e mAWSLambdaClient.InvokeAsync(lambdaRequest). What happens if you move that line and possibly more lines inside the try-catch block?

If this doesn't help, please post the stack trace.
answered Sep 19, 2018 by Priyaj
• 58,090 points

Related Questions In AWS

0 votes
0 answers

Getting exception while trying to build SsmClient from aws-sdk-java version 2

The exception I am getting -- java.lang.NoClassDefFoundError: org/apache/http/impl/client/DefaultClientConnectionReuseStrategy at ...READ MORE

Jan 24, 2023 in AWS by Tejashwini
• 3,820 points
384 views
0 votes
1 answer

AWS Lambda Provisioning Business Logic

AWS Lambda's main function is to reuse ...READ MORE

answered Apr 17, 2018 in AWS by Cloud gunner
• 4,670 points
678 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+1 vote
3 answers
+1 vote
10 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP