-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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' can not read data if same FD is used for write and readFile #13572
Comments
Can reproduce with Node.js 8.1.0 on Windows 7 x64. This is something like #7099, but not valid for cc @nodejs/fs |
@thisconnect Flushing the data to the filesystem seems unneeded, the file size is correctly changed before reading, if I get this right: const fs = require('fs');
const path = require('path');
const filepath = path.resolve(__dirname, './test-write-and-read.txt');
const fd = fs.openSync(filepath, 'w+');
console.log(fs.statSync(filepath).size);
fs.writeSync(fd, 'FOOBAR');
console.log(fs.statSync(filepath).size);
console.log(fs.readFileSync(fd, { encoding: 'utf8' })); 0
6
// empty string |
Line 576 in 47b9772
Line 542 in 47b9772
Line 680 in 47b9772
Which may fall into this note from the doc:
Does the binding function equal
Line 348 in 47b9772
Line 426 in 47b9772
Line 458 in 47b9772
Line 396 in 47b9772
Does the binding function equal It seems we should transfer |
This happens because when called with an |
It seems this better be fixed than documented. |
@vsemozhetbyt Please read the discussion in that PR. TL;DR some |
Seems to be a duplicate of the #9671 |
@thisconnect are you seeing the same failure in 6.10.x? edit: this appears to fail on 6.10.x and 6.9.x... so it is the same older bug not a new regression |
@MylesBorins yes same on 6.10.x |
@vsemozhetbyt flushing was just an attempt to rule out this being a possible problem. I too get correct sizes with stats. |
After reading why to not use I wasn't aware about the fact that fd have something like a position or could open non-seekable files (sockets?). After reading through the issues libuv/libuv#1357 , #7099, #9671, #9699, #10809, #10853 As a naive Node.js user, this is what I suspect:
|
I pretty much agree with the expectations, FYI, here is some more info:
|
@sam-github Thanks for the info!
Just for the record. This behavior is unexpected for me and feels like a bug |
Should we create an issue to discuss what we're going to do with |
#10853 already dicusses, it could be reopened. |
IMO The expected cases should be fixed and behavior changed according to the current documentation. readFile should read whole file. writeFile should write whole file. The weird edge cases with non-seekable fd's (files without a beginning) should be documented (assuming they already kind of work as currently documented). #10853 is already too long and hard to follow. I suggest to open a new issue with a proposal like this (if you agree) that people can agree or decline. To me this is a bug and should be fixed in 6.x too EDIT: read @sam-github's summary #13572 (comment) |
@nodejs/fs What should happen with this? Close it? Doc change? Code change? Discussion issue? Something else? |
Below are some observations but to summarize: it doesn't matter what code changes we make, we'll still need to document the gotchas.
@bzoz Would it be possible to teach |
@bnoordhuis positional We can make OTOH, maybe we should just add |
New discussion is in #23433 so I'm going to close this out. |
Description:
#3163 added file descriptor support to *File() functions (for example
fs.readFile
).Issue: Writing then reading on the same FD does not read the data when using
fs.readFile(fd)
. Only reading (witout writing) works as expected.The demo opens a file descriptor, writes data to it and tries to read the data using the same FD and
fs.readFile
. The demo code uses sync methods for simplicity, non-sync version has the same behavior.Expected output:
Actual output:
(last line is missing the file content FOOBAR)
Note:
fs.fsync
seems to have no impact. Assuming this could help and flush the data to the filesystemThe text was updated successfully, but these errors were encountered: