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

fs: do not emit open and read if stream destroyed #24055

Closed
wants to merge 9 commits 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
16 changes: 15 additions & 1 deletion lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ ReadStream.prototype.open = function() {
}

this.fd = fd;

if (this.destroyed) {
// swallow the error to prevent error from emitting after close.
closeFsStream(this, () => {});
return;
dexterleng marked this conversation as resolved.
Show resolved Hide resolved
}

this.emit('open', fd);
this.emit('ready');
// start the flow of data.
Expand Down Expand Up @@ -204,8 +211,9 @@ ReadStream.prototype._read = function(n) {
};

ReadStream.prototype._destroy = function(err, cb) {
// if stream is not open it will be closed in open()
if (typeof this.fd !== 'number') {
this.once('open', closeFsStream.bind(null, this, cb, err));
cb(err);
return;
}

Expand Down Expand Up @@ -294,6 +302,12 @@ WriteStream.prototype.open = function() {
}

this.fd = fd;

if (this.destroyed) {
closeFsStream(this, () => {});
return;
}

this.emit('open', fd);
this.emit('ready');
});
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-fs-stream-no-events-after-destroy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
const common = require('../common');
const fs = require('fs');
const assert = require('assert');

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

const test = (stream) => {
const err = new Error('DESTROYED');
stream.destroy(err);
stream.on('open', common.mustNotCall());
stream.on('ready', common.mustNotCall());
stream.on('close', common.mustCall());
mcollina marked this conversation as resolved.
Show resolved Hide resolved
stream.on('error', common.mustNotCall());
}

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