writeStream - possible EventEmitter memory leak detected

1.5k views Asked by At

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

2

There are 2 answers

0
Andras On BEST ANSWER

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.

0
TheEhsanSarshar On

after fetching all the data or error. remove the listeners

file.removeAllListeners()