Skip to content

fs: honor flush option in writeFileSync utf8 fast path#63887

Open
MarshallOfSound wants to merge 1 commit into
nodejs:mainfrom
MarshallOfSound:fix/writefilesync-flush
Open

fs: honor flush option in writeFileSync utf8 fast path#63887
MarshallOfSound wants to merge 1 commit into
nodejs:mainfrom
MarshallOfSound:fix/writefilesync-flush

Conversation

@MarshallOfSound

Copy link
Copy Markdown
Member

Description of Change

fs.writeFileSync(path, data, { encoding: 'utf8', flush: true }) does not fsync. The C++ utf8 fast path (4466dee) skips the JS write loop that performs the flush, and its gate never checked the flush option (e01c1d7, which predates it). The same call without an explicit encoding misses the fast path and flushes correctly. Verified with strace: zero fsync syscalls on the fast path, one on the slow path.

This PR skips the fast path when flush: true is set, routing those writes through the existing open/write/fsync/close path. appendFileSync delegates to writeFileSync and is fixed by the same change.

Affects every release line since v21.3.0.

Extended the existing flush tests (test-fs-write-file-flush.js, test-fs-append-file-flush.js) with the explicit-utf8-encoding case — the only combination that takes the fast path, and exactly the cell their matrices didn't cover. Both new cases fail without this change.

Checklist

  • I have built and tested this change
  • tests are changed or added
  • test/parallel/test-fs-* pass

The C++ utf8 fast path added in 4466dee does not perform an
fsync, but the gate routing writeFileSync through it did not consult
the flush option added in e01c1d7. As a result
writeFileSync(path, data, { encoding: 'utf8', flush: true }) wrote
without flushing, while the same call without an explicit encoding
flushed correctly. Skip the fast path when flush is requested.

appendFileSync delegates to writeFileSync and was affected the same
way. Extend the existing flush tests with the explicit-utf8-encoding
case for both; the new cases fail without this change.

Signed-off-by: Sam Attard <sattard@anthropic.com>
@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. labels Jun 13, 2026
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. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants