-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
first version #39118
first version #39118
Conversation
createAsyncIterator should throw ERR_STREAM_PREMATURE_CLOSE if 'close' is emitted before 'end'
@@ -1123,6 +1124,10 @@ async function* createAsyncIterator(stream, options) { | |||
next.call(this); | |||
}) | |||
.on('close', function() { | |||
if (endEmitted) { | |||
throw new ERR_STREAM_PREMATURE_CLOSE(); |
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.
Throwing from an async event handler is not a good idea because it can't be caught (aside from the process-wide 'uncaughtException'
event handler).
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.
Ah, I almost forgot about that behavior in js.
Sorry, I am new to the project, so I am unsure about my solution, but what do you think about the following idea?
on('close', function() {
if (endEmitted) {
stream.emit('error', new ERR_STREAM_PREMATURE_CLOSE());
} else {
closeEmitted = true;
next.call(this);
}
});
I don't quite understand how, but my guess is that next.call(this)
awakens the while loop that awaits a Promise:
else {
await new Promise(next);
}
So my idea is, to emit error
event with an instance of ERR_STREAM_PREMATURE_CLOSE
so that it could, in turn, throw the error properly inside the try/catch block:
else if (errorEmitted) {
throw error;
}
Why I am unsure:
- If I emit
error
event, when will it be processed? Will it be the next loop of the event loop? - If so, can other events getting processed before the
error
event result in an unwanted behavior?
I believe that I also need an input from @ronag on this
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.
You don't want to do anything in the event handler here. You should probably look at the finally block.
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 think you should start with writing a failing tests and try to make that pass.
…e-close-before-end
It looks like the issue has been closed by another PR. |
createAsyncIterator should throw ERR_STREAM_PREMATURE_CLOSE if 'close' is emitted before 'end'
Closes #39086