Monday, August 26, 2013

Node.js fs.watchFile error EventEmitter memory leak detected . Max listeners

This happens when fs.WatchFile is called on the same file multiple times.
Explaination
fs module contains a local object "statWatchers" {} to track files being watched.
So when fs.watchFile('fileX') is called a new statWatcher object is created if one does not exits and stored it in the "statWatchers" object against the file name. If there is already a statWatcher object associated with this file, it is returned.
statWatcher["fileX"] = new statWatcher();
And then calls addListener('change', listener) on the statWatcher object associated with this file.
Incase fs.watchFile is called for 11 times on "fileX", it will result in calling addListener on the statWatcher for event "change" 11 times.
EventEmitter throws an error if tried to add more than 11 listeners for the same event.

No comments:

Post a Comment