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

"MaxListenersExceededWarning: Possible EventEmitter memory leak detected" when creating models with autoCreate: true before mongoose.connect() #9778

Closed
Janoyan opened this issue Jan 7, 2021 · 7 comments
Assignees
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Janoyan
Copy link

Janoyan commented Jan 7, 2021

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

What is the current behavior?
When creating more than 10 models MaxListenersExceededWarning appears

If the current behavior is a bug, please provide the steps to reproduce.
Create more than 10 models like this:

mongoose.model<DocumentType, ModelType>(name, schema);

If you are using TypeScript, please include your tsconfig.json

{
  "compilerOptions": {
    "target": "es5"  ,
    "module": "commonjs"  ,
    "declaration": true  ,
    "declarationMap": true  ,
    "sourceMap": true  ,
    "outDir": "./dist"  ,
    "strict": true  ,
    "baseUrl": "./",
    "paths": {
      "@sm-errors": [
        "./common-library/errors"
      ],
      "@sm-microsoft-graph-api": [
        "./common-library/microsoft-graph-api"
      ],
      "@sm-shared-models": [
        "./common-library/shared-models"
      ],
      "@sm-shared-services": [
        "./common-library/shared-services"
      ],
      "@sm-lib": [
        "./common-library/lib"
      ]
    },
    "esModuleInterop": true  ,
    "skipLibCheck": true  ,
    "forceConsistentCasingInFileNames": true
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}

What is the expected behavior?
Should create more models without warnings

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

mongoose: "^5.9.27",
Node 12
MongoDB 4.2.2

Currently I solved the issue by calling the following function each time before creating models:

mongoose.connection.setMaxListeners(MAX_LISTENERS_COUNT);

but I think there should be another solution for this.

@vkarpov15 vkarpov15 added this to the 5.11.12 milestone Jan 8, 2021
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Jan 8, 2021
@vkarpov15 vkarpov15 modified the milestones: 5.11.12, 5.11.x Jan 13, 2021
@deser
Copy link

deser commented Jan 14, 2021

It also happens when I use autoCreate: true on Schemas. Without that option it is ok

@deser
Copy link

deser commented Jan 25, 2021

Will someone look at this?

@IslandRhythms
Copy link
Collaborator

import { model, Schema, Model, Document, connect } from 'mongoose';

interface IUser extends Document {
  email: string;
  firstName: string;
  lastName: string;
}

const UserSchema: Schema = new Schema({
  email: { type: String, required: true },
  firstName: { type: String, required: true },
  lastName: { type: String, required: true }
});

const User: Model<IUser> = model('User', UserSchema);
const test: Model<IUser> = model('test', UserSchema);
const test1: Model<IUser> = model('test1', UserSchema);
const test2: Model<IUser> = model('test2', UserSchema);
const test3: Model<IUser> = model('test3', UserSchema);
const test4: Model<IUser> = model('test4', UserSchema);
const test5: Model<IUser> = model('test5', UserSchema);
const test6: Model<IUser> = model('test6', UserSchema);
const test7: Model<IUser> = model('test7', UserSchema);
const test8: Model<IUser> = model('test8', UserSchema);
const test9: Model<IUser> = model('test9', UserSchema);
(async function() {
  await connect('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

  const user: IUser = await User.create({
    email: 'bill@microsoft.com',
    firstName: 'Bill',
    lastName: 'Gates'
  });

  console.log('Done', user.email);
})();

@IslandRhythms IslandRhythms added can't reproduce Mongoose devs have been unable to reproduce this issue. 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 Feb 22, 2021
@IslandRhythms IslandRhythms removed this from the 5.11.18 milestone Feb 22, 2021
@Janoyan
Copy link
Author

Janoyan commented Feb 23, 2021

@IslandRhythms @vkarpov15 just add autoCreate: true option to UserSchema and the warning will appear.

image

@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const UserSchema = mongoose.Schema({
  email: { type: String, required: true },
  firstName: { type: String, required: true },
  lastName: { type: String, required: true }
}, {autoCreate: true});

const User = mongoose.model('User', UserSchema);
const test = mongoose.model('test', UserSchema);
const test1 = mongoose.model('test1', UserSchema);
const test2 = mongoose.model('test2', UserSchema);
const test3 = mongoose.model('test3', UserSchema);
const test4 = mongoose.model('test4', UserSchema);
const test5 = mongoose.model('test5', UserSchema);
const test6 = mongoose.model('test6', UserSchema);
const test7 = mongoose.model('test7', UserSchema);
const test8 = mongoose.model('test8', UserSchema);
const test9 = mongoose.model('test9', UserSchema);

(async function() {
  await mongoose.connect('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });

  const user = await User.create({
    email: 'bill@microsoft.com',
    firstName: 'Bill',
    lastName: 'Gates'
  });

  console.log('Done', user.email);
})();

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Feb 23, 2021
@IslandRhythms
Copy link
Collaborator

This problem does not exist on the most recent version of mongoose.

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Mar 1, 2021
@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Mar 23, 2021
@IslandRhythms IslandRhythms removed the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Mar 23, 2021
@IslandRhythms IslandRhythms reopened this Mar 23, 2021
@IslandRhythms
Copy link
Collaborator

@vkarpov15 its very finicky but I can reproduce it but I'm not sure what triggers it.

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Mar 23, 2021
@IslandRhythms IslandRhythms added this to the 5.12.3 milestone Mar 24, 2021
@vkarpov15 vkarpov15 changed the title MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 open listeners added to [NativeConnection]. Use emitter.setMaxListeners() to increase limit "MaxListenersExceededWarning: Possible EventEmitter memory leak detected" when creating models with autoCreate: true before mongoose.connect() Mar 31, 2021
vkarpov15 added a commit that referenced this issue Apr 11, 2021
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