Repository navigation
fs/promises's readFile returns truncated content for files in /proc/net/ #50437
Description
Activity
- addedconfirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.fsIssues and PRs related to file-system APIs and the fs module.Issues and PRs related to file-system APIs and the fs module.promisesIssues and PRs related to ECMAScript promises.Issues and PRs related to ECMAScript promises.
on Oct 28, 2023 Can confirm. The bug here is that the promise-based version allocates a 64k buffer and thinks it's done when it reads less than that. That optimization is valid for regular files but not for files under /proc. It should keep reading until EOF (zero read.)
Reacted by Benjamin Gruenbaum, ExE Boss and Florian Prud'homme(rookie question) Is there a way to way to check whether the file we are reading is one of these special files? other than maybe checking the path?
Checking the path isn't enough because a procfs can be mounted anywhere; /proc is just the most common.
Checking
fs.statfsSync(path).type === 0x9FA0(PROC_SUPER_MAGIC) works but isn't race-free.I'm not 100% sure if it's okay to assume that
fs.fstatSync(fd).dev === 22means the file is on procfs.Reacted by Benjamin GruenbaumForgot to mention, linux has a
fstatfs(fd)syscall but that isn't exposed by libuv. Would IMO be okay to use in a linux-only code path though.edit: having said that... it's easier all around to simply read until EOF. Zero-sized files are rare enough that they aren't worth optimizing for. You're making at least two system calls either way.
Reacted by Benjamin Gruenbaum, ExE Boss, Juan José and CattChen@nodejs/fs
- addedlinuxIssues and PRs related to the Linux platform.Issues and PRs related to the Linux platform.
on Nov 19, 2023 Hi! It's been a few months since any activity on this issue. I just attempted to reproduce in v22.6.0, and I was unable to do:
require("fs/promises").readFile("/proc/net/unix", "utf-8").then(x => console.log(x.length)) require("fs").readFile("/proc/net/unix", "utf-8", (_, x) => console.log(x.length))
$ node index.js 75897 75897
For that reason, I'm optimistically closing this issue, but feel free to reopen.
Version
v21.1.0
Platform
Linux ubuntu 6.2.0-35-generic #35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Oct 6 10:23:26 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
Subsystem
fs/promises
What steps will reproduce the bug?
Compare the length of the content returned by
fs/promises.readFileandfs.readFilewhen reading any file larger than 4 KiB in/proc/net.fs/promisesonly returns the first 4 KiB or so.How often does it reproduce? Is there a required condition?
Always
What is the expected behavior? Why is that the expected behavior?
fs/promises'sreadFileandfs.readFilereturn the same untruncated file contents.What do you see instead?
fs/promises'sreadFiletruncates procfs file contents to around 4 KiB.Additional information
No response