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 3 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
26 changes: 22 additions & 4 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ ReadStream.prototype.open = function() {
}

this.fd = fd;

if (this.destroyed) {
closeFsStream(this, (er) => {
if (er) {
this.emit('error', er);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause error to be emitted after close. I think we should swallow this error.

Copy link
Contributor Author

@dexterleng dexterleng Dec 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed it and the error still got emitted. The error event does not seem to come from lib/internal/fs/streams.js... 🤔

Path: parallel/test-fs-stream-no-events-after-destroy
assert.js:128
  throw err;
  ^

AssertionError [ERR_ASSERTION]: function should not have been called at /Users/hackintosh/Documents/code/node/test/parallel/test-fs-stream-no-events-after-destroy.js:15
    at ReadStream.mustNotCall (/Users/hackintosh/Documents/code/node/test/common/index.js:404:12)
    at ReadStream.emit (events.js:189:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
    at internalTickCallback (internal/process/next_tick.js:72:19)
    at process._tickCallback (internal/process/next_tick.js:47:5)
    at Function.Module.runMain (internal/modules/cjs/loader.js:778:11)
    at startup (internal/bootstrap/node.js:300:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:826:3)

});
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,11 +214,9 @@ ReadStream.prototype._read = function(n) {
};

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are swallowing the original error here.

return;
}

closeFsStream(this, cb, err);
this.fd = null;
Expand Down Expand Up @@ -290,6 +298,16 @@ WriteStream.prototype.open = function() {
}

this.fd = fd;

if (this.destroyed) {
closeFsStream(this, (er) => {
if (er) {
this.emit('error', er);
}
});
return;
}

this.emit('open', fd);
this.emit('ready');
});
Expand Down
17 changes: 17 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,17 @@
'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) {
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
}