jgable / gulp-cache

A cache proxy task for Gulp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory leak when running task

donaldallen opened this issue · comments

☁  assets [master] ⚡ gulp images
[gulp] Using file /Users/donaldallen/Sites/somesite.ca/app/assets/gulpfile.js
[gulp] Working directory changed to /Users/donaldallen/Sites/somesite.ca/app/assets
[gulp] Running 'images'...
(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at Stream.EventEmitter.addListener (events.js:160:15)
    at _.extend._runProxiedTask (/Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/lib/TaskProxy.js:134:19)
    at _.extend._runProxiedTaskAndCache (/Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/lib/TaskProxy.js:107:21)
    at /Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/lib/TaskProxy.js:26:25
    at tryCatch1 (/Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/node_modules/bluebird/js/main/util.js:64:19)
    at Promise$_callHandler [as _callHandler] (/Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:708:13)
    at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (/Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:724:18)
    at Promise$_settlePromiseAt [as _settlePromiseAt] (/Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:896:14)
    at Promise$_fulfillPromises [as _fulfillPromises] (/Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:1041:14)
    at Async$_consumeFunctionBuffer [as _consumeFunctionBuffer] (/Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/node_modules/bluebird/js/main/async.js:64:12)
(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at Stream.EventEmitter.addListener (events.js:160:15)
    at Stream.EventEmitter.once (events.js:185:8)
    at _.extend._runProxiedTask (/Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/lib/TaskProxy.js:137:19)
    at _.extend._runProxiedTaskAndCache (/Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/lib/TaskProxy.js:107:21)
    at /Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/lib/TaskProxy.js:26:25
    at tryCatch1 (/Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/node_modules/bluebird/js/main/util.js:64:19)
    at Promise$_callHandler [as _callHandler] (/Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:708:13)
    at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (/Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:724:18)
    at Promise$_settlePromiseAt [as _settlePromiseAt] (/Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:896:14)
    at Promise$_fulfillPromises [as _fulfillPromises] (/Users/donaldallen/Sites/somesite.ca/app/assets/node_modules/gulp-cache/node_modules/bluebird/js/main/promise.js:1041:14)

And here is my task:

gulp.task('images', function() {
    return gulp.src(['img/**/*.png', 'img/**/*.jpg', 'img/**.*.gif'])
        .pipe(cache(image()))
        .pipe(gulp.dest('../../public/assets/public/img'));
});

So the errors above show up, then there's about a 15 second pause, and the task continues. In this case, my images start compressing and moving. It feels slower than it should be.

Yeah, I've run into this before. The issue is that when we run the original task, we have to bind some event listeners to listen for the end of the task. When there are a lot of files moving through the stream, we can add a bunch of listeners at once. We remove them all when the task is done, but once we have more than 10 it triggers that warning.

I see a couple workarounds right now

  • use setMaxListeners(0) to eliminate the warning and just hope we are being good citizens
  • figure out how to run the original task without binding to it
  • increment max listeners for every time we run the proxied task

I'm going to try and do the incrementing and test it out. If that doesn't work, I'm going to just setMaxListeners(0) and punt on it.

For the time being, this might be a workaround:

gulp.task('images', function() {
    var imageTask = image();
    imageTask.setMaxListeners(0);
    return gulp.src(['img/**/*.png', 'img/**/*.jpg', 'img/**.*.gif'])
        .pipe(cache(imageTask))
        .pipe(gulp.dest('../../public/assets/public/img'));
});

Awesome, thanks @jgable. I'll give this a go. I truly appreciate it.

Alrighty, have a fix for this published as 0.1.5 on npm. Thanks for reporting it and let me know if you see the warning again.

Just a note about the performance you were noticing, it should be slow the first time you run it because nothing is cached, but if you notice it is slow every time even when you haven't changed anything there might be a configuration problem. Let me know whether it's still slow for you every time and I'll setup something locally to get to the bottom of it.