Skip to content
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: make sure to write entire buffer #42434

Closed
wants to merge 13 commits into from
Closed

Conversation

ronag
Copy link
Member

@ronag ronag commented Mar 22, 2022

fs.write(v) is not guaranteed to write everything in a single
call. Make sure we don't assume so.

fs.write(v) is not guaranteed to write everything in a single
call. Make sure we don't assume so.
@ronag ronag added the fs Issues and PRs related to the fs subsystem / file system. label Mar 22, 2022
@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Mar 22, 2022
lib/internal/fs/streams.js Show resolved Hide resolved
lib/internal/fs/streams.js Show resolved Hide resolved
@ronag ronag requested a review from mcollina March 22, 2022 13:18
retries = bytesWritten ? 0 : retries + 1

if (retries > 5) {
return cb(new Error('writev failed'));
Copy link
Member

Choose a reason for hiding this comment

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

What are we trying for 5 times here? Writing a non-zero number of bytes?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes

Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a comment here.

retries = bytesWritten ? 0 : retries + 1

if (retries > 5) {
return cb(new Error('writev failed'));
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a comment here.

}

if (bytesWritten < size) {
writevAll([Buffer.concat(buffers).slice(bytesWritten)], pos + bytesWritten, cb, retries);
Copy link
Member

Choose a reason for hiding this comment

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

I would recommend being a bit more subtle here and avoid the massive slowdown. Worst case you are allocating 2x memory to pick one byte. I would recommend just concatenating the buffers that have not been completely written.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a cold path though? It should almost never happen. Is it worth optimizing for?

pos += bytesWritten;

if (retries > 5) {
cb(new Error('writev failed'));
Copy link
Member Author

Choose a reason for hiding this comment

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

I need a little help here... not sure what error to use

Copy link
Member

Choose a reason for hiding this comment

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

Lemme check

Copy link
Member

Choose a reason for hiding this comment

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

Checked the existing codes, no idea either 😅

if (retries > 5) {
cb(new Error('writev failed'));
} else if (size) {
writevAll([Buffer.concat(buffers).slice(bytesWritten)], size, pos, cb, retries);
Copy link
Contributor

@mscdex mscdex Mar 22, 2022

Choose a reason for hiding this comment

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

If we're going to be concatenating, we may as well just do it once and call writeAll() with it instead. Otherwise like @mcollina said, we should handle this more efficiently.

Ideally it would be great if we didn't have to even keep recreating arrays on retry. That might be a nice addition to the fs.writev() API to make it more like fs.write() with its offset parameter, so we could just pass a starting index or something. With something like that all we'd have to do is possibly slice() a single buffer (or simply increasing the starting index) before retrying.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a cold path though? It should almost never happen. Is it worth optimizing for?

Copy link
Member

Choose a reason for hiding this comment

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

It should almost never happen

It think it would be useful to get an idea of how often it happens (I personally have no idea). How cold is it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, if it does happen often then we have a lot of seriously broken software out there... I guess we would have quite a bit of reports....

Copy link
Contributor

@mscdex mscdex Mar 22, 2022

Choose a reason for hiding this comment

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

or people have learned to handle retries on their own?

Copy link
Member Author

@ronag ronag Mar 22, 2022

Choose a reason for hiding this comment

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

I don’t think you can actually detect that in this interface… there is nothing a user can do… it will just be corrupt w/o any way to detect or recover… so no

Copy link
Member

Choose a reason for hiding this comment

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

Well, if it does happen often then we have a lot of seriously broken software out there... I guess we would have quite a bit of reports....

makes sense, yeah sounds like a cold path to me.

@mscdex
Copy link
Contributor

mscdex commented Mar 22, 2022

Needs a test as well.

pos += bytesWritten;

if (retries > 5) {
cb(new Error('writev failed'));
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be a proper error with code?

Copy link
Member Author

Choose a reason for hiding this comment

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

Suggestions?

@mscdex mscdex added the needs-benchmark-ci PR that need a benchmark CI run. label Mar 22, 2022
lib/internal/fs/promises.js Outdated Show resolved Hide resolved
Co-authored-by: Antoine du Hamel <[email protected]>
}

if (this.destroyed || er) {
return cb(er);
Copy link
Contributor

Choose a reason for hiding this comment

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

will er here be something if this.destroyed is truthy? Otherwise, this could signal to the caller that the call succeeded?

@ronag
Copy link
Member Author

ronag commented Oct 27, 2022

@nodejs/fs someone willing to help get this over the finish line? I still think it's a potential bug.

@benjamingr
Copy link
Member

Maybe @rluvaton or @atlowChemi ?

@rluvaton
Copy link
Member

What help is needed?

@ronag
Copy link
Member Author

ronag commented Aug 14, 2023

Linting and Tests

@atlowChemi
Copy link
Member

@nodejs/fs someone willing to help get this over the finish line? I still think it's a potential bug.

@ronag Can you check the Allow edits and access to secrets by maintainers?
If there is another way for me to push changes to the branch let me know 🙂

@ronag
Copy link
Member Author

ronag commented Aug 17, 2023

@nodejs/fs someone willing to help get this over the finish line? I still think it's a potential bug.

@ronag Can you check the Allow edits and access to secrets by maintainers? If there is another way for me to push changes to the branch let me know 🙂

Hm. I don't know how to do that. Can you just push to a new branch/PR?

@aduh95
Copy link
Contributor

aduh95 commented Sep 20, 2023

Superseded by #49211

@aduh95 aduh95 closed this Sep 20, 2023
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-benchmark-ci PR that need a benchmark CI run. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants