Skip to content

Commit b5391bd

Browse files
committed
fs: don't fail with EBADF on double close
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: #2950
1 parent 212c9c0 commit b5391bd

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

lib/fs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,8 @@ ReadStream.prototype.close = function(cb) {
17701770
close();
17711771

17721772
function close(fd) {
1773+
if (fd === undefined && self.fd === null)
1774+
return;
17731775
fs.close(fd || self.fd, function(er) {
17741776
if (er)
17751777
self.emit('error', er);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fs = require('fs');
5+
6+
common.refreshTmpDir();
7+
fs.writeFileSync(common.tmpDir + '/ro', '');
8+
9+
const s = fs.createReadStream(common.tmpDir + '/ro');
10+
s.close(common.mustCall(function() {}));
11+
s.close(common.mustCall(function() {}));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fs = require('fs');
5+
6+
common.refreshTmpDir();
7+
8+
const s = fs.createWriteStream(common.tmpDir + '/rw');
9+
s.close(common.mustCall(function() {}));
10+
s.close(common.mustCall(function() {}));

0 commit comments

Comments
 (0)