From ec390b63a1989d5da0ec7043f7e2e91398212877 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sun, 8 Sep 2019 10:35:34 +0200 Subject: [PATCH] stream: do not call _read() after destroy() PR-URL: https://github.com/nodejs/node/pull/29491 Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum Reviewed-By: Minwoo Jung Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Jeremiah Senkpiel --- lib/_stream_readable.js | 7 ++++--- lib/internal/fs/streams.js | 3 --- test/parallel/test-stream-readable-destroy.js | 9 +++++++++ test/parallel/test-wrap-js-stream-exceptions.js | 2 +- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 7502a1ac2c5ef2..2dc1323f099842 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -490,9 +490,10 @@ Readable.prototype.read = function(n) { debug('length less than watermark', doRead); } - // However, if we've ended, then there's no point, and if we're already - // reading, then it's unnecessary. - if (state.ended || state.reading) { + // However, if we've ended, then there's no point, if we're already + // reading, then it's unnecessary, and if we're destroyed, then it's + // not allowed. + if (state.ended || state.reading || state.destroyed) { doRead = false; debug('reading or ended', doRead); } else if (doRead) { diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index d51fbb1b580113..b0c4224893e9a6 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -137,9 +137,6 @@ ReadStream.prototype._read = function(n) { }); } - if (this.destroyed) - return; - if (!pool || pool.length - pool.used < kMinPoolSpace) { // Discard the old pool. allocNewPool(this.readableHighWaterMark); diff --git a/test/parallel/test-stream-readable-destroy.js b/test/parallel/test-stream-readable-destroy.js index 05e7dd464ddca0..7687ea90cc82d8 100644 --- a/test/parallel/test-stream-readable-destroy.js +++ b/test/parallel/test-stream-readable-destroy.js @@ -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(); +} diff --git a/test/parallel/test-wrap-js-stream-exceptions.js b/test/parallel/test-wrap-js-stream-exceptions.js index cde7c178446a11..eeab26f525ae50 100644 --- a/test/parallel/test-wrap-js-stream-exceptions.js +++ b/test/parallel/test-wrap-js-stream-exceptions.js @@ -10,7 +10,7 @@ process.once('uncaughtException', common.mustCall((err) => { })); const socket = new JSStreamWrap(new Duplex({ - read: common.mustCall(), + read: common.mustNotCall(), write: common.mustCall((buffer, data, cb) => { throw new Error('exception!'); })