Skip to content

Commit 593e6b2

Browse files
committed
stream: don't call _read after destroy()
1 parent 17d87d5 commit 593e6b2

3 files changed

Lines changed: 21 additions & 30 deletions

File tree

lib/_stream_readable.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,11 @@ Readable.prototype.read = function(n) {
420420
n = parseInt(n, 10);
421421
}
422422
const state = this._readableState;
423+
424+
if (state.destroyed) {
425+
return;
426+
}
427+
423428
const nOrig = n;
424429

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

977982
function resume_(stream, state) {
978983
debug('resume', state.reading);
984+
985+
if (state.destroyed) {
986+
state.resumeScheduled = false;
987+
return;
988+
}
989+
979990
if (!state.reading) {
980991
stream.read(0);
981992
}

test/parallel/test-stream-readable-destroy.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,12 @@ const assert = require('assert');
189189
read.push('hi');
190190
read.on('data', common.mustNotCall());
191191
}
192+
193+
{
194+
const read = new Readable({
195+
read: common.mustNotCall(function() {})
196+
});
197+
read.destroy();
198+
assert.strictEqual(read.destroyed, true);
199+
read.read();
200+
}

test/parallel/test-stream-transform-destroy.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -91,35 +91,6 @@ const assert = require('assert');
9191
transform.destroy();
9292
}
9393

94-
{
95-
const transform = new Transform({
96-
transform(chunk, enc, cb) {}
97-
});
98-
transform.resume();
99-
100-
transform._destroy = common.mustCall(function(err, cb) {
101-
assert.strictEqual(err, null);
102-
process.nextTick(() => {
103-
this.push(null);
104-
this.end();
105-
cb();
106-
});
107-
}, 1);
108-
109-
const fail = common.mustNotCall('no event');
110-
111-
transform.on('finish', fail);
112-
transform.on('end', fail);
113-
transform.on('close', common.mustCall());
114-
115-
transform.destroy();
116-
117-
transform.removeListener('end', fail);
118-
transform.removeListener('finish', fail);
119-
transform.on('end', common.mustCall());
120-
transform.on('finish', common.mustCall());
121-
}
122-
12394
{
12495
const transform = new Transform({
12596
transform(chunk, enc, cb) {}

0 commit comments

Comments
 (0)