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

Error Plugin did not start in time: '@fastify/mongodb'. You may have forgotten to call 'done' function or to resolve a Promise #224

Closed
2 tasks done
hoanghiep1x0 opened this issue Sep 20, 2023 · 19 comments

Comments

@hoanghiep1x0
Copy link

hoanghiep1x0 commented Sep 20, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.x

Plugin version

^6.0.1

Node.js version

16.0.9

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

ventura 13.5.2

Description

Why does connect localhost:27017 work.
but connect to mongo with the address mongodb+srv://user:pass@cluster0.m9frj.mongodb.net/
then get an error

Plugin file

  'use strict'

  const fp = require('fastify-plugin')
  const fastifyMongo = require('@fastify/mongodb')

  module.exports = fp(async function datasourcePlugin(fastify, opts, next) {

    try {
      fastify.register(fastifyMongo, {
        forceClose: true,
        connectTimeoutMS: 10000,
        url: fastify.secrets.MONGO_URL
      });
    } catch (err) {
      next(err);
    }
  }, {
    dependencies: ['application-config']
  })

Plugin did not start in time: '@fastify/mongodb'. You may have forgotten to call 'done' function or to resolve a Promise

Steps to Reproduce

  1. config plugin and run with localhost => work
  2. config with mongolab => error

Expected Behavior

Plugin did not start in time: '@fastify/mongodb'. You may have forgotten to call 'done' function or to resolve a Promise
    at Timeout._onTimeout (/Users/admin/Desktop/app/docker/service-data/node_modules/avvio/plugin.js:122:19)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7) {
  code: 'AVV_ERR_READY_TIMEOUT',
  fn: <ref *1> [AsyncFunction: fastifyMongodb] {
    default: [Circular *1],
    fastifyMongodb: [Circular *1],
    ObjectId: [Function: ObjectId] {
      getInc: [Function (anonymous)],
      generate: [Function (anonymous)],
      createPk: [Function (anonymous)],
      createFromTime: [Function (anonymous)],
      createFromHexString: [Function (anonymous)],
      isValid: [Function (anonymous)],
      fromExtendedJSON: [Function (anonymous)],
      index: 16644978
    },
    [Symbol(skip-override)]: true,
    [Symbol(fastify.display-name)]: '@fastify/mongodb',
    [Symbol(plugin-meta)]: { fastify: '4.x', name: '@fastify/mongodb' }
  }
}
@Uzlopak
Copy link
Contributor

Uzlopak commented Sep 20, 2023

This indicates that the connection to the db could not be established, as the plugin never resolves.

@hoanghiep1x0
Copy link
Author

I have configured the db on mongo lap correctly because network open is 0.0.0.0 and user + password is correct. But I don't understand why it doesn't work

@hoanghiep1x0
Copy link
Author

@Uzlopak But when working on localhost:27017 it runs again, can you help me? I just learned about fastify.

@climba03003
Copy link
Member

I am closing the issue because there is no indication the problem related to fastify.
You can still comment on the issue or seek for helps on the Discord channel.

From my point of view, it clearly you have some connection error to your database, please try the same connection string with MongoDB Compass to see if your computer allowed to connect.

@hoanghiep1x0
Copy link
Author

hoanghiep1x0 commented Sep 21, 2023

I am 100% sure the error is from @fastify/mongodb

Here is the code I run perfectly fine with mongoose

const mongoose = require("mongoose");

(async () => {
    try {
        await mongoose.connect('mongodb+srv://demo:demo@cluster0.m9frj.mongodb.net/')
        console.log("connect success");
    } catch (err) {
        console.log(err);
    }
})()

and here is the code that runs with fastify

'use strict'

const fp = require('fastify-plugin')
const fastifyMongo = require('@fastify/mongodb')

module.exports = fp(async function databaseConnect(fastify, opts) {
  fastify.register(fastifyMongo, {
    forceClose: true,
    url: 'mongodb+srv://demo:demo@cluster0.m9frj.mongodb.net/'
  })
}, {
  dependencies: ['application-config']
})

The result is the same as the error I posted

@Eomm
Copy link
Member

Eomm commented Sep 21, 2023

was about to say that in your first code version you are mixin async and next, the latter version of the code should be fine.

I think it may be a firewall or something.
I would suggest to debug it with:


module.exports = fp(async function databaseConnect(fastify, opts) {
try{
  await fastify.register(fastifyMongo, {
    forceClose: true,
    url: 'mongodb+srv://demo:demo@cluster0.m9frj.mongodb.net/'
  })
} catch (err) {
console.log(err)
}
}, {
  dependencies: ['application-config']
})

@hoanghiep1x0
Copy link
Author

I still get the same error.
One confusing thing is that it works with localhost:27017

while testing with mongoose new file index.js it works. If it's due to a firewall, mongoose will also not work. Because I am running on the same local machine environment.

@hoanghiep1x0
Copy link
Author


