GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fs: WriteStream should handle partial writes by kyliau · Pull Request #22740 · nodejs/node

Conversation

@kyliau

@kyliau kyliau commented Sep 6, 2018

Copy link
Copy Markdown

Given a buffer of length l, fs.write() will not necessarily write the
entire buffer to the file. This can occur if, for example, there is
insufficient space on the underlying physical medium.

WriteStream did not handle this case, and when partial write occurs,
it will errorneously report that the write is successful.

This commit changes the _write() behavior to continue the write
operation, picking up from where the last operation left off.

More information about the write() system call is available at
http://man7.org/linux/man-pages/man2/write.2.html

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@nodejs-github-bot nodejs-github-bot added the fs Issues and PRs related to file-system APIs and the fs module. label Sep 6, 2018
TimothyGu
TimothyGu reviewed Sep 7, 2018
View reviewed changes
Comment thread test/parallel/test-fs-write-stream-partial-write.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

TimothyGu
TimothyGu reviewed Sep 7, 2018
View reviewed changes
Comment thread lib/internal/fs/streams.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

ofrobots
ofrobots reviewed Sep 13, 2018
View reviewed changes

@ofrobots ofrobots left a comment

Copy link
Copy Markdown
Contributor

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.

Thanks for contributing a fix for this bug @kyliau ! I left a comment, but otherwise this is looking great. Please take a look.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread lib/internal/fs/streams.js Outdated

@ofrobots ofrobots Sep 13, 2018

Copy link
Copy Markdown
Contributor

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.

Note that pos may be undefined if this.pos was undefined. It would be better to pass undefined to fs.write rather than NaN in this case.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ofrobots

ofrobots commented Sep 13, 2018

Copy link
Copy Markdown
Contributor

CI: https://ci.nodejs.org/job/node-test-pull-request/17174/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ofrobots

ofrobots commented Sep 13, 2018

Copy link
Copy Markdown
Contributor

/cc @nodejs/fs

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ronkorving

ronkorving commented Sep 14, 2018

Copy link
Copy Markdown
Contributor

Thanks for addressing this issue! Makes you wonder... what happens with _writev?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

addaleax
addaleax previously approved these changes Sep 14, 2018
View reviewed changes
Comment thread test/parallel/test-fs-write-stream-partial-write.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

@addaleax addaleax added the needs-ci PRs that need a full CI run. label Sep 14, 2018
@addaleax
addaleax dismissed their stale review September 16, 2018 08:16

too soon

@addaleax

addaleax commented Sep 16, 2018

Copy link
Copy Markdown
Member

@kyliau Can you look into @ronkorving’s comment? I think _writev might be having the same issue. Maybe it would be better to address this in libuv? Also, the example you mentioned in the PR description is a full disk – it doesn’t sound to me like trying to continue to write would make sense in that scenario?

CI: https://ci.nodejs.org/job/node-test-pull-request/17214/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@addaleax addaleax removed the needs-ci PRs that need a full CI run. label Sep 16, 2018
@joyeecheung joyeecheung mentioned this pull request Sep 18, 2018
CI failures: 20180918 nodejs/reliability#16
Closed
@addaleax

addaleax commented Sep 20, 2018

Copy link
Copy Markdown
Member

Also, how does libuv/libuv#1742 fit into this? I haven’t dug much but from the sound of it it seems like it would fix the same issue on the libuv level?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kyliau

kyliau commented Sep 20, 2018

Copy link
Copy Markdown
Author

@ronkorving I briefly looked at _writev. It also does not check the return value of bytes written. However in this case it doesn't seem like it's a good choice to reimplement the logic to determine the remaining buffers in JS since similar logic already exists in https://github.com/nodejs/node/blob/master/src/stream_wrap.cc#L290.

@addaleax It's true that if the underlying medium is a HDD and it's full then we shouldn't continue writing. The case where I encountered this bug is in a Unix pipe, where partial write occurred when the payload is much larger than the pipe buffer, but it's ok to resume writing once the pipe is drained.

In either case, the user has no way of knowing that a partial write occurred. Would it be better to pass along the written bytes to the callback so user is aware and can take appropriate steps?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Keen Yee Liau
fs: WriteStream should handle partial writes
6456af8
Given a buffer of length l, fs.write() will not necessarily write the
entire buffer to the file. This can occur if, for example, there is
insufficient space on the underlying physical medium.

WriteStream did not handle this case, and when partial write occurs,
it will errorneously report that the write is successful.

This commit changes the _write() behavior to continue the write
operation, picking up from where the last operation left off.

More information about the write() system call is available at
http://man7.org/linux/man-pages/man2/write.2.html
@kyliau
kyliau force-pushed the writestream branch from ed666c7 to 6456af8 Compare September 20, 2018 23:26
@kyliau

kyliau commented Sep 20, 2018

Copy link
Copy Markdown
Author

@gireeshpunathil reported similar issue with an example here

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@santigimeno

santigimeno commented Sep 28, 2018

Copy link
Copy Markdown
Member

FYI: libuv/libuv#1742 has just landed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kyliau kyliau closed this Oct 22, 2018
@kyliau
kyliau deleted the writestream branch October 22, 2018 22:33
@coderabbitai coderabbitai Bot mentioned this pull request Aug 20, 2026
v0.2: crash-safe execution journal, beginExecute/confirm lifecycle, reconcile callback jpka/safe-write-mcp-core#17
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@addaleax addaleax addaleax left review comments
+2 more reviewers
@ofrobots ofrobots ofrobots left review comments
@TimothyGu TimothyGu TimothyGu left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

fs Issues and PRs related to file-system APIs and the fs module.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@kyliau @ofrobots @ronkorving @addaleax @santigimeno @TimothyGu @nodejs-github-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.