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: #42320
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Nitzan Uziely <linkgoron@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
KhooHaoYit authored and bengl committed Mar 21, 2022
1 parent 3bd0078 commit 5367002
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 5367002

Please sign in to comment.