GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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') #18274

New issue
Copy link
New issue
Copy link
Closed
Closed
Delete file after createWriteStream raises no error when stream.write('whatever')#18274
Copy link
Labels
fsIssues and PRs related to file-system APIs and the fs module.questionIssues asking questions about Node.js.

Description

@ghost
ghost
opened on 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.

Activity

  1. addaleax
    added
    questionIssues asking questions about Node.js.
    fsIssues and PRs related to file-system APIs and the fs module.
    on Jan 20, 2018
  2. addaleax commented on Jan 20, 2018

    @addaleax
    addaleax
    on Jan 20, 2018
    Member
    More actions

    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

  3. addaleax commented on Jan 24, 2018

    @addaleax
    addaleax
    on Jan 24, 2018
    Member
    More actions

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

  4. addaleax
    closed this as completedon Jan 24, 2018
  5. leodutra commented on Oct 2, 2019

    @leodutra
    leodutra
    on Oct 2, 2019
    More actions

    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.

  6. amitzur commented on Jul 26, 2022

    @amitzur
    amitzur
    on Jul 26, 2022
    Contributor
    More actions

    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.

  7. rejetto commented on Aug 9, 2022

    @rejetto
    rejetto
    on Aug 9, 2022
    More actions

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

  8. amitzur commented on Aug 9, 2022

    @amitzur
    amitzur
    on Aug 9, 2022
    Contributor
    More actions

    @rejetto makes sense. I will, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to file-system APIs and the fs module.questionIssues asking questions about Node.js.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.