Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any CRUD statement execution may have rare hang case #9492

Closed
jack-chui opened this issue Oct 21, 2020 · 8 comments
Closed

Any CRUD statement execution may have rare hang case #9492

jack-chui opened this issue Oct 21, 2020 · 8 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@jack-chui
Copy link

jack-chui commented Oct 21, 2020

Do you want to request a feature or report a bug?
bug report

What is the current behavior?
I am using Mongoose in AWS Lambda and using the following code to connect with MongoDB

let conn = null;

const connectionDB = async (logger) => {
    logger.writeActionLog("MongoDB", "Start Connection");

    if (conn == null) {
        conn = mongoose.createConnection(uri, {
            useNewUrlParser: true,
            useFindAndModify: false,
            useUnifiedTopology: true,

            bufferCommands: false, // Disable mongoose buffering
            bufferMaxEntries: 0 // and MongoDB driver buffering
        });

        await conn;

        for (const [key, value] of Object.entries(CollectionName)) {
            conn.model(value, require(`../../schema/${value}`));
        }
    } else {
        logger.writeActionLog("MongoDB", "Used cache");
    }

    logger.writeActionLog("MongoDB", "Start Success");
}

let getCollection = async (name) => {
    return conn.model(name);
}

module.exports = {
    CollectionName,
    connectionDB,
    getCollection,
    ObjectId
}

When I execute any CRUD statement (e.g. aggregate), Mongoose can return result to in < 1s. However, it has a rare hanging case without any exception or error in the same CRUD. Even I wait it in 900s, it still hang.

If the current behavior is a bug, please provide the steps to reproduce.

  1. Use the above connectionDB() function
  2. getCollection and execute CRUD

What is the expected behavior?
All CRUD statement should be always work.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
mongoose: ^5.10.9
Node.js 12.x

@jack-chui jack-chui changed the title Execute any CRUD statement may hang in sometime Execute any CRUD statement may have rare hang case Oct 21, 2020
@jack-chui jack-chui changed the title Execute any CRUD statement may have rare hang case Any CRUD statement execution may have rare hang case Oct 21, 2020
@bhrigushr
Copy link

Issue may be related with #9179

@jack-chui
Copy link
Author

Issue may be related with #9179

It seem that it is not the same issue. #9179 still can obtain the result. But I cannot obtain any result or error if CRUD statement hang.

@vkarpov15
Copy link
Collaborator

@SinLok just to confirm, have you tried waiting for 10 seconds? Also, try reducing heartbeatFrequencyMS to 1000 and see if that helps.

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Oct 24, 2020
@jack-chui
Copy link
Author

@SinLok just to confirm, have you tried waiting for 10 seconds? Also, try reducing heartbeatFrequencyMS to 1000 and see if that helps.

I tried to set AWS Lambda timeout to 900s and added 2 console.log surrounding Mongoose aggregate function. After 900s, I saw AWS print timeout to me and only the first console.log output if the aggregate hanging.

For heartbeatFrequencyMS, I will try it later.

@jack-chui
Copy link
Author

@SinLok just to confirm, have you tried waiting for 10 seconds? Also, try reducing heartbeatFrequencyMS to 1000 and see if that helps.

Using heartbeatFrequencyMS 1000, the problem still exist.

@vkarpov15 vkarpov15 added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Nov 2, 2020
@vkarpov15 vkarpov15 added this to the 5.10.12 milestone Nov 2, 2020
@vkarpov15
Copy link
Collaborator

Try reducing serverSelectionTimeoutMS to 1000:

conn = mongoose.createConnection(uri, {
            useNewUrlParser: true,
            useFindAndModify: false,
            useUnifiedTopology: true,

            serverSelectionTimeoutMS: 1000, // <-- Add this
            heartbeatFrequencyMS: 1000,

            bufferCommands: false, // Disable mongoose buffering
            bufferMaxEntries: 0 // and MongoDB driver buffering
        });

