fs: fix immediate WriteStream#end()
Fix an exception that was raised when the WriteStream was closed immediately after creating it: TypeError: Cannot read property 'fd' of undefined at WriteStream.close (fs.js:1537:18) <snip> Avoid the TypeError and make sure the file descriptor is closed. Fixes #4745.
This commit is contained in:
parent
ed3d553d82
commit
c7b84a1d01
10
lib/fs.js
10
lib/fs.js
@ -1522,19 +1522,21 @@ ReadStream.prototype.destroy = function() {
|
|||||||
|
|
||||||
|
|
||||||
ReadStream.prototype.close = function(cb) {
|
ReadStream.prototype.close = function(cb) {
|
||||||
|
var self = this;
|
||||||
if (cb)
|
if (cb)
|
||||||
this.once('close', cb);
|
this.once('close', cb);
|
||||||
if (this.closed || 'number' !== typeof this.fd) {
|
if (this.closed || 'number' !== typeof this.fd) {
|
||||||
if ('number' !== typeof this.fd)
|
if ('number' !== typeof this.fd) {
|
||||||
this.once('open', close);
|
this.once('open', close);
|
||||||
|
return;
|
||||||
|
}
|
||||||
return process.nextTick(this.emit.bind(this, 'close'));
|
return process.nextTick(this.emit.bind(this, 'close'));
|
||||||
}
|
}
|
||||||
this.closed = true;
|
this.closed = true;
|
||||||
var self = this;
|
|
||||||
close();
|
close();
|
||||||
|
|
||||||
function close() {
|
function close(fd) {
|
||||||
fs.close(self.fd, function(er) {
|
fs.close(fd || self.fd, function(er) {
|
||||||
if (er)
|
if (er)
|
||||||
self.emit('error', er);
|
self.emit('error', er);
|
||||||
else
|
else
|
||||||
|
@ -21,27 +21,22 @@
|
|||||||
|
|
||||||
var common = require('../common');
|
var common = require('../common');
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
|
var path = require('path');
|
||||||
|
var fs = require('fs');
|
||||||
|
|
||||||
var path = require('path'),
|
|
||||||
fs = require('fs');
|
|
||||||
|
|
||||||
var writeEndOk = false;
|
|
||||||
(function() {
|
(function() {
|
||||||
debugger;
|
var file = path.join(common.tmpDir, 'write-end-test0.txt');
|
||||||
var file = path.join(common.tmpDir, 'write-end-test.txt');
|
|
||||||
var stream = fs.createWriteStream(file);
|
var stream = fs.createWriteStream(file);
|
||||||
|
stream.end();
|
||||||
stream.end('a\n', 'utf8');
|
stream.on('close', common.mustCall(function() { }));
|
||||||
stream.on('close', function() {
|
|
||||||
var content = fs.readFileSync(file, 'utf8');
|
|
||||||
assert.equal(content, 'a\n');
|
|
||||||
writeEndOk = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
(function() {
|
||||||
process.on('exit', function() {
|
var file = path.join(common.tmpDir, 'write-end-test1.txt');
|
||||||
assert.ok(writeEndOk);
|
var stream = fs.createWriteStream(file);
|
||||||
});
|
stream.end('a\n', 'utf8');
|
||||||
|
stream.on('close', common.mustCall(function() {
|
||||||
|
var content = fs.readFileSync(file, 'utf8');
|
||||||
|
assert.equal(content, 'a\n');
|
||||||
|
}));
|
||||||
|
})();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user