Description
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
?