That should help you get an error message. It might be that Mongoose isn't able to connect for some reason, and is stuck in a server selection loop.

@vkarpov15 vkarpov15 added needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Nov 3, 2020
@vkarpov15 vkarpov15 removed this from the 5.10.12 milestone Nov 3, 2020
@jack-chui
Copy link
Author

jack-chui commented Nov 3, 2020

Try reducing serverSelectionTimeoutMS to 1000:

conn = mongoose.createConnection(uri, {
            useNewUrlParser: true,
            useFindAndModify: false,
            useUnifiedTopology: true,

            serverSelectionTimeoutMS: 1000, // <-- Add this
            heartbeatFrequencyMS: 1000,

            bufferCommands: false, // Disable mongoose buffering
            bufferMaxEntries: 0 // and MongoDB driver buffering
        });

That should help you get an error message. It might be that Mongoose isn't able to connect for some reason, and is stuck in a server selection loop.

I am getting 3 types of error:

  1. Task timed out after 10.01 seconds <- this comes from AWS Lambda timeout since CRUD statement hang
  2. Server selection timed out after 1000 ms
  3. MongooseServerSelectionError: connection <monitor> to **_MY SERVER IP_** timed out\\n at Function.Model.$wrapCallback (\/var\/task\/node_modules\/mongoose\/lib\/model.js:4856:32)\\n at \/var\/task\/node_modules\/mongoose\/lib\/query.js:4393:21\\n at \/var\/task\/node_modules\/mongoose\/lib\/helpers\/promiseOrCallback.js:31:5\\n at new Promise (<anonymous>)\\n at promiseOrCallback (\/var\/task\/node_modules\/mongoose\/lib\/helpers\/promiseOrCallback.js:30:10)\\n at model.Query.exec (\/var\/task\/node_modules\/mongoose\/lib\/query.js:4392:10)\\n at model.Query.Query.then (\/var\/task\/node_modules\/mongoose\/lib\/query.js:4473:15)\\n at processTicksAndRejections (internal\/process\/task_queues.js:97:5)

Btw, I am using MongoDB atlas as my DB server

@vkarpov15 vkarpov15 removed the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Nov 4, 2020
@vkarpov15 vkarpov15 added this to the 5.10.12 milestone Nov 4, 2020
@vkarpov15
Copy link
Collaborator

Try setting socketTimeoutMS to 5000:

conn = mongoose.createConnection(uri, {
            useNewUrlParser: true,
            useFindAndModify: false,
            useUnifiedTopology: true,

            serverSelectionTimeoutMS: 1000,
            heartbeatFrequencyMS: 1000,
            socketTimeoutMS: 5000, // <-- Add this

            bufferCommands: false, // Disable mongoose buffering
            bufferMaxEntries: 0 // and MongoDB driver buffering
        });

The "connection timed out" error is often indicative of a very slow query timing out the socket.

Another alternative: there is a slight race condition in your connectionDB() function. If you call connectionDB() twice in parallel, the 2nd call won't wait for the connection to be established. I don't think that is causing this issue, but, just to be safe, try putting:

    if (conn == null) {
        conn = mongoose.createConnection(uri, {
            useNewUrlParser: true,
            useFindAndModify: false,
            useUnifiedTopology: true,

            bufferCommands: false, // Disable mongoose buffering
            bufferMaxEntries: 0 // and MongoDB driver buffering
        });

        await conn;

        for (const [key, value] of Object.entries(CollectionName)) {
            conn.model(value, require(`../../schema/${value}`));
        }
    } else {
        logger.writeActionLog("MongoDB", "Used cache");

        await conn; // <-- add this
    }

@vkarpov15 vkarpov15 modified the milestones: 5.10.12, 5.10.13, 5.10.14 Nov 4, 2020
@vkarpov15 vkarpov15 modified the milestones: 5.10.14, 5.10.15, 5.10.16 Nov 12, 2020
@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Nov 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

3 participants