Skip to content

Commit 7bbc072

Browse files
mcollinatargos
authored andcommitted
stream: do not error async iterators on destroy(null)
Fixes: #23890 PR-URL: #23901 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 5ce3b6d commit 7bbc072

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

lib/internal/streams/async_iterator.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ const createReadableStreamAsyncIterator = (stream) => {
153153
});
154154

155155
finished(stream, (err) => {
156-
if (err) {
156+
if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') {
157157
const reject = iterator[kLastReject];
158158
// reject if we are waiting for data in the Promise
159159
// returned by next() and store the error

test/parallel/test-stream-readable-async-iterators.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,8 @@ async function tests() {
335335

336336
readable.destroy();
337337

338-
try {
339-
await readable[Symbol.asyncIterator]().next();
340-
} catch (e) {
341-
assert.strictEqual(e.code, 'ERR_STREAM_PREMATURE_CLOSE');
342-
}
338+
const { done } = await readable[Symbol.asyncIterator]().next();
339+
assert.strictEqual(done, true);
343340
})();
344341

345342
await (async function() {
@@ -380,6 +377,22 @@ async function tests() {
380377
for await (const b of r) {
381378
}
382379
})();
380+
381+
await (async () => {
382+
console.log('destroy mid-stream does not error');
383+
const r = new Readable({
384+
objectMode: true,
385+
read() {
386+
this.push('asdf');
387+
this.push('hehe');
388+
}
389+
});
390+
391+
// eslint-disable-next-line no-unused-vars
392+
for await (const a of r) {
393+
r.destroy(null);
394+
}
395+
})();
383396
}
384397

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

0 commit comments

Comments
 (0)