Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: do cleanup when iterator is destroyed #42320

Merged
merged 3 commits into from
Mar 16, 2022
Merged
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
5 changes: 4 additions & 1 deletion lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ async function* createAsyncIterator(stream, options) {
stream.on('readable', next);

let error;
eos(stream, { writable: false }, (err) => {
const cleanup = eos(stream, { writable: false }, (err) => {
error = err ? aggregateTwoErrors(error, err) : null;
callback();
callback = nop;
Expand Down Expand Up @@ -1150,6 +1150,9 @@ async function* createAsyncIterator(stream, options) {
(error === undefined || stream._readableState.autoDestroy)
) {
destroyImpl.destroyer(stream, null);
} else {
stream.off('readable', next);
cleanup();
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-stream-readable-async-iterators.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,20 @@ async function tests() {
}
);
}

// Check for dangling listeners
(async function() {
const readable = createReadable();
const opts = { destroyOnReturn: false };
while (readable.readable) {
// eslint-disable-next-line no-unused-vars
for await (const chunk of readable.iterator(opts)) {
break;
}
}

assert.deepStrictEqual(readable.eventNames(), []);
})().then(common.mustCall());
}

{
Expand Down