-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Event 'close' emits twice (fs.createWriteStream) #31366
Comments
const fs = require('fs')
const fileReadStream = fs.createReadStream('1.in')
const fileWriteStream = fs.createWriteStream('1.out', {
emitClose: true
})
fileReadStream.pipe(fileWriteStream)
fileWriteStream.on('close', function () {
console.log('fileWriteStream.on(close)')
}) fileWriteStream.on(close)
fileWriteStream.on(close) with node |
more examples: const fs = require('fs')
const fileWriteStream = fs.createWriteStream('1.out', {
emitClose: true
})
fileWriteStream.on('close', () => {
console.log('called')
})
fileWriteStream.write('1')
fileWriteStream.write('2')
fileWriteStream.end() |
const fs = require('fs')
const fileWriteStream = fs.createWriteStream('1.out', {
emitClose: true
})
fileWriteStream.on('close', () => {
console.log('called')
})
fileWriteStream.write('1')
fileWriteStream.write('2')
fileWriteStream.destroy() events.js:298
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
?[90m at WriteStream._write (internal/fs/streams.js:415:33)?[39m
?[90m at WriteStream.<anonymous> (internal/fs/streams.js:411:12)?[39m
?[90m at Object.onceWrapper (events.js:428:26)?[39m
?[90m at WriteStream.emit (events.js:333:22)?[39m
?[90m at internal/fs/streams.js:402:12?[39m
?[90m at FSReqCallback.oncomplete (fs.js:154:23)?[39m
Emitted 'error' event on WriteStream instance at:
?[90m at errorOrDestroy (internal/streams/destroy.js:128:12)?[39m
?[90m at onwriteError (_stream_writable.js:462:3)?[39m
?[90m at onwrite (_stream_writable.js:483:7)?[39m
?[90m at WriteStream._write (internal/fs/streams.js:415:30)?[39m
?[90m at WriteStream.<anonymous> (internal/fs/streams.js:411:12)?[39m
[... lines matching original stack trace ...]
?[90m at FSReqCallback.oncomplete (fs.js:154:23)?[39m {
code: ?[32m'ERR_STREAM_DESTROYED'?[39m
} |
Should i close issue or developers do it? |
const fileWriteStream = fs.createWriteStream('1.out', {
emitClose: true
}) Don't use Preferrably it shouldn't even be forwarded. |
fs streams have some backwards compat behavior that does not behave well if emitClose: true is passed in options. This fixes this edge case until the backwards compat is removed. Fixes: nodejs#31366
I tried without |
if i set (emitClose: false) then on version 12.14.1 event emits once.
Documentation: By default, the stream will not emit a 'close' event after it has been destroyed. This is the opposite of the default for other Writable streams. Set the emitClose option to true to change this behavior.
const fileWriteStream = fs.createWriteStream(saveTo, {emitClose: true});
file.pipe(fileWriteStream);
fileWriteStream.on('close', function () {
console.log('fileWriteStream.on(close')
}
The text was updated successfully, but these errors were encountered: