Skip to content

Commit 499c41d

Browse files
ronagMylesBorins
authored andcommitted
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. PR-URL: #31314 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 4981f97 commit 499c41d

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-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

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

489506
{

0 commit comments

Comments
 (0)