Skip to content

(node) Possible eventEmitter memory leak detected. 11 listeners added. #86

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

Closed
jfeldstein opened this issue Jan 16, 2012 · 7 comments
Closed

Comments

@jfeldstein
Copy link

Getting this, with a stack trace back into pg. Any tips?

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace: 
    at [object Object].<anonymous> (events.js:123:17)
    at [object Object].<anonymous> (/Users/Jordan/Development/sway-nodes/node_modules/pg/lib/client.js:82:9)
    at [object Object].g (events.js:143:14)
    at [object Object].emit (events.js:81:20)
    at Socket.<anonymous> (/Users/Jordan/Development/sway-nodes/node_modules/pg/lib/connection.js:47:12)
    at Socket.emit (events.js:64:17)
    at Socket._onReadable (net.js:676:14)
----------------------------------------
    at EventEmitter.on
    at [object Object].once (events.js:147:8)
    at [object Object].connect (/Users/Jordan/Development/sway-nodes/node_modules/pg/lib/client.js:72:7)
    at new <anonymous> (/Users/Jordan/Development/sway-nodes/ServicesApi.js:22:11)
    at Object.<anonymous> (/Users/Jordan/Development/sway-nodes/update_server.js:28:17)
    at Module._compile (module.js:404:26)
    at Object..js (module.js:410:10)
    at Module.load (module.js:336:31)
    at Function._load (module.js:297:12)
@jfeldstein
Copy link
Author

It's part of a script that uses lots of async callbacks. Could I be writing too quickly/often to the database?

And which uses long-stack-traces https://github.com/tlrobinson/long-stack-traces. Could that library be causing the issue ( tlrobinson/long-stack-traces#5 )

@booo
Copy link
Contributor

booo commented Jan 16, 2012

This is totally normal if the number of event listeners is greater than 10. Maybe you should use emitter.setMaxListeners() to increase limit. or rethink your code.

@jfeldstein
Copy link
Author

Is an event listener being created each time I run a command that takes a
callback, such as running a pg query?

~ J

(via phone)

@brianc
Copy link
Owner

brianc commented Jan 23, 2012

can I get code to reproduce (an entire executable script from top to bottom minus connection info is best) this and a version of node, version of node-postgres, and if you are using pure-javascript or native bindings? I've done a decent amount of load testing and never seen max connection listeners exceeded.

More generally to answer your question, there should be no place within node-postgres where event listeners are attached & detached multiple times on a query-by-query basis. the Client object manages the event listening binding once to each backend message type once and only once when the initial connection has been established and is ready to be queried. If you are manually connecting & disconnecting a single client object over and over again this could happen...

@jfeldstein
Copy link
Author

Unfortunately, I stopped using the plugin that exposed these warnings, so I'm not actually able to give better code examples.

Luckily, it still sounds like this might have been an issue from something else, and not a PG bug.

@rcreasi
Copy link

rcreasi commented Aug 6, 2014

I had the same issue. I figured out it was caused by doing

pg.connect(conString, function(err, client, done) {
    var errorListener = function(error) {
        console.log("Oh noes, you got error'd: ", error);
    };
    client.on('error', errorListener);

    // ... do some stuff with client then call done() ...
})

but never doing

client.removeListener('error', errorListener);

I think in this case client is a reference to an old connection, reused by the connection pooler, and every time I asked for one I was adding a redundant listener to it.

@PritamUpadhyay
Copy link

Hello.
I am having same issue.
My code looks like

var exports = module.exports = {};

var pg = require('pg');
var connectionString = "postgres://postgres:[email protected]:5432/postgres";

var client = new pg.Client(connectionString);

var connected = false;

function connect() {
    client.connect(function (err) {
        if (err) {
            connected = false;
        }
        else {
            connected = true;
        }
    });
}

exports.Insert = function (packet) {
    if (!connected) connect();    
    var tablename = packet.msgExchange.split('-')[1];

    client.query('CREATE TABLE IF NOT EXISTS ' + tablename + '(msgid serial NOT NULL, msgtxt character varying, "msgDateTime" timestamp with time zone DEFAULT now(), "msgChannel" character varying, "msgClient" character varying, CONSTRAINT ' + tablename + '_pkey PRIMARY KEY (msgid)) WITH ( OIDS=FALSE )');


    for (var i = 0; i < packet.msgChannel.length; i++) {
        client.query('INSERT INTO '+ tablename + '(msgTxt, "msgDateTime", "msgChannel", "msgClient") VALUES ($1, $2, $3, $4)', [packet.msgTxt.toString(), packet.msgDateTime, packet.msgChannel[i], packet.msgClient.toString()]);
    }
}

I have created separate file to use Insert function. When I call Insert function inside the for loop, It is giving me same error given by @jfeldstein.
However if I use this function without loop, it works fine.

brianc pushed a commit that referenced this issue Dec 27, 2019
* Add failing test for correct removal from checkout queue on timeout

* Remove timed-out checkouts from queue correctly

Fixes #85.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants