I have a simple lambda function that asynchronously makes an API calls and then returns data. 99% of the time this works great. When ever the API takes longer then the lambda configured timeout, it gives an error as expected. Now the issue is that when I make any subsequent calls to the lambda function it permanently gives me the timeout error.
"errorMessage": "2016-05-14T22:52:07.247Z {session} Task timed out after 3.00 seconds"
In order to test that this was the case I set the lambda timeout to 3 seconds and have a way to trigger these two functions within the lambda.
Javascript
function now() { return response.tell('success'); } function wait() { setTimeout(function() { return response.tell('success'); }, 4000); }
When I call the now
function there are no problems. When I call the wait
function I get the timeout error and then any subsequent calls to now
give me the same error.
Is this an expected behavior? I would think that any subsequent calls to the lambda function should work. I understand I can always increase the configuration timeout, but would rather not.
Lambda may retry multiple times, with a period of time between each retry.
If a Lambda function times out, it is terminated and any code that was running in it is discarded. This could lead to unexpected behavior in your application if you were relying on the function to perform a specific task.
To troubleshoot the retry and timeout issues, first review the logs of the API call to find the problem. Then, change the retry count and timeout settings of the AWS SDK as needed for each use case. To allow enough time for a response to the API call, add time to the Lambda function timeout setting.
Finding the root cause of the timeout. There are many reasons why a function might time out, but the most likely is that it was waiting on an IO operation to complete. Maybe it was waiting on another service (such as DynamoDB or Stripe) to respond.
You should look for how your function handle works with a specific context.callbackWaitsForEmptyEventLoop
If that boolean-type is false
, the setTimeout won't be ever fired, because you might've answered/handled the lambda invocation earlier. But if the value of callbackWaitsForEmptyEventLoop
is true
- then your code will do what you are looking for.
Also - it's probably easier to handle everything via callbacks directly, without the need for "hand-written" timeouts, changing configuration timeouts and so on...
E.g.
function doneFactory(cb) { // closure factory returning a callback function which knows about res (response) return function(err, res) { if (err) { return cb(JSON.stringify(err)); } return cb(null, res); }; } // you're going to call this Lambda function from your code exports.handle = function(event, context, handleCallback) { // allows for using callbacks as finish/error-handlers context.callbackWaitsForEmptyEventLoop = false; doSomeAsyncWork(event, context, doneFactory(handleCallback)); };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With