-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
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:
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?