Repository navigation
Stream destruction breaks async-iteration and ends nodejs event loop #23730
Copy link
Copy link
Closed
Labels
confirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.streamIssues and PRs related to Node.js streams.Issues and PRs related to Node.js streams.
Description
Activity
- addedstreamIssues and PRs related to Node.js streams.Issues and PRs related to Node.js streams.
on Oct 18, 2018 @nodejs/streams
Simplified repro:
(async () => { var stream = require('fs').createReadStream(__filename); stream.destroy(); await stream[Symbol.asyncIterator]().next(); console.log('Done'); })().catch(console.log);
The
resolvefunction assigned toiterator[kLastResolve]never gets called:node/lib/internal/streams/async_iterator.js
Lines 143 to 152 in 05394d2
const data = iterator[kStream].read(); if (data) { iterator[kLastPromise] = null; iterator[kLastResolve] = null; iterator[kLastReject] = null; resolve(createIterResult(data, false)); } else { iterator[kLastResolve] = resolve; iterator[kLastReject] = reject; } - addedconfirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.
on Oct 18, 2018 Definitely it's a bug, as @Hakerh400 showed.
@mika-fischer the event loop is shutting down because there is nothing asynchronous left to do. If you add the following:
setInterval(() => { console.log('a second passed') }, 1000)
The application will keep running
See #23785 for a fix.
Fixed in 3ec8cec.
- added a commit that references this issue
on Oct 24, 2018 - added a commit that references this issue
on Oct 24, 2018 - added a commit that references this issue
on Nov 26, 2018 - added a commit that references this issue
on Nov 28, 2018 - added a commit that references this issue
on Nov 29, 2018
Metadata
Metadata
Assignees
Labels
confirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.streamIssues and PRs related to Node.js streams.Issues and PRs related to Node.js streams.
Async iteration over a readable stream breaks horribly if for any reason the stream is destroyed while the iteration is still running (or even if it is destroyed before starting the iteration). I would expect that the iteration runs normally or throws an exception. What happens instead is that the whole event loop is shut down.
Most notably this makes async iteration useless with
pipeline.Test code:
Output: