Skip to content

Commit

Permalink
stream: do not error async iterators on destroy(null)
Browse files Browse the repository at this point in the history
Fixes: #23890

PR-URL: #23901
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
mcollina authored and MylesBorins committed Nov 29, 2018
1 parent 6f4a638 commit cb861a4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/internal/streams/async_iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const createReadableStreamAsyncIterator = (stream) => {
});

finished(stream, (err) => {
if (err) {
if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') {
const reject = iterator[kLastReject];
// reject if we are waiting for data in the Promise
// returned by next() and store the error
Expand Down
23 changes: 18 additions & 5 deletions test/parallel/test-stream-readable-async-iterators.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,8 @@ async function tests() {

readable.destroy();

try {
await readable[Symbol.asyncIterator]().next();
} catch (e) {
assert.strictEqual(e.code, 'ERR_STREAM_PREMATURE_CLOSE');
}
const { done } = await readable[Symbol.asyncIterator]().next();
assert.strictEqual(done, true);
})();

await (async function() {
Expand Down Expand Up @@ -380,6 +377,22 @@ async function tests() {
for await (const b of r) {
}
})();

await (async () => {
console.log('destroy mid-stream does not error');
const r = new Readable({
objectMode: true,
read() {
this.push('asdf');
this.push('hehe');
}
});

// eslint-disable-next-line no-unused-vars
for await (const a of r) {
r.destroy(null);
}
})();
}

// to avoid missing some tests if a promise does not resolve
Expand Down

0 comments on commit cb861a4

Please sign in to comment.