Skip to content

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

Closed
@cjihrig

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

added
feature requestIssues that request new features to be added to Node.js.
on Sep 26, 2023
added
fsIssues and PRs related to the fs subsystem / file system.
on Sep 26, 2023
aduh95

aduh95 commented on Sep 26, 2023

@aduh95
Contributor

/cc @nodejs/fs

bnoordhuis

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.

mcollina

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.

benjamingr

benjamingr commented on Sep 27, 2023

@benjamingr
Member

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

cjihrig

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.
added a commit that references this issue on Oct 2, 2023
added a commit that references this issue on Oct 2, 2023
added a commit that references this issue on Oct 8, 2023

23 remaining items

Loading
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 that request new features to be added to Node.js.fsIssues and PRs related to the fs subsystem / file system.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

      Participants

      @mcollina@bnoordhuis@colemickens@benjamingr@cjihrig

      Issue actions

        request: add option to call fsync() when closing files · Issue #49886 · nodejs/node