loopback icon indicating copy to clipboard operation
loopback copied to clipboard

generator-loopback: possible EventEmitter memory leak detected

Open glesage opened this issue 12 years ago • 17 comments

While creating properties for a model during yo loopback:model I got this error:

Let's add another order property.
Enter an empty property name when done.
[?] Property name: state
   invoke   loopback:property
(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
/home/jeff/.nvm/v0.11.13/lib/node_modules/generator-loopback/node_modules/loopback-workspace/node_modules/longjohn/dist/longjohn.js:185
        throw e;
              ^
Console.trace (console.js:95:17),ReadStream.EventEmitter.addListener (events.js:179:15),ReadStream.Readable.on (_stream_readable.js:667:33),new Interface (readline.js:124:11),Object.exports.createInterface (readline.js:38:10),Object.Interface.createInterface (/home/jeff/.nvm/v0.11.13/lib/node_modules/yo/node_modules/yeoman-generator/node_modules/inquirer/node_modules/readline2/index.js:39:21),PromptUI.UI (/home/jeff/.nvm/v0.11.13/lib/node_modules/yo/node_modules/yeoman-generator/node_modules/inquirer/lib/ui/baseUI.js:23:40),new PromptUI (/home/jeff/.nvm/v0.11.13/lib/node_modules/yo/node_modules/yeoman-generator/node_modules/inquirer/lib/ui/prompt.js:26:8)

Thanks


Clean 14.04 server x64 NodeJS 0.11.13 --> installed with nvm 0.12.1 npm 1.4.9 yo 1.2

glesage avatar Jul 25 '14 06:07 glesage

I am not able to reproduce the problem. How many properties have you added to your model this way? Do you get the same error when running yo loopback:property?

bajtos avatar Jul 25 '14 06:07 bajtos

BTW v1.1.0 of yo loopback uses a newer version of yeoman-generator which introduced a bug that prevents yo loopback:model from running yo loopback:property: https://github.com/yeoman/generator/issues/589

The workaround is to repeatedly call yo loopback:property from command-line.

bajtos avatar Jul 25 '14 07:07 bajtos

Hi @bajtos, I just had 4 or 5 properties built, was attempting another one...

Thanks for the info, strange though because it did seem like I ran the yo loopback:property couple times just fine before it broke...

glesage avatar Jul 25 '14 21:07 glesage

There seems to be a second place where this error arises:

$ slc run
(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at EventEmitter.addListener (events.js:179:15)
    at EventEmitter.once (events.js:204:8)
    at createListener (.../central/node_modules/loopback-datasource-juggler/lib/datasource.js:395:19)
    at DataSource.defineRelations (.../central/node_modules/loopback-datasource-juggler/lib/datasource.js:443:9)
    at DataSource.setupDataAccess (.../central/node_modules/loopback-datasource-juggler/lib/datasource.js:491:8)
    at DataSource.attach (.../central/node_modules/loopback-datasource-juggler/lib/datasource.js:702:8)
    at Function.ModelClass.attachTo (.../central/node_modules/loopback-datasource-juggler/lib/model-builder.js:278:16)
    at Object.registry.configureModel (.../central/node_modules/loopback/lib/registry.js:173:15)
    at configureModel (.../dev/central/node_modules/loopback/lib/application.js:401:12)
    at Function.app.model (.../dev/central/node_modules/loopback/lib/application.js:139:5)
Browse your REST API at http://localhost:3000/explorer
Web server listening at: http://localhost:3000/

However, in this case execution continues just fine... no crash

glesage avatar Jul 28 '14 16:07 glesage

How many relations do you have? The code is trying to set up once listeners so that relations can be configured after the model has been attached to a data source. Maybe we should increase the max number of listeners in this case.

raymondfeng avatar Jul 28 '14 17:07 raymondfeng

Oh.. lots of relations indeed. I've been looking for how to increase that number but what is it by default?

glesage avatar Jul 28 '14 19:07 glesage

I'll increase the max listeners in base model class that functions as the event emitter.

raymondfeng avatar Jul 28 '14 19:07 raymondfeng

Great thanks!

glesage avatar Jul 28 '14 19:07 glesage

The fix was released in loopback v2.2.0.

bajtos avatar Aug 12 '14 12:08 bajtos

@bajtos @raymondfeng

I am still facing the issue. Is loopback trying to generate all the APIs at the boot because its is very slow or it is looking the model-config.json to generate APIs for the public api only?

[2015-06-24 13:53:13.218] [ERROR] console - (node) warning: possible EventEmitter memory leak detected. 33 dataAccessConfigured listeners added. Use emitter.setMaxListeners() to increase limit.
[2015-06-24 13:53:13.221] [ERROR] console - Trace
    at EventEmitter.addListener (events.js:179:15)
    at EventEmitter.once (events.js:204:8)
    at createListener (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/datasource.js:410:19)
    at /Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/datasource.js:472:9
    at Array.forEach (native)
    at DataSource.defineRelations (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/datasource.js:442:28)
    at DataSource.setupDataAccess (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/datasource.js:521:8)
    at DataSource.attach (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/datasource.js:737:8)
    at Function.ModelClass.attachTo (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/model-builder.js:331:16)
    at Registry.configureModel (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback/lib/registry.js:238:15)
    at configureModel (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback/lib/application.js:411:16)
    at EventEmitter.app.model (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback/lib/application.js:132:5)
    at /Users/Coderider/TCM/Dev/api-server/node_modules/loopback-boot/lib/executor.js:172:9
    at Array.forEach (native)
    at setupModels (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-boot/lib/executor.js:168:23)
    at execute (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-boot/lib/executor.js:32:3)
INFO strong-agent not profiling, agent metrics requires a valid license.
Please contact [email protected] for assistance.

I have lot of Models around 700 and lots of relations. When I had around 500 models it was working now suddnly it is also throwing errors like this Not sure if it is related.

{
error: {
name: "TypeError"
status: 500
message: "Cannot read property 'type' of undefined"
stack: "TypeError: Cannot read property 'type' of undefined at MsSQL.toDatabase (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-connector-mssql/lib/mssql.js:497:11) at /Users/Coderider/TCM/Dev/api-server/node_modules/loopback-connector-mssql/lib/mssql.js:786:20 at Array.forEach (native) at MsSQL._buildWhere (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-connector-mssql/lib/mssql.js:770:22) at MsSQL.buildWhere (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-connector-mssql/lib/mssql.js:854:20) at MsSQL.all (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-connector-mssql/lib/mssql.js:666:17) at /Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/dao.js:1399:19 at doNotify (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/observer.js:93:49) at doNotify (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/observer.js:93:49) at doNotify (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/observer.js:93:49) at doNotify (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/observer.js:93:49) at Function.ObserverMixin._notifyBaseObservers (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/observer.js:116:5) at Function.ObserverMixin.notifyObserversOf (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/observer.js:91:8) at Function.ObserverMixin._notifyBaseObservers (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/observer.js:114:15) at Function.ObserverMixin.notifyObserversOf (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/observer.js:91:8) at Function.ObserverMixin._notifyBaseObservers (/Users/Coderider/TCM/Dev/api-server/node_modules/loopback-datasource-juggler/lib/observer.js:114:15)"
}-
}

FoysalOsmany avatar Jun 24 '15 07:06 FoysalOsmany

@f0ysal If you are using [email protected], then it loads all models configured in model-config.json, regardless of whether they are public or not.

Regarding possible EventEmitter memory leak detected, please open a new GH issue and provide specific steps how we can reproduce the problem ourselves.

Regarding TypeError: Cannot read property 'type' of undefined at MsSQL.toDatabase, this may be a bug caused by recent changes in the connector, please open a new GH issue in loopback-connector-mssql.

bajtos avatar Jun 24 '15 14:06 bajtos

For the EventEmitter warning, it is expected in your case as there are many relations. We use the event listener to wait for related models to be loaded before creating relations. We could increase the number of max listeners to disable the warning.

For the TypeError, most likely you need to run npm up to update the dependencies.

raymondfeng avatar Jun 24 '15 15:06 raymondfeng

How and where to set emitter. setMaxListeners ? Please help

AkankS19 avatar Sep 08 '15 12:09 AkankS19

setMaxListeners is hard-coded here. For our purposes (I work with Foysal) we do fine by increasing this to 64. I guess it should be configurable?

zamb3zi avatar Mar 15 '16 04:03 zamb3zi

@zamb3zi Would you like to submit a PR?

superkhau avatar Mar 15 '16 07:03 superkhau

I am encountering this too, and after looking a bit into what's causing it, here my assessment:

When using async functions in auto-migration, the used data-sources will end up observing as many 'connected' events as there are models, because of these lines here in SQLConnector:

https://github.com/strongloop/loopback-connector/blob/ffa776cf6a0e401ee0427ce42e9f5e24cb124bdf/lib/sql.js#L620-L624

if (!this.dataSource.connected) {
  return this.dataSource.once('connected', function() {
    self.execute(sql, params, options, callback);
  });
}

Here a simple solution to prevent the error while still having a reasonable value for the maximum of listeners:

const numModels = Object.keys(app.models).length
for (let dataSource of Object.values(app.dataSources)) {
  dataSource.setMaxListeners(numModels)
}

Perhaps it is worth putting something like this into LoopBack itself?

lehni avatar Jul 21 '17 13:07 lehni

Perhaps it is worth putting something like this into LoopBack itself?

That makes sense to me. I think you should use something like this to obtain the number of models possible associated with a datasource:

Object.keys(dataSource.models).length;

Few alternatives:

  • increase the maximum number of listeners whenever a new model is attached to a datasource
  • when creating a new dataSource, set max listeners to a hardcoded value. We use the value 32 for models - see here, the code points back to this issue #404.

bajtos avatar Jul 21 '17 15:07 bajtos