-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Closed
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5945668
fs: do not emit open and read if stream destroyed
ee2f868
Add test
0255340
fs: close stream if destroyed before opened
650079e
fs: address comments
fc7241f
Merge branch 'master' into noop-after-destroy
dexterleng a26991b
fs: emit error after close
dexterleng a066fbb
Revert "fs: emit error after close"
dexterleng e657b86
fs: update test
dexterleng 930c8d4
fs: Swallow error
dexterleng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,6 +132,16 @@ ReadStream.prototype.open = function() { | |
} | ||
|
||
this.fd = fd; | ||
|
||
if (this.destroyed) { | ||
closeFsStream(this, (er) => { | ||
if (er) { | ||
this.emit('error', er); | ||
} | ||
}); | ||
return; | ||
dexterleng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
this.emit('open', fd); | ||
this.emit('ready'); | ||
// start the flow of data. | ||
|
@@ -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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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'); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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... 🤔