简体   繁体   中英

NodeJS memory leak

I'm making a game with socket.io and mysql , and at every client connect, I check if the user is banned by IP with my custom MySQL function :

MySQL.Query('SELECT * FROM bans WHERE user = ?', username, function(rows) {
    // do something
})


but when I connect > 5 clients, I have this error on the server console:

(node) warning: possible EventEmitter memory leak detected. 11 error listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at PoolConnection.addListener (events.js:239:17)
    at C:\Users\user\Documents\_server\mysql\mysql.js:26:21
    at Ping.onOperationComplete [as _callback] (C:\Users\user\node_modules\mysql\lib\Pool.js:99:5)
    at Ping.Sequence.end (C:\Users\user\node_modules\mysql\lib\protocol\sequences\Sequence.js:96:24)
    at Ping.Sequence.OkPacket (C:\Users\user\node_modules\mysql\lib\protocol\sequences\Sequence.js:105:8)
    at Protocol._parsePacket (C:\Users\user\node_modules\mysql\lib\protocol\Protocol.js:280:23)
    at Parser.write (C:\Users\user\node_modules\mysql\lib\protocol\Parser.js:73:12)
    at Protocol.write (C:\Users\user\node_modules\mysql\lib\protocol\Protocol.js:39:16)
    at Socket.<anonymous> (C:\Users\user\node_modules\mysql\lib\Connection.js:96:28)
    at emitOne (events.js:77:13)

Here's mysql.js file :

var config = require('../config.js');
var mysql = require('mysql');
var pool = mysql.createPool({
    connectionLimit : 100,
    host     : config.sqlhost,
    user     : config.sqluser,
    password : config.sqlpass,
    database : config.sqlbase,
    debug    :  false
});
var MySQL = {
    Query: function(req, reqvars, callback) {
        pool.getConnection(function(err,connection){
            if (err) {
              connection.release();
              console.log('[MySQL] Error while connecting to the database');
            }   
            connection.query(req, reqvars, function(err,rows){
                connection.release();
                if (typeof callback == 'function') callback(rows);
            });
            connection.on('error', function(err) {      
                console.log('[MySQL] Error while attempting query');   
            });
        });
    }, 
}
module.exports = MySQL;

Where's the problem?

Node.js warns you whenever you attach more than 10 (the default) listeners for the same event on a single event emitter.

In your particular case, you attach an error listener to the connection object each and every time you make an SQL query. This is probably not what you want, because if the first x queries executed fine, but then one fails, those error handlers for the previous queries will still get called.

Remedy

While I have no practical experience with the mysql library, the first thing hitting me is that you are completely ignoring the err parameter of your connection.query() callback. You must not ignore these, unless you would like to spend hours of debugging a seemingly valid piece of code.

It would seem that instead of attaching an error handler to the connection , you simply check the err argument.

PS. It is common practice to always pass along any errors that occur during execution as the first argument in callback-based APIs (specifying null in case of no error). Your callback function seems to not adhere to this practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM