Skip to content

Commit 8407086

Browse files
addaleaxtargos
authored andcommitted
zlib: allow writes after readable 'end' to finish
Call the callback for writes that occur after the stream is closed. This also requires changes to the code to not call `.destroy()` on the stream in `.on('end')`, and to ignore chunks written afterwards. Previously, these writes would just queue up silently, as their `_write()` callback would never have been called. Fixes: #30976 PR-URL: #31082 Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 4cd459c commit 8407086

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

lib/zlib.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
264264
this._defaultFlushFlag = flush;
265265
this._finishFlushFlag = finishFlush;
266266
this._defaultFullFlushFlag = fullFlush;
267-
this.once('end', this.close);
267+
this.once('end', _close.bind(null, this));
268268
this._info = opts && opts.info;
269269
}
270270
Object.setPrototypeOf(ZlibBase.prototype, Transform.prototype);
@@ -476,7 +476,7 @@ function processChunkSync(self, chunk, flushFlag) {
476476

477477
function processChunk(self, chunk, flushFlag, cb) {
478478
const handle = self._handle;
479-
assert(handle, 'zlib binding closed');
479+
if (!handle) return process.nextTick(cb);
480480

481481
handle.buffer = chunk;
482482
handle.cb = cb;
@@ -502,13 +502,9 @@ function processCallback() {
502502
const self = this[owner_symbol];
503503
const state = self._writeState;
504504

505-
if (self._hadError) {
506-
this.buffer = null;
507-
return;
508-
}
509-
510-
if (self.destroyed) {
505+
if (self._hadError || self.destroyed) {
511506
this.buffer = null;
507+
this.cb();
512508
return;
513509
}
514510

@@ -528,6 +524,7 @@ function processCallback() {
528524
}
529525

530526
if (self.destroyed) {
527+
this.cb();
531528
return;
532529
}
533530

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
const common = require('../common');
3+
const zlib = require('zlib');
4+
5+
// Regression test for https://github.com/nodejs/node/issues/30976
6+
// Writes to a stream should finish even after the readable side has been ended.
7+
8+
const data = zlib.deflateRawSync('Welcome');
9+
10+
const inflate = zlib.createInflateRaw();
11+
12+
inflate.resume();
13+
inflate.write(data, common.mustCall());
14+
inflate.write(Buffer.from([0x00]), common.mustCall());
15+
inflate.write(Buffer.from([0x00]), common.mustCall());
16+
inflate.flush(common.mustCall());

0 commit comments

Comments
 (0)