const fp = require('fastify-plugin')
const fastifyMongo = require('@fastify/mongodb')

module.exports = fp(async function databaseConnect(fastify, opts) {
 try {
   await fastify.register(fastifyMongo, {
     forceClose: true,
     url: 'mongodb+srv://demo:demo@cluster0.m9frj.mongodb.net/'
   })
 } catch (err) {
   console.log("ERROR ===>",err)
 }
}, {
 dependencies: ['application-config']
})

Run -> ERROR ===> AvvioError [Error]: Plugin did not start in time: 'databaseConnect-auto-2'. You may have forgotten to call 'done' function or to resolve a Promise
at Timeout._onTimeout. :(

@climba03003
Copy link
Member

climba03003 commented Sep 21, 2023

I suggest you to try the official package mongodb instead of mongoose.

import { MongoClient } from 'mongodb'

const client = new MongoClient('mongodb+srv://demo:demo@cluster0.m9frj.mongodb.net/')
await client.connect()

@hoanghiep1x0
Copy link
Author

Thank. I'm just testing to find the problem and fix it.

@hoanghiep1x0
Copy link
Author

ERROR

'use strict'

const fp = require('fastify-plugin')
const fastifyMongo = require('@fastify/mongodb')

module.exports = fp(function datasourcePlugin(fastify, opts, done) {
  fastify.register(fastifyMongo, {
    forceClose: true,
    url: 'mongodb+srv://demo:demo@cluster0.m9frj.mongodb.net/'
  });
  done();
}, {
  name: 'mongodb-plugin',
  dependencies: ['application-config']
})

A plugin does not fail

'use strict'

const fp = require('fastify-plugin')
const mongoose = require("mongoose");

module.exports = fp(function databaseConnect(fastify, opts, done) {
  mongoose.connect('mongodb+srv://demo:demo@cluster0.m9frj.mongodb.net/').then(() => {
    console.log("connect success");
    return;
  }).catch(err => {
    console.log("ERROR MONGOO CONNECT===>", err);
  })
  done();
}, {
  name: 'plugin-mongoose',
  dependencies: ['application-config']
})

@climba03003
Copy link
Member

climba03003 commented Sep 21, 2023

You are not doing the same as this plugin, and you ignore the connection state by resolving the plugin immediately.
The below one is waiting for the connection properly.

'use strict'

const fp = require('fastify-plugin')
const mongoose = require("mongoose");

module.exports = fp(function databaseConnect(fastify, opts, done) {
  mongoose.connect('mongodb+srv://demo:demo@cluster0.m9frj.mongodb.net/').then(() => {
    console.log("connect success");
    done();
  }).catch(err => {
    console.log("ERROR MONGOO CONNECT===>", err);
    done(err);
  })
}, {
  name: 'plugin-mongoose',
  dependencies: ['application-config']
})

@hoanghiep1x0
Copy link
Author

You are not doing the same as this plugin, and you ignore the connection state by resolving the plugin immediately. The below one is waiting for the connection properly.

'use strict'

const fp = require('fastify-plugin')
const mongoose = require("mongoose");

module.exports = fp(function databaseConnect(fastify, opts, done) {
  mongoose.connect('mongodb+srv://demo:demo@cluster0.m9frj.mongodb.net/').then(() => {
    console.log("connect success");
    done();
  }).catch(err => {
    console.log("ERROR MONGOO CONNECT===>", err);
    done(err);
  })
}, {
  name: 'plugin-mongoose',
  dependencies: ['application-config']
})

I tried but doing the above will get an error.

@climba03003
Copy link
Member

climba03003 commented Sep 22, 2023

I tried but doing the above will get an error.

So, you would need to measure how much time you need to connect to the MongoDB Altas.

Increase the pluginTimeout to 30s or longer, if a single connection required more than 30s than it is absolutely something wrong in your network stack.

@hoanghiep1x0
Copy link
Author

IMG ERROR

@hoanghiep1x0
Copy link
Author

What is network classification error and how to fix it. You can specify the way or area to learn how to fix it

@hoanghiep1x0
Copy link
Author

Thanks I know how to solve the problem maybe this is it. pluginTimeout

@climba03003
Copy link
Member

climba03003 commented Sep 22, 2023

The MongoDB Altas connection consist too many steps.

  1. DNS Resolution
  2. TCP / IP handshake
  3. Authentication

Normally, a connection will be resolved within 1 second. More than 10 second will exist in a slow network.

@hoanghiep1x0
Copy link
Author

hoanghiep1x0 commented Sep 22, 2023

Thank you very much. after fixing with options with pluginTimeout server it is working.

const fastify = Fastify({
  disableRequestLogging: true,
  logger: loggerOptions,
  ajv: {
    customOptions: {
      removeAdditional: 'all'
    }
  },
  connectionTimeout: 60000,
  requestTimeout: 60000,
  pluginTimeout: 90000
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants