Skip to content

request: add option to call fsync() when closing files #49886

Description

@cjihrig

What is the problem this feature will solve?

I believe it is possible that Node can close a file and then try to interact with the same file before all of the data has been written to disk. According to the docs for close(2) this can be fixed by calling fsync().

What is the feature you are proposing to solve the problem?

Add an option to the appropriate fs functions (close(), writeFile(), etc.) to call fsync() automatically.

What alternatives have you considered?

Flushing the data "by hand."

Activity

  1. added
    fsIssues and PRs related to file-system APIs and the fs module.
    on Sep 26, 2023
  2. aduh95 commented on Sep 26, 2023

    @aduh95
    Contributor

    /cc @nodejs/fs

  3. bnoordhuis commented on Sep 26, 2023

    @bnoordhuis
    Member

    For fs.writeFile() that makes sense, it's a sequence of system calls.

    For fs.close() it makes less sense, IMO. Just call fs.fsync() before fs.close().

    See also #28513 about interleaving fs.fsync() and fs.WriteStream.

  4. mcollina commented on Sep 27, 2023

    @mcollina
    SponsorMember

    I think adding an option to fs.WriteStream would be helpful too.

    @bnoordhuis as a side note, I have been experiencing significant read-after-write issues when using Node.js fs APIs since the move to IO_URING. It's too fast and the filesystem is not flushed.

  5. benjamingr commented on Sep 27, 2023

    @benjamingr
    Member

    I think writeFile and fs.WriteStream makes sense (especially writeFile)

  6. cjihrig commented on Oct 1, 2023

    @cjihrig
    ContributorAuthor

    In addition to writeFile() and fs.WriteStream, there are a few other APIs where this could make sense:

    • appendFile() - calls writeFile() under the hood with a modified copy of its options. This one should Just Work™, but should include tests as well.
    • truncate() - opens the file, calls ftruncate(), and then closes the file.
    • lchmod() - opens the file, calls fchmod(), and then closes the file. I'm less sure if it is necessary here.
    • copyFile() - this one is tricky because it is implemented several different ways in libuv and Node never has access to the file descriptor.
  7. 6 remaining items

  8. colemickens commented on Oct 24, 2023

    @colemickens

    Sorry, I didn't realize that the new option in node 21 was a result of this thread. There's no remaining question, now.

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

    feature requestIssues requesting new Node.js features.fsIssues and PRs related to file-system APIs and the fs module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions