Udi
Udi

Reputation: 107

writeStream - possible EventEmitter memory leak detected

I'm creating a writestream:

var file = fs.createWriteStream('path', {flags: 'a+', encoding: 'utf16le'});

Using async.queue, I'm queuing this job:

file.write(data, 'utf8');

file.on('error', function(error) {
    console.error('ERROR with file stream', error);
});

Getting this warning: (node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.

Is there a good way to resolve this, without using setMaxListeners(0)?

Node version: 0.10.29

Upvotes: 0

Views: 1595

Answers (2)

Ehsan Sarshar
Ehsan Sarshar

Reputation: 3211

after fetching all the data or error. remove the listeners

file.removeAllListeners()

Upvotes: 0

Andras
Andras

Reputation: 3055

Use file.addListener('error', fn) to add the listener, and file.removeListener() to remove it when the job is done. Listeners exist independently of the registering function, and adding even the identical function stacks, it will get called twice.

function listenerCallback() {
}
file.addListener('error', listenerCallback);
file.removeListener('error', listenerCallback);

The removed listener should be === identical to the one added; adding function(){} then remove function(){} does not cancel the first.

emitter.once() adds a one-shot listener, but since this is used for errors and not fetching the work, it's not appropriate here.

Upvotes: 3

Related Questions