-
-
Notifications
You must be signed in to change notification settings - Fork 37.1k
Closing fs streams early could call close during or before I/O #2006
Copy link
Copy link
Closed
Labels
confirmed-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.help wantedIssues that need assistance from volunteers or PRs that need help to proceed.Issues that need assistance from volunteers or PRs that need help to proceed.
Description
Activity
Metadata
Metadata
Assignees
Labels
confirmed-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.help wantedIssues that need assistance from volunteers or PRs that need help to proceed.Issues that need assistance from volunteers or PRs that need help to proceed.
Suppose you
fs.createWriteStream, pipe something into it, and then need to close the stream early because of an error somewhere else.Calling
closeon the write stream in this case could causecloseto be called on the underlying file descriptor while a write operation is still pending. Or, if more than one worker thread is being used, it's possible for thecloseto happen before the write begins.Specifically,
WriteStream.closedoes not check whether afs.writeoperation is pending before callingfs.close:https://github.com/nodejs/io.js/blob/41951d45b6df789d7e9cf134f0029b0e791706c4/lib/fs.js#L1770
It seems like this makes it impossible to safely close a write stream early. I've never seen bad behavior from this in practice though, so maybe I'm misunderstanding something.
Are we instead supposed to call
Writable.endand should never useWriteStream.close?