diff --git a/lib/zlib.js b/lib/zlib.js index 6d9f47dca2..ffb7ba6c69 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -574,6 +574,16 @@ Zlib.prototype._processChunk = function(chunk, flushFlag, cb) { req.callback = callback; function callback(availInAfter, availOutAfter) { + // When the callback is used in an async write, the callback's + // context is the `req` object that was created. The req object + // is === this._handle, and that's why it's important to null + // out the values after they are done being used. `this._handle` + // can stay in memory longer than the callback and buffer are needed. + if (this) { + this.buffer = null; + this.callback = null; + } + if (self._hadError) return;