Skip to content

Commit

Permalink
Avoid closing a WriteStream before it has been opened.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobie authored and ry committed Sep 9, 2010
1 parent a6659e7 commit 0b9dab6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
27 changes: 15 additions & 12 deletions lib/fs.js
Expand Up @@ -949,20 +949,23 @@ WriteStream.prototype.destroy = function (cb) {
var self = this;
this.writeable = false;

fs.close(self.fd, function(err) {
if (err) {
if (cb) {
cb(err);
function close() {
fs.close(self.fd, function(err) {
if (err) {
if (cb) { cb(err); }
self.emit('error', err);
return;
}

self.emit('error', err);
return;
}
if (cb) { cb(null); }
self.emit('close');
});
}

if (cb) {
cb(null);
}
self.emit('close');
});
if (this.fd) {
close();
} else {
this.addListener('open', close);
}
};

19 changes: 19 additions & 0 deletions test/simple/test-fs-write-stream.js
@@ -0,0 +1,19 @@
common = require("../common");
assert = common.assert

var path = require('path'),
fs = require('fs');

var file = path.join(common.fixturesDir, "write.txt");

(function() {
var stream = fs.createWriteStream(file),
_fs_close = fs.close;

fs.close = function(fd) {
assert.ok(fd, "fs.close must not be called without an undefined fd.")
fs.close = _fs_close;
}
stream.destroy();
})();

0 comments on commit 0b9dab6

Please sign in to comment.