Skip to content

fs: fix process crash in fs.promises.writeFile#61849

Open
Han5991 wants to merge 1 commit intonodejs:mainfrom
Han5991:fix/fs-promises-writefile-stream-error
Open

fs: fix process crash in fs.promises.writeFile#61849
Han5991 wants to merge 1 commit intonodejs:mainfrom
Han5991:fix/fs-promises-writefile-stream-error

Conversation

@Han5991
Copy link
Contributor

@Han5991 Han5991 commented Feb 16, 2026

This commit fixes an issue where the process would crash if the input stream emitted an error while the file was being opened.

The issue was caused by a race condition where the stream would emit an 'error' event before the file handle was returned from open(). Since there were no error listeners attached to the stream at that point, the process would crash with an "Unhandled 'error' event".

This fix attaches a temporary error listener to the stream before calling open(). If the stream emits an error, the promise is rejected immediately. When open() completes (even if the stream already errored), the file handle is safely closed to prevent resource leaks.

Fixes: #58742

@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 Feb 16, 2026
@codecov
Copy link

codecov bot commented Feb 16, 2026

Codecov Report

❌ Patch coverage is 93.10345% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.72%. Comparing base (4f13746) to head (d9248e5).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/fs/promises.js 93.10% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #61849   +/-   ##
=======================================
  Coverage   89.72%   89.72%           
=======================================
  Files         675      675           
  Lines      204806   204834   +28     
  Branches    39355    39361    +6     
=======================================
+ Hits       183761   183787   +26     
- Misses      13330    13333    +3     
+ Partials     7715     7714    -1     
Files with missing lines Coverage Δ
lib/internal/fs/promises.js 98.61% <93.10%> (-0.13%) ⬇️

... and 29 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This commit fixes an issue where the process would crash if the input

stream emitted an error while the file was being opened.

Fixes: nodejs#58742
@Han5991 Han5991 force-pushed the fix/fs-promises-writefile-stream-error branch from 5df105d to d9248e5 Compare February 16, 2026 22:05
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