diff --git a/lib/processImage.js b/lib/processImage.js index 46a8a02..c1c242b 100644 --- a/lib/processImage.js +++ b/lib/processImage.js @@ -71,8 +71,13 @@ module.exports = function (options) { if (filter.end) { filter.end(); } + filter.removeAllListeners(); + // Some of the filters seem to emit error more than once sometimes: + filter.on('error', function () {}); }); + filters = null; } + res.removeAllListeners(); } } @@ -152,19 +157,21 @@ module.exports = function (options) { filters[i].on('etagFragment', function (etagFragment) { etagFragments.push(etagFragment); }); - filters[i].on('error', handleError); + // Some of the filters appear to emit error more than once: + filters[i].once('error', handleError); } res.pipe(filters[0]); filters[filters.length - 1].on('end', function () { hasEnded = true; + cleanUp(); }).pipe(res); - res.on('error', function () { + res.once('error', function () { res.unhijack(); next(500); }); - res.on('close', cleanUp); + res.once('close', cleanUp); } else { res.unhijack(); }