NodeJS : How to debug “EventEmitter memory leak detected. 11 listeners added”

前端 未结 8 543
攒了一身酷
攒了一身酷 2020-11-28 20:23

How can I debug my application which throw this error:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxList         


        
8条回答
  •  醉梦人生
    2020-11-28 21:06

    This is exactly what happened to me. For me, I nested an event listener within another event listener accidentally.

    Look at your code and make sure you DO NOT have an event listener block WITHIN another event listener block for example(unless you are doing it on purpose):

    socket.on('data', function(data) {
    //code goes here
    
    socket.on('close' , function() {
    //code goes here
         });
    
       });
    

    In the wrong example above, the socket.on ('close') listener should be OUTSIDE of the socket.on('data') block.

    In my case when I received 5 data streams, the socket.on('close') listener is waiting for a close event to happen. When I close once, another 4th closing event would execute. This is clearly not what I wanted. This is due to the nature of Node.js which is non-blocking. It 'remembers' events due to the callback function.

提交回复
热议问题