-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Stream emitting same error twice under certain circomstances #36053
Comments
This is not a regression from my point of view, it actually fixed something that was broken before. The problem is that I would also note that this behave exactly as planned: I do not think there is anything to do here :/. |
@mcollina Ya agreed. Watson and I discussed this issue offline and came to the same conclusion. |
Unfortunately not. The problem is that |
@mcollina isn't it only emitted by destroy? Ie we can't just check if .destroyed = true in an overwrite of emit and throw a warning if not and the event is error? |
Yes
We could. The problem is that adding an override for |
@mcollina We could just have an internal _emit that is just an alias to normal emit to avoid any slowdown |
Unfortunately that's not possible as some descendants override We should probably benchmark this and ship it anyway. I'm happy to review a PR. |
No statistically significant performance drop for --- a/lib/internal/streams/legacy.js
+++ b/lib/internal/streams/legacy.js
@@ -93,6 +93,13 @@ Stream.prototype.pipe = function(dest, options) {
return dest;
};
+Stream.prototype.emit = function(event) {
+ if (event === 'error' && !this.destroyed)
+ process.emitWarning('Avoid emitting directly an \'error\' to Stream' +
+ ', prefer Stream.destroy()');
+ return EE.prototype.emit.apply(this, arguments);
+};
+
function prependListener(emitter, event, fn) {
// Sadly this is not cacheable as some libraries bundle their own
// event emitter implementation with them. Everything seems to work, but still an avalanche of warnings during Do you really think it makes sense to add it? |
What steps will reproduce the bug?
Run this code on Node.js 12.18.1 or newer and notice how the error is emitted on
b
twice. On any older Node.js version it's only emitted once.If I change
b.emit('error', err)
tob.destroy(err)
the error goes away. Usingdestroy
is best practise and the user isn't supposed to emiterror
events manually on streams like in the above code. However, this was how we told users to do it previously, so a lot of code most likely exists in the wild expecting this to be possible and emitting the same error twice can have bad consequences.Thoughts:
error
event on a stream.error
manually.This regression was most likely introduced in #30869.
The text was updated successfully, but these errors were encountered: