Skip to content

Commit c6a43fa

Browse files
mafintoshMylesBorins
authored andcommitted
zlib: do not leak on destroy
PR-URL: #23734 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 4749640 commit c6a43fa

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

lib/zlib.js

+9
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,11 @@ Zlib.prototype.close = function close(callback) {
427427
this.destroy();
428428
};
429429

430+
Zlib.prototype._destroy = function _destroy(err, callback) {
431+
_close(this);
432+
callback(err);
433+
};
434+
430435
Zlib.prototype._transform = function _transform(chunk, encoding, cb) {
431436
var flushFlag = this._defaultFlushFlag;
432437
// We use a 'fake' zero-length chunk to carry information about flushes from
@@ -589,6 +594,10 @@ function processCallback() {
589594
assert(false, 'have should not go down');
590595
}
591596

597+
if (self.destroyed) {
598+
return;
599+
}
600+
592601
// exhausted the output buffer, or used all the input create a new one.
593602
if (availOutAfter === 0 || self._outOffset >= self._chunkSize) {
594603
handle.availOutBefore = self._chunkSize;
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const zlib = require('zlib');
5+
6+
const ts = zlib.createGzip();
7+
const buf = Buffer.alloc(1024 * 1024 * 20);
8+
9+
ts.on('data', common.mustCall(() => ts.close()));
10+
ts.end(buf);

test/parallel/test-zlib-destroy.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const assert = require('assert');
6+
const zlib = require('zlib');
7+
8+
// verify that the zlib transform does clean up
9+
// the handle when calling destroy.
10+
11+
const ts = zlib.createGzip();
12+
ts.destroy();
13+
assert.strictEqual(ts._handle, null);

0 commit comments

Comments
 (0)