Skip to content
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

Delete file after createWriteStream raises no error when stream.write('whatever') #18274

Closed
ghost opened this issue Jan 20, 2018 · 7 comments
Closed
Labels
fs Issues and PRs related to the fs subsystem / file system. question Issues that look for answers.

Comments

@ghost
Copy link

ghost commented Jan 20, 2018

I'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.

@addaleax addaleax added question Issues that look for answers. fs Issues and PRs related to the fs subsystem / file system. labels Jan 20, 2018
@addaleax
Copy link
Member

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
Copy link
Member

@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)

@ghost
Copy link
Author

ghost commented Jan 24, 2018 via email

@leodutra
Copy link

leodutra commented Oct 2, 2019

I can confirm this is still happening on Windows, when using https://github.com/sindresorhus/got
This is using Node stream package (https://github.com/sindresorhus/got/blob/master/source/as-stream.ts).

Whenever I start downloading and streaming to the opened file, and hard delete the file, no error is thrown.

@amitzur
Copy link
Contributor

amitzur commented 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
Copy link

rejetto commented Aug 9, 2022

@amitzur if this is not reopened, would you consider opening another issue with your example?

@amitzur
Copy link
Contributor

amitzur commented Aug 9, 2022

@rejetto makes sense. I will, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

4 participants