Skip to content

Commit

Permalink
stream: do cleanup when iterator is destroyed
Browse files Browse the repository at this point in the history
PR-URL: nodejs#42320
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Nitzan Uziely <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
KhooHaoYit authored Mar 16, 2022
1 parent 6a51306 commit 6d3920d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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

0 comments on commit 6d3920d

Please sign in to comment.