Automigrate Memory Leak
(node) warning: possible EventEmitter memory leak detected. 11 connected listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
at DataSource.addListener (events.js:179:15)
at DataSource.once (events.js:204:8)
at MySQL.query (/Users/brunocascio/Documents/Projects/Backend/NodeJS/loopback/WTE/node_modules/loopback-connector-mysql/lib/mysql.js:133:28)
at MySQL.SqlConnector.command (/Users/brunocascio/Documents/Projects/Backend/NodeJS/loopback/WTE/node_modules/loopback-connector-mysql/node_modules/loopback-connector/lib/sql.js:45:15)
at MySQL.SqlConnector.dropTable (/Users/brunocascio/Documents/Projects/Backend/NodeJS/loopback/WTE/node_modules/loopback-connector-mysql/node_modules/loopback-connector/lib/sql.js:411:8)
at /Users/brunocascio/Documents/Projects/Backend/NodeJS/loopback/WTE/node_modules/loopback-connector-mysql/node_modules/loopback-connector/lib/sql.js:389:10
at /Users/brunocascio/Documents/Projects/Backend/NodeJS/loopback/WTE/node_modules/loopback-connector-mysql/node_modules/async/lib/async.js:125:13
at Array.forEach (native)
at _each (/Users/brunocascio/Documents/Projects/Backend/NodeJS/loopback/WTE/node_modules/loopback-connector-mysql/node_modules/async/lib/async.js:46:24)
at Object.async.each (/Users/brunocascio/Documents/Projects/Backend/NodeJS/loopback/WTE/node_modules/loopback-connector-mysql/node_modules/async/lib/async.js:124:9)
Loopback version 2.8.0
LoopBack try to defer a CRUD method invocation if the data source has not been connected to the DB. The scheduled method will be called once the connected event is fired and the listener will be removed. Can you check what's going on with your DB connection?
I'm also getting this warning. Everything continues to run fine, but it's annoying none the less. Using a local DB connection, nothing seems to be wrong there.
I also get this warning when running against a local MySQL instance. The update process seems to run fine.
+1
LoopBack uses new DataSource(...) to create data source synchronously. It takes some time to create a connection pool to the database server. The constructor returns before the connection pool is ready. SQL statements will be deferred using once('connected'). To avoid the warnings, you can run automigrate upon the connected event. For example,
var ds = app.dataSources.mysql;
if(ds.connected) {
ds.automigrate(...);
} else {
ds.once('connected', function() {
ds.automigrate(...);
});
}
@raymondfeng Is this good to close?
I have the same problem with postgres. I tried the solution suggested by @raymondfeng but I still get the same error message.
(node) warning: possible EventEmitter memory leak detected. 11 connected listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
at DataSource.addListener (events.js:179:15)
at DataSource.once (events.js:204:8)
at stillConnecting (/Users/peter/Projects/Project1/node_modules/loopback-connector-postgresql/lib/postgresql.js:147:14)
at PostgreSQL.query (/Users/peter/Projects/Project1/node_modules/loopback-connector-postgresql/lib/postgresql.js:166:7)
at dropTableFun (/Users/peter/Projects/Project1/node_modules/loopback-connector-postgresql/lib/postgresql.js:1333:10)
at PostgreSQL.dropTable (/Users/peter/Projects/Project1/node_modules/loopback-connector-postgresql/lib/postgresql.js:1338:3)
at /Users/peter/Projects/Project1/node_modules/loopback-connector-postgresql/node_modules/loopback-connector/lib/sql.js:389:10
at /Users/peter/Projects/Project1/node_modules/async/lib/async.js:125:13
at Array.forEach (native)
at _each (/Users/peter/Projects/Project1/node_modules/async/lib/async.js:46:24)
Never mind. The suggested solution works fine.
I'm getting the same issue but with Mongodb upon server startup.
(node) warning: possible EventEmitter memory leak detected. 11 connected listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
at DataSource.addListener (events.js:239:17)
at DataSource.once (events.js:265:8)
at DataSource.ready (/Users/petergiannopoulos/Projects/otolane/occ/node_modules/loopback-datasource-juggler/lib/datasource.js:2105:8)
at stillConnecting (/Users/petergiannopoulos/Projects/otolane/occ/node_modules/loopback-datasource-juggler/lib/dao.js:365:18)
at Function.DataAccessObject.create (/Users/petergiannopoulos/Projects/otolane/occ/node_modules/loopback-datasource-juggler/lib/dao.js:160:27)
at Object.eval [as forward](eval at recompile %28/Users/petergiannopoulos/.nvm/versions/node/v4.2.1/lib/node_modules/strongloop/node_modules/strong-supervisor/node_modules/strong-agent/lib/proxy.js:245:15%29,
@petergi Did you try the solution provided by @raymondfeng above?
Yeah no dice. But I tracked it down to using faker on a before hook. It's what seems to trigger the warning. I was using faker to generate fake data to work with.
Thanks!
Sent from my iPhone
On Oct 16, 2015, at 9:46 PM, Simon Ho [email protected] wrote:
@petergi Did you try the solution provided by @raymondfeng above?
— Reply to this email directly or view it on GitHub.
@petergi Can you provide a link to a test project on GitHub? See https://github.com/strongloop/loopback/wiki/Reporting-issues#bug-report
I wrote a simple data-fixture script:
var server = require('./server');
var ds = server.dataSources.postgre;
var fs = require("fs");
var fixtures = [
'some',
'model',
'names'
];
function migrate(modelName, data) {
var model = server.models[modelName];
var count = data.length;
ds.automigrate(modelName, function(er){
if (er) throw er;
data.forEach(function(item) {
model.create(item, function(er, result) {
if (er) {
throw er;
console.log(er);
return;
}
console.log('Record created:', result);
count--;
if(count === 0) {
console.log('done');
ds.disconnect();
}
});
});
});
}
function readData(modelName) {
var content = fs.readFileSync('data-fixtures/' + modelName + '.json');
// Define to JSON type
return JSON.parse(content);
}
for (var i = 0; i < fixtures.length; i++) {
var data = readData(fixtures[i]);
migrate(fixtures[i], data);
}
the script runs fine, and all looks fine in the db, but I am getting
_(node) warning: possible EventEmitter memory leak detected. 11 connected listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
at DataSource.addListener (events.js:179:15)
at DataSource.once (events.js:204:8)
at migrate (/home/.../strongloop-test/server/data-fixture.js:152:8)
at Object.
The solution https://github.com/strongloop/loopback/issues/1186#issuecomment-111934355 by @raymondfeng does not solve the problem.
// it is ugly, just for testing
function migrate(modelName, data) {
var model = server.models[modelName];
var count = data.length;
if(ds.connected) {
ds.automigrate(modelName, function(er){
if (er) throw er;
data.forEach(function(item) {
model.create(item, function(er, result) {
if (er) {
throw er;
console.log(er);
return;
}
console.log('Record created:', result);
count--;
if(count === 0) {
console.log('done');
ds.disconnect();
}
});
});
});
} else {
ds.once('connected', function() {
ds.automigrate(modelName, function(er){
if (er) throw er;
data.forEach(function(item) {
model.create(item, function(er, result) {
if (er) {
throw er;
console.log(er);
return;
}
console.log('Record created:', result);
count--;
if(count === 0) {
console.log('done');
ds.disconnect();
}
});
});
});
});
}
}
_(node) warning: possible EventEmitter memory leak detected. 11 connected listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
at DataSource.addListener (events.js:179:15)
at DataSource.once (events.js:204:8)
at migrate (/home/.../strongloop-test/server/data-fixture.js:151:8)
at Object.
I'm having basically the same problem. Trying to load static lookup data in a boot script, and I guess all the callbacks attachments are stacking up before the connection succeeds in the background?
+1
:+1: I am also facing the same issue after upgrading my node to v4.2.x Earlier I was using v0.10.25 (legacy) and I didn't face this error, and I am sure reverting back to that version will solve the problem. Do we have any better fix?
@aldegoeij @yashmurty @warriorpostman @BenjaminHorn and everybody who is still having the same problem, I cannot reporduce the error with mysql, however I can reproduce with postgres connector, so the way I got it to work, is the following:
in your app, go to : node_modules\loopback-connector\lib\sql.js and line 395, after this line
if (!this.dataSource.connected) {
please add:
this.dataSource.setMaxListeners(0);
let me if this works for all of you, and I will update the code. Thanks.
@loay thanks for investigating this. It works for me!
@BenjaminHorn Thanks. I am closing the issue. If the problem is still persisting, please feel free to leave a comment and I will re-open. Thanks.