nodejs / nodejs/node

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

Open
#64,367 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

fs
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

Version

v24.18.0

Platform
Windows 11 x64
Subsystem

fs

What steps will reproduce the bug?

'use strict';

const fs = require('node:fs');

fs.writeFileSync('repro.txt', 'Hello world');

fs.open('repro.txt', 'r', (err, fd) => {
if (err) throw err;

fs.readFile(fd, { buffer: Buffer.alloc(5) }, (err, data) => {
console.log('err:', err && err.code);
console.log('data:', data && data.toString());
fs.closeSync(fd);
});
});

How often does it reproduce? Is there a required condition?

Always, when fs.readFile() is called with:
a numeric file descriptor, and
options.buffer, and
the supplied buffer is too small for the file

What is the expected behavior? Why is that the expected behavior?

The call should fail with ERR_INVALID_ARG_VALUE because the provided buffer is too small to contain the full file contents.

What do you see instead?

The error is dropped for user-supplied file descriptors, and the callback can complete successfully instead of reporting ERR_INVALID_ARG_VALUE.

Additional information

Root cause appears to be in lib/internal/fs/read/context.js (lines 180-194): ReadFileContext.close(err) ignores err when isUserFd is true, so the final callback runs as if no failure occurred.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied JavaScript reproduction on Node.js, then inspect lib/internal/fs/read/context.js around lines 180-194, especially ReadFileContext.close(err) for user file descriptors. Done means the numeric-fd call with an undersized buffer reports ERR_INVALID_ARG_VALUE instead of completing successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.