-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fs: ignore fstat errors on user fds in fs.readFile{Sync} #20800
Conversation
This comment has been minimized.
This comment has been minimized.
cc @nodejs/fs |
lib/fs.js
Outdated
binding.fstat(fd, req); | ||
// On Windows fstat cannot be called on std fds, so just use 0 as its size, | ||
// which would result in a list of buffers to be concatenated | ||
if (isStdFD(fd)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's just Windows, should we be checking process.platform === 'win32' && isStdFD(fd)
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mscdex hmm, yes, it would probably favor the case on non-windows platforms where the stdin is redirected from a regular file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, the check probably is unnecessary since on Windows fstat
still works on stdin if it's redirected. The isUserFd
check should already be enough in case of errors.
7bb7697
to
d9aa3e3
Compare
Updated to just ignore fstat errors on user-provided fds. In cases where the std fds are redirected from regular files, we may still be able to know the size of the buffer to allocate. |
I'm confused as to why 0 size is now being forced for all user-supplied file descriptors if this is specifically about stdin/stdout/stderr. Also, if this is only a problem on Windows, I still think we should only be bypassing |
Lastly, if this is only a problem in only some situations with stdin/stdout/stderr on Windows, why not just try |
The size is only assumed 0 when the user-provided fd cannot be |
That is more-or-less what this PR does at the moment. Difference is it does not error out right away when |
I still think we should be more strict when enabling this behavior, checking platform, error type, whether it's an std{in,out,err} fd, etc. No matter what I think this should be semver-major. |
I agree but the complexity should probably be best addressed in |
I'm not so sure that it makes sense to change it in libuv, since reporting a size of 0 but the fd still being readable seems contradictory? |
stdin without being directed on non-windows platforms also has size 0 in fstat so that doesn't seem to be an issue. (also, the fact that you can get an EISDIR with fstat is...odd) |
This might be a silly question, but why are we trying to get the file size in advance anyway? I could imagine it is to save on the # of Buffer allocations, but I don’t think it’s really necessary, right? |
@addaleax Definitely not a silly question. The size is used to
|
If we want to eliminate the need to know the size beforehand I think we could only go with 3 (always use list of buffers to deal with unknown size), but that adds the overhead of concatenation for the most common cases (reading a regular file) |
Ping @addaleax |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about the ping (I didn’t block this & my questions had been answered) but LGTM. Still needs a rebase, though. :)
Note that on Windows, calling fstat on std fds that are not redirected returns EISDIR but they can still be read, so we just need to avoid treating them as regular files.
Is that a bug in libuv?
I thinks so, at least |
On Windows fstat cannot be called on std fds. In those cases, we already know that the fd is not for a regular file and can just use a list of buffers to store the data. Also makes sure that we don't close fds provided by users if we cannot call fstat on them.
d9aa3e3
to
15e8393
Compare
Ping @joyeecheung |
@joyeecheung Still working on this? |
@Trott Not really...I'll close this |
On Windows fstat cannot be called on std fds. In those cases,
we already know that the fd is not for a regular file and can
just use a list of buffers to store the data.
Also makes sure that we don't close fds provided by users
if we cannot call fstat on them.
Fixes: #19831
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesNote that this does not actually fix #19831 (comment) (on Windows calling
fstat
on those fds returnsEISDIR
..even if errors are intended,EISDIR
is a rather odd error for this)