Skip to content

fs: preserve readFile() errors for numeric file descriptors#64368

Open
jibin7jose wants to merge 2 commits into
nodejs:mainfrom
jibin7jose:fix-fs-readfile-buffer-fd-error
Open

fs: preserve readFile() errors for numeric file descriptors#64368
jibin7jose wants to merge 2 commits into
nodejs:mainfrom
jibin7jose:fix-fs-readfile-buffer-fd-error

Conversation

@jibin7jose

@jibin7jose jibin7jose commented Jul 8, 2026

Copy link
Copy Markdown

Summary

fs.readFile() with a numeric file descriptor and the new buffer option could lose errors on the user-fd path.

Root cause

In lib/internal/fs/read/context.js, ReadFileContext.close(err) did not forward err when isUserFd was true. As a result, failures that should have reached the callback, including an undersized caller-provided buffer, could be treated as successful completion.

Fix

Forward the close error for user-supplied file descriptors so the final callback receives the actual failure instead of silently succeeding.

Tests

Add regression coverage for the numeric file descriptor path with:

  • a preallocated buffer
  • a function-returned buffer
  • a buffer that is too small

This verifies the error is preserved for the callback-based API.

Fixes: #64367

@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 Jul 8, 2026
@jibin7jose jibin7jose changed the title fs: preserve readFile() errors for numeric fds fs: preserve readFile() errors for numeric file descriptors Jul 8, 2026
@jibin7jose

Copy link
Copy Markdown
Author

Hi @joyeecheung, could you please take a look at this PR when you have a chance? It fixes an issue where fs.readFile() with a numeric file descriptor and the new buffer option could lose errors on the user-FD path. I've added regression tests covering the affected scenarios. Any feedback would be greatly appreciated. Thank you!

@joyeecheung joyeecheung left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not very familiar with this code any more, but it seems straightforward enough and from a glance it makes sense to me. Would be better if @addaleax can take a look since it was introduced in #35487

Comment on lines +29 to +32
fs.readFile(fd, options, (err, data) => {
if (err) reject(err);
else resolve(data);
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fs.readFile(fd, options, (err, data) => {
if (err) reject(err);
else resolve(data);
});
fs.readFile(fd, options, common.mustCall((err, data) => {
if (err) reject(err);
else resolve(data);
}));

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I've updated the code to wrap the callback with common.mustCall() as suggested. Thanks!

@jibin7jose jibin7jose requested a review from joyeecheung July 10, 2026 03:07
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.

fs: preserve readFile() errors when using a numeric fd and buffer option

3 participants