Functions are timing out after 10 seconds despite completing task in less than 10 seconds

All of my functions are timing out after 10 seconds despite their tasks being completed in less than 10 seconds. Most of these functions connect to the database to do CRUD operations, below I’ve posted a snippet of my login.js function.

const middy = require('middy')

const onlyPost = require('./_middleware/onlyPost')

const bcrypt = require('bcryptjs')

const jwt = require('jsonwebtoken')

const { connectToDb } = require('./_utils/db')

const { BAD_REQUEST } = require('./_utils/errorMessages')

const login = async (event, context) => {

    // Destructure 'username' and 'password' out of the body

    const { username, password } = JSON.parse(event.body)

    // Check if 'username' and 'password' exist and are set

    if (!username || !password) return BAD_REQUEST

    // Connect to database

    const { User } = connectToDb()

    // Check if the user exists in the database

    const user = await User.findOne({

        where: { username }

    })

    if (!user)

        return {

            statusCode: 400,

            body: JSON.stringify({

                err: true,

                msg: 'User not found.'

            })

        }

    // Compare passwords

    const comparePasswords = await bcrypt.compare(password, user.hashedPassword)

    if (!comparePasswords)

        return {

            statusCode: 403,

            body: JSON.stringify({

                err: true,

                msg: 'Password invalid.'

            })

        }

    // Generate a JWT

    const token = jwt.sign(

        {

            id: user.id,

            isAdmin: user.isAdmin

        },

        process.env.AUTH_SECRET

    )

    // Console log

    console.log(user)

    // Return the JWT

    return {

        statusCode: 200,

        body: JSON.stringify({

            err: false,

            token

        })

    }



module.exports.handler = middy(login).use(onlyPost())

This is the output from Netlify for login.js

   1:09:20 PM: 2020-02-15T19:09:20.448Z	d5e2e2d4-071b-4c86-995d-2aafab1456f1	INFO	Executing (default): SELECT `id`, `firstname`, `username`, `hashedPassword`, `email`, `discord`, `count`, `isAdmin`, `createdAt`, `updatedAt` FROM `users` AS `user` WHERE `user`.`username` = 'Athys' LIMIT 1;

1:09:20 PM: 2020-02-15T19:09:20.667Z	d5e2e2d4-071b-4c86-995d-2aafab1456f1	INFO	user {...}

1:09:29 PM: Duration: 10010.15 ms	Memory Usage: 112 MB	Init Duration: 519.65 ms	
1:09:29 PM: 2020-02-15T19:09:29.992Z d5e2e2d4-071b-4c86-995d-2aafab1456f1 Task timed out after 10.01 second

As you can see, the user is grabbed within 1 second of invoking the function, however, I’m still receiving this after 10 seconds:

Any ideas as to what is going on?

Hi, @Athys, and welcome to our Netlify community site.

The most common root cause of this type of issue is that there is a worker process which isn’t exiting. This is most commonly a thread/worker that make a connections to a database or other external service/API.

Would you please confirm you code is closing this connection before returning the response?

Is there a set way to do this? Should I log something from the module where I connect to the database? I’ve console.logged the object returned from the database (pasted above in the code snippet) and immediately do a return statement (thus completing the function lifecycle). Am I missing something?

The correct way to close the connection will depend in what package is being used to access a specific database but in many cases the connection can be ended with something similar to this:

connection.end();
// or
connection.close();

It looks like the database connection above is named User based on this:

    // Connect to database

    const { User } = connectToDb()

So, maybe try:

User.close();

What package/library are you using to access this database and what kind of database is it?

I’m using Sequalize as my ORM and MySQL as the database.

It appears that Sequelize uses .close().