Skip to content

Commit

Permalink
zlib: allow writes after readable 'end' to finish
Browse files Browse the repository at this point in the history
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 <shishugi@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
addaleax authored and BethGriggs committed Feb 6, 2020
1 parent b1b7f67 commit 7cad756
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
13 changes: 5 additions & 8 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
this._defaultFlushFlag = flush;
this._finishFlushFlag = finishFlush;
this._defaultFullFlushFlag = fullFlush;
this.once('end', this.close);
this.once('end', _close.bind(null, this));
this._info = opts && opts.info;
}
Object.setPrototypeOf(ZlibBase.prototype, Transform.prototype);
Expand Down Expand Up @@ -476,7 +476,7 @@ function processChunkSync(self, chunk, flushFlag) {

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

handle.buffer = chunk;
handle.cb = cb;
Expand All @@ -502,13 +502,9 @@ function processCallback() {
const self = this[owner_symbol];
const state = self._writeState;

if (self._hadError) {
this.buffer = null;
return;
}

if (self.destroyed) {
if (self._hadError || self.destroyed) {
this.buffer = null;
this.cb();
return;
}

Expand All @@ -528,6 +524,7 @@ function processCallback() {
}

if (self.destroyed) {
this.cb();
return;
}

Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-zlib-write-after-end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
const common = require('../common');
const zlib = require('zlib');

// Regression test for https://github.com/nodejs/node/issues/30976
// Writes to a stream should finish even after the readable side has been ended.

const data = zlib.deflateRawSync('Welcome');

const inflate = zlib.createInflateRaw();

inflate.resume();
inflate.write(data, common.mustCall());
inflate.write(Buffer.from([0x00]), common.mustCall());
inflate.write(Buffer.from([0x00]), common.mustCall());
inflate.flush(common.mustCall());

0 comments on commit 7cad756

Please sign in to comment.