GeezerVison is showing github.com — Delete file after createWriteStream raises no error when stream.write('whatever') · Issue #18274 · nodejs/node
Delete file after createWriteStream raises no error when stream.write('whatever') #18274Labels fsIssues and PRs related to file-system APIs and the fs module.questionIssues asking questions about Node.js. DescriptionI'm on a mac, Node v8.6.0.
Briefly, if I do this in mocha :
import fs from 'fs-extra'
const stream = fse.createWriteStream(
path.normalize('./myfile.txt'),
{flags: 'a', encoding: 'utf8', mode: 0o0666}
)
stream.on('error', err => {
// this is never called
console.log(err)
})
fs.unlinkSync('./myfile.txt')
try{
stream.write('whatever', () => {
console.log('All is well.')
})
}
catch (err) {
assert.isUndefined(err)
}
const stream = fse.createWriteStream(
path.normalize('./myfile.txt'),
{flags: 'a', encoding: UTF8, mode: FILE_MODE}
)
stream.on('error', err => {
assert.isNotOk(err)
})
// !!! I delete the stream`s file
fs.unlinkSync('./myfile.txt')
try{
stream.write('whatever')
}
catch (err) {
assert.isUndefined(err)
}
the test passes.
I don't thimk this has anything to do with importing fs-extra. It's just a wrapper for fs.
Is this a normal behaviour? Shouldn't there be some way to prevent this kind of error?
Thank you for your time. Activityadded questionIssues asking questions about Node.js. fsIssues and PRs related to file-system APIs and the fs module. on Jan 20, 2018addaleax commented on Jan 20, 2018 Hi! A few things:
- If you have an issue and you think it is a problem with Node, try to provide a test file that doesn’t use external modules (like
fs-extra) or requires transpilation to work.
fs.createWriteStream() is not a synchronous operation – it’s possible that the fs.unlinkSync() call would always be run before the file is actually opened.
And even if it does run while the stream is open, then no, at least on non-Windows systems (don’t know about others) this is the expected behavior.
There’s a reason the operation is called unlink() and not delete() or something like that: “Removing” a file on a POSIX operating system means decreasing a reference count, not actually deleting it right away. The file is only removed once all references to it on the hard drive are gone and all programs that access the file have released their handles to them, see e.g. https://linux.die.net/man/2/unlink addaleax commented on Jan 24, 2018 @vladblindu I’m closing this in the hope that the question is answered for you. If not, please feel free to comment again (here or, if you have more questions, on the https://github.com/nodejs/help repository) leodutra commented on Oct 2, 2019 amitzur commented on Jul 26, 2022 This is happening to me as well. To reproduce:
const fs = require('fs')
const os = require('os')
const filepath = 'x.log'
const writer = fs.createWriteStream(filepath, {flags: 'a', encoding: 'utf8'})
setInterval(() => {
writer.write(`${Date.now()}${os.EOL}`, err => {
console.log('err', err);
});
}, 1000)
setTimeout(() => {
fs.unlinkSync(filepath)
}, 3000)
I'm running on Linux Ubuntu 20.04 and getting this output:
err undefined
err undefined
err undefined
err undefined
err undefined
err undefined
err undefined
...
Even though I would expect to see an exception from the writer.write after the file was deleted. rejetto commented on Aug 9, 2022 @amitzur if this is not reopened, would you consider opening another issue with your example? amitzur commented on Aug 9, 2022
MetadataMetadatafsIssues and PRs related to file-system APIs and the fs module.questionIssues asking questions about Node.js. DevelopmentNo branches or pull requestsIssue actions
|