Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,7 @@ function createAsyncIterator(stream, options) {

if (
(error || options?.destroyOnReturn !== false) &&
(error === undefined || stream._readableState.autoDestroy)
(error === undefined || stream._readableState.autoDestroy || stream.destroyed)
) {
destroyImpl.destroyer(stream, null);
} else {
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-stream-readable-async-iterators.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,29 @@ async function tests() {
assert.strictEqual(r.destroyed, true);
}

{
// An already destroyed stream with autoDestroy: false and an async _destroy
// must not emit an unhandled 'error' event after the iterator finishes.
// Refs: https://github.com/nodejs/node/issues/67418

const stream = new Readable({
read() {},
autoDestroy: false,
destroy(err, cb) {
setTimeout(() => cb(err), 10);
},
});

const expectedErr = new Error('stream error');
stream.destroy(expectedErr);

await assert.rejects(async () => {
for await (const chunk of stream) {
assert.fail(`unexpected chunk: ${chunk}`);
}
}, expectedErr);
}

{
// Don't destroy before 'end'.

Expand Down
Loading