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

Unable to use global models created with connections returned by useDb() #11240

Closed
vidjul opened this issue Jan 18, 2022 · 4 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@vidjul
Copy link

vidjul commented Jan 18, 2022

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

  • Report a bug

What is the current behavior?
We are using global model and connections in our codebase. Some of these models are being created using connections returned by useDb. Using those models after closing initial connection & reconnecting to it throw err. Models created with mongoose.connection works fine though.

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

  • Bug can be reproduced using these scripts:

order.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const OrderSchema = new Schema({
  status: String,
})

// Model created with mongoose.connection
global.Order = mongoose.model('Order', OrderSchema);

location.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const LocationSchema = new Schema({
  name: String,
})

// Model created with useDb connection
global.Location = global.db2.model('Location', LocationSchema);

main.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const connect = async () => {
  await mongoose.connect('mongodb://localhost:27017/order', { useNewUrlParser: true });

  global.db1 = mongoose.connection;
  global.db2 = global.db1.useDb('location')
}

async function run() {
  await connect();

  require('./order.js');
  require('./location.js');

  await mongoose.connection.dropDatabase();

  // Both of these works
  const location = await Location.create({name: 'Paris'})
  const order = await Order.create({status: 'PAID'})

  // We close the connection & reconnect to them
  await mongoose.connection.close();
  await connect();

  console.log(await Order.find({})); // This works & displays results
  console.log(await Location.find({})); // This throws MongoError: pool destroyed

  console.log('done');

  await mongoose.connection.close();
}

run();

What is the expected behavior?

  • Location.find({}) should return results just like Order.find({})

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

  • Node v14.17.6
  • Mongoose v5.13.14
  • MongoDB v3.4.10
@IslandRhythms
Copy link
Collaborator

What is the error message?

@vidjul
Copy link
Author

vidjul commented Jan 19, 2022

Although the issue is the same (we cannot use Location#find) we have two different error messages when executing our project or the repro scenario

Main project error:

MongoError: Topology is closed, please connect
    at processWaitQueue (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/core/sdam/topology.js:1028:42)
    at NativeTopology.selectServer (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/core/sdam/topology.js:448:5)
    at executeWithServerSelection (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/operations/execute_operation.js:131:12)
    at /home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/operations/execute_operation.js:70:9
    at maybePromise (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/utils.js:692:3)
    at executeOperation (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/operations/execute_operation.js:34:10)
    at Cursor._initializeCursor (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/core/cursor.js:541:7)
    at Cursor._initializeCursor (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/cursor.js:190:11)
    at nextFunction (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/core/cursor.js:744:10)
    at Cursor._next (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/core/cursor.js:209:5)
    at /home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/cursor.js:253:14
    at maybePromise (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/utils.js:692:3)
    at Cursor.next (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/cursor.js:238:12)
    at FindOneOperation.execute (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/operations/find_one.js:29:14)
    at /home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/operations/execute_operation.js:72:19
    at maybePromise (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/utils.js:692:3)
    at executeOperation (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/operations/execute_operation.js:34:10)
    at Collection.<anonymous> (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/collection.js:1114:12)
    at Collection.deprecated [as findOne] (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/utils.js:611:15)
    at NativeCollection.<computed> [as findOne] (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:244:33)
    at NodeCollection.findOne (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mquery/lib/collection/node.js:42:19)
    at model.Query.Query.findOne (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mquery/lib/mquery.js:2024:20)
From previous event:
    at promiseOrCallback (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
    at model.Query.exec (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongoose/lib/query.js:4509:10)
    at Function.LocationSchema.statics.findOrGenerate (/home/vidushan/work/lba/LaBelleAssiette/core/models/location.js:40:52)
    at Function.schema.statics.getLocation (/home/vidushan/work/lba/LaBelleAssiette/core/models/order/managers/location.js:33:31)
    at Migration.up (/home/vidushan/work/lba/LaBelleAssiette/migrations/1641312856758-order-from-gifts-requests.js:162:36)

On mongoose 5.12.7, the version we were using before 5.13.14, we had MongooseError: Operationlocations.findOne() buffering timed out after 10000ms

Repro scenario error:

 MongoError: pool destroyed
    at Pool.write (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/core/connection/pool.js:844:8)
    at _command (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/core/wireprotocol/command.js:149:10)
    at command (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/core/wireprotocol/command.js:28:5)
    at Object.query (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/core/wireprotocol/query.js:70:3)
    at Server.query (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/core/topologies/server.js:649:16)
    at FindOperation.execute (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/operations/find.js:38:12)
    at /home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/operations/execute_operation.js:148:15
    at Server.selectServer (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/core/topologies/server.js:837:3)
    at Server.selectServer (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/topologies/topology_base.js:342:32)
    at executeWithServerSelection (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/operations/execute_operation.js:131:12)
    at /home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/operations/execute_operation.js:70:9
    at maybePromise (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/utils.js:692:3)
    at executeOperation (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/operations/execute_operation.js:34:10)
    at Cursor._initializeCursor (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/core/cursor.js:541:7)
    at Cursor._initializeCursor (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/cursor.js:190:11)
    at nextFunction (/home/vidushan/work/lba/LaBelleAssiette/node_modules/mongodb/lib/core/cursor.js:744:10)

We've attempted to use mongoose 6 but on this version, the script hangs and don't get through #connect. I've looked into MongoDB logs & noticed that connections are created and closed endlessly

@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');
const {Schema} = mongoose;



const connect = async () => {
    await mongoose.connect('mongodb://localhost:27017/order', { useNewUrlParser: true });
  
    global.db1 = mongoose.connection;
    global.db2 = global.db1.useDb('location')
  }
  
async function run() {
    await connect();
    const OrderSchema = new Schema({
        status: String,
      })
      
      global.Order = mongoose.model('Order', OrderSchema);
      
      const LocationSchema = new Schema({
        name: String,
      })
      
      global.Location = global.db2.model('Location', LocationSchema);
  
    await mongoose.connection.dropDatabase();
  
    // Both of these works
    const location = await Location.create({name: 'Paris'})
    const order = await Order.create({status: 'PAID'})
  
    // We close the connection & reconnect to them
    await mongoose.connection.close();
    await connect();
  
    console.log(await Order.find({})); // This works & displays results
    console.log(await Location.find({})); // This throws MongoError: pool destroyed
  
    console.log('done');
  
    await mongoose.connection.close();
  }
  
  run();

happens on 6.1.7 as well

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jan 19, 2022
@vkarpov15 vkarpov15 added this to the 6.1.9 milestone Jan 23, 2022
@Uzlopak
Copy link
Collaborator

Uzlopak commented Mar 6, 2022

If I comment out the three lines here

for (const db of this.otherDbs) {

then it seems to work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

4 participants