Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

290
Views
Lambda function is not exiting but is logging the return

I have a Lambda function that I am trying to get to work fully. The code works in the sense it does exactly what I need it to however the issue comes in that the function either times out if running on Lambda or if running on serverless framework local invoke, it will just hang there not exiting.

I am looking for help in trying to get my function to exit and not time out/hang. I have included the full function - without helper functions. I can log out every step fine, the end results of DB and billing service are perfectly fine even on larger input data that is fetched from DB.

If I so a serverless invoke local command it will log out {success: true} like my function has at the end and then sit there for what seems like forever.

Code:

module.exports.billing = async (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false
  const { initClientDbConnection } = require('./db')

  // DB setup
  global.clientConnection = initClientDbConnection()
  const dbConnection = await global.clientConnection
  return { success: true }
}

Edit - I have removed a ton of code and added the below file. It hangs with just this little bit of code so I think that is the issue because if I comment out everything except the first line and the return line it exits fine. However, when I uncomment the two remaining lines it hangs.

db.js

const mongoose = require("mongoose");

// Configuration file
const DB_URL = process.env.MONGO_URI

const clientOption = {
  socketTimeoutMS: 30000,
  keepAlive: true,
  useNewUrlParser: true,
  autoIndex: false,
  useUnifiedTopology: true
}

const initClientDbConnection = () => {
  const db = mongoose.createConnection(DB_URL, clientOption)

  db.on("error", console.error.bind(console, "MongoDB Connection Error>> : "))
  db.once("open", function() {
    console.log("MongoDB Connection OK")
  })
  require("./billing")
  return db
};

module.exports = {
    initClientDbConnection
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ended up finding the solution, the issue was the DB connection was not closing which was not clearing the event loop which caused the function to not exit.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!