diff --git a/lib/zlib.js b/lib/zlib.js index c57e83661bfa9b..80ffb962b38c6c 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -472,6 +472,11 @@ function processCallback() { return; } + if (self.destroyed) { + this.buffer = null; + return; + } + var availOutAfter = state[0]; var availInAfter = state[1]; 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);