Repository navigation
Inconsistent stream events using destroy method #25373
Description
Activity
- addedquestionIssues asking questions about Node.js.Issues asking questions about Node.js.streamIssues and PRs related to Node.js streams.Issues and PRs related to Node.js streams.
on Jan 8, 2019 cc @nodejs/streams
'close'is always emitted both during normal or error shutdown. This behavior has been added in #18438. Also #19836 is relevant. Both of those are semver-major changes, and the behavior in Node 10 simplifies event management and error handling. If you would like to have the same behavior on Node 8, you can use the http://npm.im/readable-stream module.This sentence is in the docs:
Not all Writable streams will emit the 'close' event.
needs to be updated as all streams will emit
'close'(because of the changes in the above PRs) if theemitClose: trueoption is passed in. This istrueby default.As for official Node.js docs, with a custom Writable, the only standard way to know if the stream has finished, is to listen the finish event.
That is not true, as the docs report:
The 'finish' event is emitted after the stream.end() method has been called, and all data has been flushed to the underlying system.
It does not mention anything related to the error conditions. I'm not 100% sure why
'finish'is emitted on Node 8 when an'error'happens, and it's likely a bug (which we should likely not fix, because 8 has been around for so long).Thank you for the response.
Just for info, I've noticed the same behavior with Readable streams. I've also found a workaround, a setImmediate (nextTick does not work) on end, finish and close event callbacks does the trick.
- added a commit that references this issue
on Jan 14, 2019 - added a commit that references this issue
on Jan 16, 2019 - added a commit that references this issue
on Apr 28, 2019 - added a commit that references this issue
on May 10, 2019 - added a commit that references this issue
on May 16, 2019
As for official Node.js docs, with a custom Writable, the only standard way to know if the stream has finished, is to listen the finish event.
The code above works correctly on both node 8 and 10, with the finish event emitted.
The problem I've noticed is during the usage of the destroy() method, on node 8 the stream will emit first the finish event, then a error event, and no close event.
On node 10, the finish event is not emitted.
I'm not sure if the problem is node 8 or 10, but by reading the docs I suppose the finish event have to be emitted after a possible error.