1

I created a Node.js Lambda Function for AWS using the Serverless framework for increasing different counters in a Postgres database based on event parameters. The function itself runs without any errors when invoking with serverless invoke local, it runs and works as expected, however, when invoked from Java, while it should finish and return, it simply times out.

I've tried several things including waiting for the Postgres pool to close, increasing timeout, returning with the callback function (which I thing is a good practice nevertheless as it makes more clear that the function ends there), and using promise chains instead of async-await, with no luck. The real question is if it's just how it works and I have to always add callbackWaitsForEmptyEventLoop(false) or is there a more elegant solution? I even tried the why-is-node-running package, and it says that 4 handles are keeping the process running, a TCPWRAP, a Timeout, and two TickObjects. I'm almost sure that node-postgres is causing this as I created multiple lambda functions suffering from the same issue.

// These are the last lines of the handler function
const insertQueries = [ 
    // Multiple queries using a node-postgres pool, e.g.
    // pool.query(...);
];

try {
    await Promise.all(insertQueries);
} catch(err) {
    return callback('Couldn\'t insert API stats: ' + err);
}

return callback(null, 'API stats inserted successfully!');

The AWS Java SDK only prints a debug message telling me that task timed out after 10.01 seconds (serverless.yml has 10 seconds set).

Rodolffo
  • 105
  • 1
  • 6
  • Is this Lambda function deployed in a VPC? Where does the PostgreSQL database allow inbound connections from? – jarmod Jun 25 '19 at 12:27
  • Well as of now none of them are in a VPC and also the PostgreSQL server allows connections from anywhere (it's something of a playground server). But I don't think it would be related anyway as data gets written into the DB every time I invoke the function. The only problem is that it doesn't finish after the callback without ```callbackWaitsForEmptyEventLoop(false)```, but I hope there's a better way without cluttering the code. – Rodolffo Jun 25 '19 at 13:38
  • Apologies, did not read the question carefully. Is this helpful? https://stackoverflow.com/questions/50497583/when-to-disconnect-and-when-to-end-a-pg-client-or-pool – jarmod Jun 25 '19 at 13:48

1 Answers1

0

You must to close all database connections after use it

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 24 '23 at 03:37