Skip to content

Commit a09e59e

Browse files
committed
stream: fix async iterator destroyed error propagation
There was an edge case where if _destroy calls the error callback later than one tick the iterator would complete early and not propgate the error.
1 parent b0a7621 commit a09e59e

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

lib/internal/streams/async_iterator.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,20 @@ const ReadableStreamAsyncIteratorPrototype = ObjectSetPrototypeOf({
7878
}
7979

8080
if (this[kStream].destroyed) {
81-
// We need to defer via nextTick because if .destroy(err) is
82-
// called, the error will be emitted via nextTick, and
83-
// we cannot guarantee that there is no error lingering around
84-
// waiting to be emitted.
8581
return new Promise((resolve, reject) => {
86-
process.nextTick(() => {
87-
if (this[kError]) {
88-
reject(this[kError]);
89-
} else {
90-
resolve(createIterResult(undefined, true));
91-
}
92-
});
82+
if (this[kError]) {
83+
reject(this[kError]);
84+
} else if (this[kEnded]) {
85+
resolve(createIterResult(undefined, true));
86+
} else {
87+
finished(this[kStream], (err) => {
88+
if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') {
89+
reject(err);
90+
} else {
91+
resolve(createIterResult(undefined, true));
92+
}
93+
});
94+
}
9395
});
9496
}
9597

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

+16
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,22 @@ async function tests() {
484484
assert.strictEqual(e, err);
485485
})()]);
486486
}
487+
488+
{
489+
const r = new Readable({
490+
read () {
491+
},
492+
destroy(err, callback) {
493+
setTimeout(() => callback(new Error('asd')), 1);
494+
}
495+
});
496+
497+
r.destroy();
498+
const it = r[Symbol.asyncIterator]();
499+
it.next().catch(common.mustCall((err) => {
500+
assert.strictEqual(err.message, 'asd');
501+
}));
502+
}
487503
}
488504

489505
{

0 commit comments

Comments
 (0)