Skip to content

Commit

Permalink
fs: don't fail with EBADF on double close
Browse files Browse the repository at this point in the history
Calling fs.ReadStream#close() or fs.WriteStream#close() twice made it
try to close the file descriptor twice, with the second attempt using
the nulled out `.fd` property and failing with an EBADF error.

Fixes: nodejs#2950
  • Loading branch information
bnoordhuis committed Sep 26, 2015
1 parent 212c9c0 commit b5391bd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,8 @@ ReadStream.prototype.close = function(cb) {
close();

function close(fd) {
if (fd === undefined && self.fd === null)
return;
fs.close(fd || self.fd, function(er) {
if (er)
self.emit('error', er);
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-fs-read-stream-double-close.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const common = require('../common');
const fs = require('fs');

common.refreshTmpDir();
fs.writeFileSync(common.tmpDir + '/ro', '');

const s = fs.createReadStream(common.tmpDir + '/ro');
s.close(common.mustCall(function() {}));
s.close(common.mustCall(function() {}));
10 changes: 10 additions & 0 deletions test/parallel/test-fs-write-stream-double-close.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const common = require('../common');
const fs = require('fs');

common.refreshTmpDir();

const s = fs.createWriteStream(common.tmpDir + '/rw');
s.close(common.mustCall(function() {}));
s.close(common.mustCall(function() {}));

0 comments on commit b5391bd

Please sign in to comment.