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

Initial connection returns other errors than just "failed to connect to server X on first connect" #7768

Closed
thernstig opened this issue May 6, 2019 · 3 comments · Fixed by #7871
Labels
underlying library issue This issue is a bug with an underlying library, like the MongoDB driver or mongodb-core

Comments

@thernstig
Copy link

thernstig commented May 6, 2019

Do you want to request a feature or report a bug?
bug (I am not 100% sure it is a bug though)

What is the current behavior?

Consider this example:

const log4js = require('log4js');
const mongoose = require('mongoose');

const logger = log4js.getLogger('mongodb');

const connections = [];

const ip = 'mongodb';
const port = 27017;
const uri1 = `mongodb://${ip}:${port}/$test`;

const options = {
  useNewUrlParser: true,
  useCreateIndex: true,
  reconnectTries: Number.MAX_VALUE,
  reconnectInterval: 5000
};

[uri1].forEach(uri => {
  const db = mongoose.createConnection(uri, options);
  db.catch(() => {}); // handle 'Unhandled promise rejection' warning
  connections.push(db);

  db.on('connected', () => {
    logger.info(`MongoDB client: connection initialized to ${uri}`);
  });

  db.on('error', err => {
    if (
      err.message &&
      err.message.match(/failed to connect to server .* on first connect/)
    ) {
      setTimeout(() => {
        logger.info(`MongoDB client: connection attempt to ${uri}`);
        db.openUri(uri).catch(() => {});
      }, 5000);
    } else {
      logger.error(`MongoDB client: error for database ${uri}: ${err}`);
    }
  });
});
module.exports = {
  db1: connections[0]
};

This worked fine in mongoose 5.0.7 if mongodb 3.6.3 had not been started. However upgrading to mongoose 5.5.2 I get new failures. I've seen both Error: getaddrinfo ENOTFOUND mongodb mongodb:27017 and Error: connect ECONNREFUSED 10.0.51.11:27017.

All this is launched with docker stack, so sometimes the above code is launched before the mongodb docker container. Hence why I need to do this check at all. But I do not want to add extra checks for all kind of errors that can happen like the ones above.

Is it to be exected that ENOTFOUND and ERCONNREFUSED can happen in these scenarios, or should mongoose always report failed to connect to server .* on first connect during the first start-up attempts?

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

  1. Put the above code in a file and build it in a docker container.
  2. Start docker compose (or a docker stack) with mongodb as the only service and the built docker in step 1 as a service and start up the stack.

What is the expected behavior?
I was expecting failed to connect to server .* on first connect to always be returned during initial connect to mongodb in case the mongodb service is not completely up, not matter the state the service is in.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: 10.13.0
Mongoose: 5.5.2
MongoDB: 3.6.3

@thernstig
Copy link
Author

thernstig commented May 20, 2019

I still have no idea if this is a bug or not, but I re-wrote my code to better account for any errors on initial connection.

@vkarpov15
Copy link
Collaborator

We'll take a look and see if this is expected behavior or not. But realistically, we don't use docker, so if it turns out this issue only happens with docker we'll have to close it.

@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label May 26, 2019
@vkarpov15 vkarpov15 added this to the 5.5.12 milestone May 26, 2019
@vkarpov15 vkarpov15 removed this from the 5.5.12 milestone May 27, 2019
@vkarpov15 vkarpov15 added underlying library issue This issue is a bug with an underlying library, like the MongoDB driver or mongodb-core and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels May 27, 2019
@vkarpov15
Copy link
Collaborator

I opened a PR in the mongodb driver for this ☝️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
underlying library issue This issue is a bug with an underlying library, like the MongoDB driver or mongodb-core
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants