Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak caused by gulp & nodemon

I have this simple gulpfile that basically compiles some less files and triggers livereload. I also have nodemon to restart the server when code is changed.

var gulp = require('gulp'),
    less = require('gulp-less'),
    autoprefixer = require('gulp-autoprefixer'),
    minifycss = require('gulp-minify-css'),
    jshint = require('gulp-jshint'),
    uglify = require('gulp-uglify'),
    // imagemin = require('gulp-imagemin'),
    rename = require('gulp-rename'),
    concat = require('gulp-concat'),
    notify = require('gulp-notify'),
    cache = require('gulp-cache'),
    livereload = require('gulp-livereload'),
    nodemon = require('gulp-nodemon')
    // del = require('del'),
    path = require('path');

gulp.task('styles', function() {
  return gulp.src('public/css/less/app.less')
    .pipe(less())
    .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
    .pipe(gulp.dest('public/css/'))
    .pipe(rename({suffix: '.min'}))
    .pipe(minifycss())
    .pipe(gulp.dest('public/css/'))
    .pipe(notify({ message: 'Styles task complete' }))
    .pipe(livereload());
});

// Watch
gulp.task('watch', function() {

  livereload.listen();

  gulp.watch('public/css/less/*.less', ['styles']);
  gulp.watch(['public/app/**']).on('change', livereload.changed);

});


gulp.task('demon', function () {
  nodemon({
    ignore: [
      './public/',
      './node_modules/',
      './tests/'
    ]
  })
    .on('start', ['watch'])
    .on('change', ['watch'])
    .on('restart', function () {
      console.log('restarted!');
    });
});

// Default Task
gulp.task('default', ['demon']);

The problem is that everything works for 5 times then I get a warning:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at StatWatcher.addListener (events.js:160:15)
    at Object.fs.watchFile (fs.js:1175:8)
    at Gaze._pollFile (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:329:10)
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:411:12
    at Array.forEach (native)
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:409:11
    at iterate (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)
    at Object.forEachSeries (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:66:3)
    at Gaze._initWatched (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:354:10)
    at Gaze.add (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:177:8)
    at new Gaze (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:74:10)
    at gaze (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:86:10)
    at Object.module.exports [as watch] (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/index.js:12:17)
    at Gulp.watch (/Users/kris/Sites/SmartSaver/node_modules/gulp/index.js:35:16)
    at Gulp.gulp.task.nodemon.ignore (/Users/kris/Sites/SmartSaver/gulpfile.js:36:8)
    at module.exports (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:34:7)

And after words a crash:

[gulp] [nodemon] starting `node ./bin/www`
[12:24:43] Starting 'watch'...
[12:24:43] 'watch' errored after 9.52 ms
[12:24:43] Error: EMFILE: Too many opened files.
    at Gaze._handleError (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:436:31)
    at Gaze._watchDir (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:302:17)
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:358:10
    at iterate (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:61:11
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:420:5
    at iterate (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:61:11
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:420:5
    at iterate (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:61:11
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:420:5
    at iterate (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:61:11
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:420:5
    at iterate (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)

I searched the warning and there seems to be a bug in Node but I believe I am doing something wrong here such as maybe initialising too many listeners or not closing them properly.

Any feedback is appreciated.

Thanks!

like image 745
Kris Avatar asked Oct 19 '22 09:10

Kris


1 Answers

Looks like the issue is this

.on('start', ['watch'])
.on('change', ['watch'])
.on('restart', function () {
  console.log('restarted!');
});

Everytime a file changes, you're triggering watch which attaches additional listeners. After a few restarts, you will exceed the max number of listeners.

This is a standard nodemon setup, and is all you need really:

gulp.task('dev', function () {
  nodemon({
    script: 'your-start-script.js',
    ext: 'js'
  });
});
like image 155
Yuri Zarubin Avatar answered Oct 21 '22 23:10

Yuri Zarubin