Skip to content

Commit 281772e

Browse files
committed
fs: don't emit open after destroy
1 parent 2fd2dd7 commit 281772e

3 files changed

Lines changed: 35 additions & 16 deletions

File tree

lib/internal/fs/streams.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const { toPathIfFileURL } = require('internal/url');
2222

2323
const kMinPoolSpace = 128;
2424

25+
const kOpenCallback = Symbol('destroyCallback');
26+
2527
let pool;
2628
// It can happen that we expect to read a large chunk of data, and reserve
2729
// a large chunk of the pool accordingly, but the read() call only filled
@@ -125,10 +127,14 @@ ReadStream.prototype.open = function() {
125127
}
126128

127129
this.fd = fd;
128-
this.emit('open', fd);
129-
this.emit('ready');
130-
// Start the flow of data.
131-
this.read();
130+
if (!this[kOpenCallback]) {
131+
this.emit('open', fd);
132+
this.emit('ready');
133+
// Start the flow of data.
134+
this.read();
135+
} else {
136+
this[kOpenCallback]();
137+
}
132138
});
133139
};
134140

@@ -207,7 +213,7 @@ ReadStream.prototype._read = function(n) {
207213

208214
ReadStream.prototype._destroy = function(err, cb) {
209215
if (typeof this.fd !== 'number') {
210-
this.once('open', closeFsStream.bind(null, this, cb, err));
216+
this[kOpenCallback] = closeFsStream.bind(null, this, cb, err);
211217
return;
212218
}
213219

@@ -291,8 +297,12 @@ WriteStream.prototype.open = function() {
291297
}
292298

293299
this.fd = fd;
294-
this.emit('open', fd);
295-
this.emit('ready');
300+
if (!this[kOpenCallback]) {
301+
this.emit('open', fd);
302+
this.emit('ready');
303+
} else {
304+
this[kOpenCallback]();
305+
}
296306
});
297307
};
298308

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
const common = require('../common');
3+
const fs = require('fs');
4+
5+
const tmpdir = require('../common/tmpdir');
6+
tmpdir.refresh();
7+
8+
test(fs.createReadStream(__filename));
9+
test(fs.createWriteStream(`${tmpdir.path}/dummy`));
10+
11+
function test(stream) {
12+
stream.on('open', common.mustNotCall());
13+
stream.on('ready', common.mustNotCall());
14+
stream.on('close', common.mustCall());
15+
stream.destroy();
16+
stream.on('ready', common.mustNotCall());
17+
stream.on('open', common.mustNotCall());
18+
}

test/parallel/test-fs-stream-double-close.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,16 @@ const tmpdir = require('../common/tmpdir');
2727
tmpdir.refresh();
2828

2929
test1(fs.createReadStream(__filename));
30-
test2(fs.createReadStream(__filename));
3130
test3(fs.createReadStream(__filename));
3231

3332
test1(fs.createWriteStream(`${tmpdir.path}/dummy1`));
34-
test2(fs.createWriteStream(`${tmpdir.path}/dummy2`));
3533
test3(fs.createWriteStream(`${tmpdir.path}/dummy3`));
3634

3735
function test1(stream) {
3836
stream.destroy();
3937
stream.destroy();
4038
}
4139

42-
function test2(stream) {
43-
stream.destroy();
44-
stream.on('open', common.mustCall(function(fd) {
45-
stream.destroy();
46-
}));
47-
}
48-
4940
function test3(stream) {
5041
stream.on('open', common.mustCall(function(fd) {
5142
stream.destroy();

0 commit comments

Comments
 (0)