Skip to content

Closing fs streams early could call close during or before I/O #2006

Closed
@briangreenery

Description

@briangreenery

Suppose you fs.createWriteStream, pipe something into it, and then need to close the stream early because of an error somewhere else.

var writeStream = fs.createWriteStream(someFile);
source.pipe(writeStream);

// ...time passes...

source.unpipe(writeStream);
writeStream.close();

Calling close on the write stream in this case could cause close to 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 the close to happen before the write begins.

Specifically, WriteStream.close does not check whether a fs.write operation is pending before calling fs.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.end and should never use WriteStream.close?

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.fsIssues and PRs related to the fs subsystem / file system.help wantedIssues that need assistance from volunteers or PRs that need help to proceed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions