question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middy cause a timeout with async lambda (but only on AWS, not on my machine)

See original GitHub issue

I had a problem with Middy 1.0.0 and NodeJS 12.x. I was trying to connect to Redshift using dbManager, like this:

const middy = require('@middy/core')
const dbManager = require('@middy/db-manager');

const lambda = middy(async (event, context) => {
    const { db } = context;
    const records = await db.distinct('XXX').from('YYY');

    // print result
    console.log(records);

    // return result
    return {
        'statusCode': 200,
        'body': JSON.stringify(records)
    }
});

lambda    
    .use(dbManager({
        config: {
          client: 'pg',
          connection: {
            host: 'XXX',
            port: '1111',
            schema: 'public',
            user: 'XXX',
            password: 'XXX',
            database: 'XXX'
          }
        }
    }));

module.exports = { lambda }

This was causing Lambda to get stuck, until timeout kill it. I start to searching through the issues of this repo and I’ve found that set context.callbackWaitsForEmptyEventLoop to false. So I introduced the WaitsForEmptyEventLoop middleware and it works but… it only works on my machine! 😃

Yep, joke apart, if I’m executing the sam local invoke FunctionName everything works: the local Lambda invoke correctly the remote redshift and so it ends successfully. But, once I run sam deploy the same code, the same build still produce a timeout when real Lambda is executed.

Why this problem? Any idea?

Bye

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
tyvdhcommented, Apr 28, 2020

I too am wrestling with this just now.

Exploring this package atm: https://github.com/middyjs/middy/tree/master/packages/do-not-wait-for-empty-event-loop

Looks like it works when set to:

handler
.use(doNotWaitForEmptyEventLoop({
  runOnBefore: true,
  runOnAfter: true,
  runOnError: true
}))
1reaction
tyvdhcommented, Apr 28, 2020

I forgot to close some postgresql pools which caused the event loop to stay full. Adding the do-not-wait-for-empty-event-loop package solved the issue but I really need to ensure I’m closing my pg pools 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot Lambda function retry and timeout issues ... - AWS
There are three reasons why retry and timeout issues occur when invoking a Lambda function with an AWS SDK:.
Read more >
Middy cause a timeout with async lambda (but only on AWS, not on ...
I had a problem with Middy 1.0.0 and NodeJS 12.x. I was trying to connect to Redshift using dbManager , like this: const...
Read more >
Middy framework let my AWS Lambda pending so it never ...
When I run this Lambda the result are retrieved (i.e. console.log print out the results) but the function remains pending until it doesn't...
Read more >
How AWS Lambda Retry really works - Guide | Medium
Each task can have his timeout value (unlimited). If the task is not completed in time, a StateTimeout error is generated. Make sure...
Read more >
Event loops and idle connections: why is my lambda not ...
For non-async handlers, function execution continues until the event loop is empty or the function times out. The response isn't sent to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found