Skip to content

fs: handle early writeFile stream errors#63472

Draft
cookesan wants to merge 1 commit into
nodejs:mainfrom
cookesan:fs-glob-cache-seen-siblings
Draft

fs: handle early writeFile stream errors#63472
cookesan wants to merge 1 commit into
nodejs:mainfrom
cookesan:fs-glob-cache-seen-siblings

Conversation

@cookesan
Copy link
Copy Markdown
Contributor

The fs.promises.writeFile() stream path could miss a readable
error emitted before the destination file opened. The returned promise
could reject while the source stream error also surfaced as an uncaught
exception.

This attaches a temporary error listener for readable stream inputs
before opening the destination, checks recorded stream errors before and
during async iteration, and removes the listener when the write finishes
or when opening the destination fails. FileHandle.writeFile() uses the
same helper.

Tests cover early readable errors for path and FileHandle writes, plus
listener cleanup when opening the destination fails.

Tests:

ninja -C out/Release -j4
python3 tools/test.py --shell out/Release/node parallel/test-fs-promises-writefile parallel/test-fs-promises-file-handle-writeFile
make lint-js

Fixes: #58742

Attach a temporary error listener to readable stream inputs before
opening the destination file. This lets writeFile() reject with the
stream error instead of allowing an early source error to become an
uncaught exception.

Remove the listener when the write finishes or when opening the
destination fails.

Signed-off-by: cookesan <6601329+cookesan@users.noreply.github.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 May 21, 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.

fsPromise.writeFile should catch stream's error and reject the promise.

2 participants