silvesterprabu
silvesterprabu

Reputation: 1457

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

I am trying to get all message from inbox and parsing it then sending it to another server.

This is my code:

client.listMessages(0, function(err, messages) 
{
    var parser = new MailParser(self.mailParserOptions);
    messages.forEach(function(message)
    {
       parser.on("end", function(mail) 
      {
        console.log(mail);
      });
      self.client.createMessageStream(message.UID).pipe(parser);
    });
});

this is parsing perfectly but i am getting this error

(node) warning: possible EventEmitter memory leak detected. 11 listeners added.
 Use emitter.setMaxListeners() to increase limit.
 Trace
at MailParser.EventEmitter.addListener (events.js:160:15)
at Stream.pipe (stream.js:99:8)
at /home/above/Desktop/prabu/inbox.js:75:54
at Array.forEach (native)
at /home/above/Desktop/prabu/inbox.js:70:14
at IMAPClient.<anonymous> (/home/above/Desktop/node_modules/inbox/lib/client.js:1653:13)
at IMAPClient._currentRequest.callback (/home/above/Desktop/node_modules/inbox/lib/client.js:777:25)
at IMAPClient._responseRouter (/home/above/Desktop/node_modules/inbox/lib/client.js:618:30)
at IMAPClient._onServerResponse (/home/above/Desktop/node_modules/inbox/lib/client.js:551:10)
at IMAPLineParser.EventEmitter.emit (events.js:95:17)

I don't know why this is happening.

Upvotes: 1

Views: 3774

Answers (2)

Joachim Isaksson
Joachim Isaksson

Reputation: 180897

EDIT: As Paul noted, you're not even allowed to start more than one parsing at a time on a single MailParser, so you'll need to create a new one for each parsing. When you do that, the limit no longer applies, and you can use your existing loop. I'll leave the text below since it explains the message and still applies to other similar cases.


What the message means is that you're starting parsing on more than 10 messages at the same time on a single EventEmitter (MailParser). Your loop starts one parsing per iteration, and never waits for any previous ones to complete.

If that's what you're meaning to do (it's not in this case due to that you can only have one running parse per MailParser), you'd need to raise the EventEmitter limit to allow that (doing what the error message tells you). Since your loop as far as I can see could loop over a theoretically unlimited number of messages and you're attempting to parse all of them in parallel, that may not be the best thing to do.

If that's not what you're meaning to do, you have a few options. Either you need to create a new MailParser for each parse (the limit only applies per EventEmitter), or you may want to look into the async library and use for example eachSeries or eachLimit to limit the number of simultaneously started parsings to a lower number.

Upvotes: 1

Paul Mougel
Paul Mougel

Reputation: 17038

There are multiple issues with your code:

  • Assuming I understand andris9/mailparser's documentation correctly, the MailParser object is supposed to parse one email, and one only. However you create one parser and feed it with many messages, which won't produce the expected results;
  • You listen a lot of times on the end event of the same MailParser, so Node.js warns you that maybe what you're doing isn't what you intend to do. See for instance this question, where the poster kind of faced the same problem.

Instead, I suggest the following solution:

client.listMessages(0, function(err, messages)  {
    messages.forEach(function(message) {
        var parser = new MailParser(self.mailParserOptions);
        parser.once("end", function(mail) {
            console.log(mail);
        });
        self.client.createMessageStream(message.UID).pipe(parser);
    });
});

Note:

  • that the parser object is created for each message
  • since we know that the end event will be emitted only once, we use the .once() method instead of on()

Upvotes: 2

Related Questions