R
R
Roman Alekseevich2019-07-19 17:58:31
JavaScript
Roman Alekseevich, 2019-07-19 17:58:31

Problem with EventEmitter in PromiseFTP module, how to catch a leak?

has anyone worked with PromiseFTP? I get a list of files by the mask I need, then I do a get in a loop for each file. Outputs Possible EventEmitter memory leak detected. 11 error listeners added. Although eventNames and listenerCount output [ 'end', 'finish', '_socketEnd', 'error', 'close', 'data' ] 3 1 1 2 2 1 in each iteration. the numbers correspond to the number of listeners for each event. And why are there 3 of them when there should be 1 each? I've been poking around for 2 days and I don't understand.
the code:

var ftp = new PromiseFtp();
ftp.connect(ftp1).then(function (serverMessage) {
  console.log('Server message: '+serverMessage);
  return ftp.list('2019/07/18/*fr.jpg');
}).then(function (list) {
       list.forEach(function(element,nubmer) {

        returnImage(element);

  });  
}).then(function (resultofwork) {
  return ftp.end();
});;


function returnImage(element){
  return ftp.get('2019/07/18/'+element.name).then(function (stream) {
    return new Promise(function (resolve, reject) {

      stream.once('close', resolve);
      stream.once('error', reject);
      stream.pipe(fs.createWriteStream('files/'+element.name));
      console.log(stream.eventNames(), stream.listenerCount('end'),stream.listenerCount('finish'),stream.listenerCount('_socketEnd'),stream.listenerCount('error'),stream.listenerCount('close'),stream.listenerCount('data'),);
    });
})};

stream object's events property:
_events:
{ end: [ [Function], [Function], [Function] ],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
error: [ [Function], [Function] ],
close: [ [Function], [Function] ],
data: [Function: ondata] },

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2019-07-19
@Robur

https://github.com/nolanlawson/throw-max-listeners...
this will help you turn the warning into an error and catch where it happens.
You can just take the idea and debug without this lib, just catch the moment when there are more than 10 listeners and look at the callstack.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question