From 4bf5fb8233ac4aacc358639f53644ed26cc8c320 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Wed, 16 Dec 2015 08:24:46 +0100 Subject: [PATCH] Fix "zlib binding closed" errors --- lib/PerMessageDeflate.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/PerMessageDeflate.js b/lib/PerMessageDeflate.js index 20e05ec1a..5324bd8e6 100644 --- a/lib/PerMessageDeflate.js +++ b/lib/PerMessageDeflate.js @@ -76,12 +76,20 @@ PerMessageDeflate.prototype.accept = function(paramsList) { PerMessageDeflate.prototype.cleanup = function() { if (this._inflate) { - if (this._inflate.close) this._inflate.close(); - this._inflate = null; + if (this._inflate.writeInProgress) { + this._inflate.pendingClose = true; + } else { + if (this._inflate.close) this._inflate.close(); + this._inflate = null; + } } if (this._deflate) { - if (this._deflate.close) this._deflate.close(); - this._deflate = null; + if (this._deflate.writeInProgress) { + this._deflate.pendingClose = true; + } else { + if (this._deflate.close) this._deflate.close(); + this._deflate = null; + } } }; @@ -224,6 +232,7 @@ PerMessageDeflate.prototype.decompress = function (data, fin, callback) { windowBits: 'number' === typeof maxWindowBits ? maxWindowBits : DEFAULT_WINDOW_BITS }); } + this._inflate.writeInProgress = true; var self = this; var buffers = []; @@ -251,7 +260,8 @@ PerMessageDeflate.prototype.decompress = function (data, fin, callback) { if (!self._inflate) return; self._inflate.removeListener('error', onError); self._inflate.removeListener('data', onData); - if (fin && self.params[endpoint + '_no_context_takeover']) { + self._inflate.writeInProgress = false; + if ((fin && self.params[endpoint + '_no_context_takeover']) || self._inflate.pendingClose) { if (self._inflate.close) self._inflate.close(); self._inflate = null; } @@ -275,6 +285,7 @@ PerMessageDeflate.prototype.compress = function (data, fin, callback) { memLevel: this._options.memLevel || DEFAULT_MEM_LEVEL }); } + this._deflate.writeInProgress = true; var self = this; var buffers = []; @@ -303,7 +314,8 @@ PerMessageDeflate.prototype.compress = function (data, fin, callback) { if (!self._deflate) return; self._deflate.removeListener('error', onError); self._deflate.removeListener('data', onData); - if (fin && self.params[endpoint + '_no_context_takeover']) { + self._deflate.writeInProgress = false; + if ((fin && self.params[endpoint + '_no_context_takeover']) || self._deflate.pendingClose) { if (self._deflate.close) self._deflate.close(); self._deflate = null; }