diff --git a/lib/zlib.js b/lib/zlib.js index a25901ac6ef16d..d620b6bee3470a 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -431,6 +431,9 @@ Zlib.prototype._processChunk = function _processChunk(chunk, flushFlag, cb) { if (self._hadError) return; + if (self.destroyed) + return; + var have = availOutBefore - availOutAfter; assert(have >= 0, 'have should not go down'); diff --git a/test/parallel/test-zlib-destroy-pipe.js b/test/parallel/test-zlib-destroy-pipe.js new file mode 100644 index 00000000000000..38b8a5b4926e51 --- /dev/null +++ b/test/parallel/test-zlib-destroy-pipe.js @@ -0,0 +1,21 @@ +'use strict'; + +const common = require('../common'); +const zlib = require('zlib'); +const { Writable } = require('stream'); + +// verify that the zlib transform does not error in case +// it is destroyed with data still in flight + +const ts = zlib.createGzip(); + +const ws = new Writable({ + write: common.mustCall((chunk, enc, cb) => { + setImmediate(cb); + ts.destroy(); + }) +}); + +const buf = Buffer.allocUnsafe(1024 * 1024 * 20); +ts.end(buf); +ts.pipe(ws);