Skip to content

Commit 60991a2

Browse files
committed
PerMessageDeflate#cleanup - properly cleanup on close
1 parent fa99173 commit 60991a2

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

lib/permessage-deflate.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ class PerMessageDeflate {
133133
}
134134

135135
if (this._deflate) {
136+
if (this._deflate[kCallback]) {
137+
this._deflate[kCallback]();
138+
}
139+
136140
this._deflate.close();
137141
this._deflate = null;
138142
}
@@ -312,7 +316,9 @@ class PerMessageDeflate {
312316
zlibLimiter.push((done) => {
313317
this._compress(data, fin, (err, result) => {
314318
done();
315-
callback(err, result);
319+
if (err || result) {
320+
callback(err, result);
321+
}
316322
});
317323
});
318324
}
@@ -419,6 +425,8 @@ class PerMessageDeflate {
419425
this._deflate.on('data', deflateOnData);
420426
}
421427

428+
this._deflate[kCallback] = callback;
429+
422430
this._deflate.write(data);
423431
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
424432
if (!this._deflate) {
@@ -438,6 +446,11 @@ class PerMessageDeflate {
438446

439447
if (fin) data = data.slice(0, data.length - 4);
440448

449+
// Set callback to null so we won't call it twice if socket is closed
450+
// (see PerMessageDeflate#cleanup)
451+
// ideally, we would set it to `null` before calling, but we might set it to null
452+
this._deflate[kCallback] = null;
453+
441454
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
442455
this._deflate.close();
443456
this._deflate = null;

0 commit comments

Comments
 (0)