-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
throws MaxListenersExceededWarning #2198
Comments
I have the same issue with postgreSQL |
Have the same problem with postgresql. I did not ever test that with other dbs. |
The juggler uses events to manage relations internally. This is a warning message only, it does not crash the application. https://nodejs.org/api/events.html#events_eventemitter_defaultmaxlisteners You can reset the max listeners as a workaround for now. |
We will Investigate and also increase the max limit on the event emitter, and provide a simple unit test. |
Ideally, we should find out the root cause - why is LB4 registering so many event listeners and LB2/LB3 does not? Can we improve LB4 code to avoid so many listeners being registered? |
Yes, fix the root cause, not treat the symptom. |
I'm getting the same error and since I don't have any special logic added to the project I believe this is an internal issue of loopback. |
@reyraa thank you for chiming in and confirming the problem. What we really need now is a small application we can use to reproduce the issue on our machines. See https://loopback.io/doc/en/contrib/Reporting-issues.html#loopback-4x-bugs |
@reyraa Can you share the sample app with us to help reproduce the issue? |
Found a good article on this error : https://www.stefanjudis.com/today-i-learned/nodejs-sends-warnings-when-you-add-too-many-listeners-to-an-event-emitter/ |
There is a way to trace where it came from : nodejs/help#1051 |
@Joel-Raju , @sbacem , @zhaoqin-github , @reyraa If any of you can provide a repository with an application ( and exact steps) that can reproduce this I have placed an application with multiple belongsTo and hasMany relations between in an effort to create many models as @Joel-Raju did , but I wasn't able to reproduce the issue. The application connects to postgresql running in a docker container. Please see the README.md . |
Also, if you still have your application showing the warning message, you can try using the For example:
|
@emonddr
I hope that helps. |
Definitely some migration going on for postgres connector: This line https://github.com/strongloop/loopback-connector-postgresql/blob/master/lib/migration.js#L22 is responsible for the debugging output I am getting :
Notice the warning appears before any of this migration output:
|
Observations: /reyraa-api/server/boot/schemaUpdate.js async.eachSeries(datasources, function(dsName, cb) { which calls: /reyraa-api/node_modules/loopback-datasource-juggler/lib/datasource.js DataSource.prototype.isActual = function(models, cb) { which calls: /reyraa-api/node_modules/loopback-connector/lib/sql.js SQLConnector.prototype.isActual = function(models, cb) { which calls: SQLConnector.prototype.getTableStatus = function(model, cb) { which calls : /reyraa-api/node_modules/loopback-connector-postgresql/lib/migration.js PostgreSQL.prototype.showFields = function(model, cb) {
} which calls : /reyraa-api/node_modules/loopback-connector/lib/sql.js SQLConnector.prototype.execute = function(sql, params, options, callback) { ... var self = this; } The function 'function()' is added to the datasource (which inherits from EventEmitter) for the If the number of models in model-config.json is > 10, then the warning message appears. |
I think we have found out the root cause. Can we work on the fix based on #2198 (comment) and #2198 (comment)? |
One option is to modify: copied here for convenience:
and change it to:
and users have to add a new property
|
@raymondfeng , what are your thoughts? |
|
Since the datasource is an event emitter , setting the max listeners means setting the maxListeners for each event |
Based on the analysis in #2198 (comment), it's my understanding that we start automigration before the connection is set up. During automigration, we check whether model tables are actual ( Instead of tweaking In fact, I have recently made such change in loopbackio/loopback-datasource-juggler#1756: I would hope the change should have fixed the problem with maxListeners too. @emonddr could you please verify? |
(IMO, we should not need to install N event listeners when migrating N models - a single listener should be enough.) |
@bajtos Please note we defer the SQL execution for each statement when the datasource is not connected - see https://github.com/strongloop/loopback-connector/blob/master/lib/sql.js#L626. This code cannot coordinate multiple operations with one listener. Of course, we can optimize the migration by checking the datasource |
I have confirmed that https://github.com/strongloop/loopback-connector/blob/master/lib/sql.js#L626
is never entered when migration is happening in an LB4 application. By the time that migration-related SQL is executed, the datasource I tried running the migration from
and never encountered the warning, despite my app having many,many models. |
@bajtos @raymondfeng , the particular change you made was in the @reyraa kindly provided his LB3 application, and his server/boot/schemaUpdate.js Not certain if this a typical schema update for an LB3 application. @raymondfeng @bajtos can you confirm? Regardless, with @b-admike 's help, I created my own local branch (using 3.x branch as a base, and cherry-picking your commit ). Using this code (with your commit), I still get the warning about max listeners. When I add my suggested code change to datasource.js mentioned earlier, then I do NOT get the warning anymore. |
Replying to my own question. https://loopback.io/doc/en/lb3/Creating-a-database-schema-from-models.html mentions this code:
which is identical to https://github.com/reyraa/reyraa-api/blob/develpment/server/boot/schemaUpdate.js#L5 . So looks like this is following a recommended approach. Not sure why @bajtos commit mentioned earlier doesn't prevent migration from occurring before a datasource is connected. |
It only happens when the DB connection takes time to establish, for example, a remote DB over slow network. Did you test against a local DB? |
In general, DB operations can be performed upon the startup of an application. Such requests such as data loading, schema migration, data seeding, or other things. Using |
@raymondfeng , I am using a docker image for postgresql. So yes, it is on my local laptop.
|
So I've created a PR to merge into 3.x branch I cherry-picked this commit from master: Miroslav's commit for automigrate and autoupdate Other commits of interest: I didn't cherry-pick this one, I just fixed the typo in my PR. I also needed to delete a test suite on the new Datasource.execute capability. @dhmlau mentioned to me afterwards that we usually commit to I am just wondering right now, what test can I create to test this fix. I was using @reyraa 's application to test my fix. |
I think this is unnecessary complexity. There is a timer that also removes the 'connected' listeners. So we may run into timing issues when trying to obtain an accurate count of how many are queued. If we simply have the code suggested above, the only problem the user would face is the same warning message concerning max emitters. Then he/she only needs to bump up the number. My 2 cents. |
Create mockup connector from SQL connector. Artificially defer the connection. we should emit a new error message in here |
Closing as loopbackio/loopback-connector#149 and loopbackio/loopback-datasource-juggler#1767 are landed. |
Description
I'm using LB4 with PostgreSQL DataSource. Currently I have 36 models with multiple relations between them. Throws the following warning
(node:39148) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 connected listeners added. Use emitter.setMaxListeners() to increase limit
setup
The text was updated successfully, but these errors were encountered: