Skip to content

Commit

Permalink
stream: don't call _read after destroy()
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Sep 7, 2019
1 parent 17d87d5 commit 593e6b2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
13 changes: 12 additions & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ Readable.prototype.read = function(n) {
n = parseInt(n, 10);
}
const state = this._readableState;

if (state.destroyed) {
return;
}

const nOrig = n;

// If we're asking for more than the current hwm, then raise the hwm.
Expand Down Expand Up @@ -643,7 +648,7 @@ function maybeReadMore_(stream, state) {
// called push() with new data. In this case we skip performing more
// read()s. The execution ends in this method again after the _read() ends
// up calling push() with more data.
while (!state.reading && !state.ended &&
while (!state.reading && !state.ended && !state.destroyed &&
(state.length < state.highWaterMark ||
(state.flowing && state.length === 0))) {
const len = state.length;
Expand Down Expand Up @@ -976,6 +981,12 @@ function resume(stream, state) {

function resume_(stream, state) {
debug('resume', state.reading);

if (state.destroyed) {
state.resumeScheduled = false;
return;
}

if (!state.reading) {
stream.read(0);
}
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-stream-readable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,12 @@ const assert = require('assert');
read.push('hi');
read.on('data', common.mustNotCall());
}

{
const read = new Readable({
read: common.mustNotCall(function() {})
});
read.destroy();
assert.strictEqual(read.destroyed, true);
read.read();
}
29 changes: 0 additions & 29 deletions test/parallel/test-stream-transform-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,35 +91,6 @@ const assert = require('assert');
transform.destroy();
}

{
const transform = new Transform({
transform(chunk, enc, cb) {}
});
transform.resume();

transform._destroy = common.mustCall(function(err, cb) {
assert.strictEqual(err, null);
process.nextTick(() => {
this.push(null);
this.end();
cb();
});
}, 1);

const fail = common.mustNotCall('no event');

transform.on('finish', fail);
transform.on('end', fail);
transform.on('close', common.mustCall());

transform.destroy();

transform.removeListener('end', fail);
transform.removeListener('finish', fail);
transform.on('end', common.mustCall());
transform.on('finish', common.mustCall());
}

{
const transform = new Transform({
transform(chunk, enc, cb) {}
Expand Down

0 comments on commit 593e6b2

Please sign in to comment.