Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[x] fs: don't emit open after destroy #28765

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const { toPathIfFileURL } = require('internal/url');

const kMinPoolSpace = 128;

const kOpenCallback = Symbol('destroyCallback');

let pool;
// It can happen that we expect to read a large chunk of data, and reserve
// a large chunk of the pool accordingly, but the read() call only filled
Expand Down Expand Up @@ -125,10 +127,14 @@ ReadStream.prototype.open = function() {
}

this.fd = fd;
this.emit('open', fd);
this.emit('ready');
// Start the flow of data.
this.read();
if (!this[kOpenCallback]) {
this.emit('open', fd);
this.emit('ready');
// Start the flow of data.
this.read();
} else {
this[kOpenCallback]();
}
});
};

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

ReadStream.prototype._destroy = function(err, cb) {
if (typeof this.fd !== 'number') {
this.once('open', closeFsStream.bind(null, this, cb, err));
this[kOpenCallback] = closeFsStream.bind(null, this, cb, err);
return;
}

Expand Down Expand Up @@ -291,8 +297,12 @@ WriteStream.prototype.open = function() {
}

this.fd = fd;
this.emit('open', fd);
this.emit('ready');
if (!this[kOpenCallback]) {
this.emit('open', fd);
this.emit('ready');
} else {
this[kOpenCallback]();
}
});
};

Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-fs-stream-destroy-open.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
const common = require('../common');
const fs = require('fs');

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

test(fs.createReadStream(__filename));
test(fs.createWriteStream(`${tmpdir.path}/dummy`));

function test(stream) {
stream.on('open', common.mustNotCall());
stream.on('ready', common.mustNotCall());
stream.on('close', common.mustCall());
stream.destroy();
stream.on('ready', common.mustNotCall());
stream.on('open', common.mustNotCall());
}
9 changes: 0 additions & 9 deletions test/parallel/test-fs-stream-double-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,16 @@ const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

test1(fs.createReadStream(__filename));
test2(fs.createReadStream(__filename));
test3(fs.createReadStream(__filename));

test1(fs.createWriteStream(`${tmpdir.path}/dummy1`));
test2(fs.createWriteStream(`${tmpdir.path}/dummy2`));
test3(fs.createWriteStream(`${tmpdir.path}/dummy3`));

function test1(stream) {
stream.destroy();
stream.destroy();
}

function test2(stream) {
stream.destroy();
stream.on('open', common.mustCall(function(fd) {
stream.destroy();
}));
}

function test3(stream) {
stream.on('open', common.mustCall(function(fd) {
stream.destroy();
Expand Down