From 2cd98924252fc1b59f72f4ad280001bfcc98a8c7 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sat, 25 Jan 2020 12:00:21 +0100 Subject: [PATCH] Revert "stream: fix async iterator destroyed error propagation" This reverts commit d15b8ea3bdcb72b8e2dd3aee0cc717daa512d2f6. PR-URL: https://github.com/nodejs/node/pull/31508 Reviewed-By: James M Snell Reviewed-By: Anatoli Papirovski Reviewed-By: Colin Ihrig Reviewed-By: Rich Trott Reviewed-By: Benjamin Gruenbaum --- lib/internal/streams/async_iterator.js | 24 +++++++++---------- .../test-stream-readable-async-iterators.js | 17 ------------- 2 files changed, 11 insertions(+), 30 deletions(-) diff --git a/lib/internal/streams/async_iterator.js b/lib/internal/streams/async_iterator.js index c4c220a5afe34f..1df891d30c4dc4 100644 --- a/lib/internal/streams/async_iterator.js +++ b/lib/internal/streams/async_iterator.js @@ -106,20 +106,18 @@ const ReadableStreamAsyncIteratorPrototype = ObjectSetPrototypeOf({ } if (this[kStream].destroyed) { + // We need to defer via nextTick because if .destroy(err) is + // called, the error will be emitted via nextTick, and + // we cannot guarantee that there is no error lingering around + // waiting to be emitted. return new Promise((resolve, reject) => { - if (this[kError]) { - reject(this[kError]); - } else if (this[kEnded]) { - resolve(createIterResult(undefined, true)); - } else { - finished(this[kStream], (err) => { - if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') { - reject(err); - } else { - resolve(createIterResult(undefined, true)); - } - }); - } + process.nextTick(() => { + if (this[kError]) { + reject(this[kError]); + } else { + resolve(createIterResult(undefined, true)); + } + }); }); } diff --git a/test/parallel/test-stream-readable-async-iterators.js b/test/parallel/test-stream-readable-async-iterators.js index 1da31b45e23ba9..58f33177aa92cf 100644 --- a/test/parallel/test-stream-readable-async-iterators.js +++ b/test/parallel/test-stream-readable-async-iterators.js @@ -567,23 +567,6 @@ async function tests() { assert.strictEqual(e, err); })()]); } - - { - const _err = new Error('asd'); - const r = new Readable({ - read() { - }, - destroy(err, callback) { - setTimeout(() => callback(_err), 1); - } - }); - - r.destroy(); - const it = r[Symbol.asyncIterator](); - it.next().catch(common.mustCall((err) => { - assert.strictEqual(err, _err); - })); - } } {