-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
domain: fix error emit handling #17588
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Fix an issue where error is never emitted on the original EventEmitter in situations where a listener for error does exist. Refactor to eliminate unnecessary try/catch/finally block.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -399,32 +399,33 @@ EventEmitter.init = function() { | |
| const eventEmit = EventEmitter.prototype.emit; | ||
| EventEmitter.prototype.emit = function emit(...args) { | ||
| const domain = this.domain; | ||
| if (domain === null || domain === undefined || this === process) { | ||
| return Reflect.apply(eventEmit, this, args); | ||
| } | ||
|
|
||
| const type = args[0]; | ||
| // edge case: if there is a domain and an existing non error object is given, | ||
| // it should not be errorized | ||
| // see test/parallel/test-event-emitter-no-error-provided-to-error-event.js | ||
| if (type === 'error' && args.length > 1 && args[1] && | ||
| !(args[1] instanceof Error)) { | ||
| domain.emit('error', args[1]); | ||
| return false; | ||
| } | ||
| const doError = type === 'error' && | ||
| this._events !== undefined && | ||
| this._events.error !== undefined; | ||
|
|
||
| domain.enter(); | ||
| try { | ||
| if (doError || domain === null || domain === undefined || this === process) { | ||
| return Reflect.apply(eventEmit, this, args); | ||
| } catch (er) { | ||
| } | ||
|
|
||
| if (type === 'error') { | ||
| const er = args.length > 1 && args[1] ? | ||
|
Member
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.
Something like: er = args[1]
if (!er) {
const errors = lazyErrors();
er = new errors.Error('ERR_UNHANDLED_ERROR');
}
if (typeof er === 'object' && er !== null) {
er.domainEmitter = this;
er.domain = domain;
er.domainThrown = false;
}
Contributor
Author
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. That triggers a slow path in V8. We need the |
||
| args[1] : new errors.Error('ERR_UNHANDLED_ERROR'); | ||
|
|
||
| if (typeof er === 'object' && er !== null) { | ||
|
Member
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.
Contributor
Author
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. @BridgeAR Great job noticing that! Seems like we've had that pointless check in this code for a while. Guessing it was useful at some point but clearly left over and unnecessary now. The only reason I can think of leaving the object check is in case someone is testing for those properties and assuming that they're dealing with an object afterwards... ? But that seems obscure enough that it shouldn't be a real issue.
Member
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. That could be solved with a comment as well, right?
Contributor
Author
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. I just mean for the purposes of third-party code... but that seems like a pretty obscure thing to me. We can run CitGM to double-check.
Contributor
Author
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. Although, something to think about:
Member
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. I am not sure what you mean that third-party code relies on that? Trying to set a property on a primitive (not function) is a noop. The outcome will be the same as it is right now. About the performance implications - I am aware that a typeof is cheap but do we want to optimize for the average case or for obscure cases? Because passing in a non object as error sounds really weird to me...
Contributor
Author
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. Does it? I've definitely seen people use strings. It's tricky... we've allowed it till now so we're kind of stuck with supporting it. Anyway, if we're optimizing then it should be done in another PR. I don't think I'll remove the |
||
| er.domainEmitter = this; | ||
| er.domain = domain; | ||
| er.domainThrown = false; | ||
| } | ||
|
|
||
| domain.emit('error', er); | ||
| return false; | ||
| } finally { | ||
| domain.exit(); | ||
| } | ||
|
|
||
| domain.enter(); | ||
| const ret = Reflect.apply(eventEmit, this, args); | ||
| domain.exit(); | ||
|
|
||
| return ret; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const domain = require('domain').create(); | ||
| const EventEmitter = require('events'); | ||
|
|
||
| domain.on('error', common.mustNotCall()); | ||
|
|
||
| const ee = new EventEmitter(); | ||
|
|
||
| const plainObject = { justAn: 'object' }; | ||
| ee.once('error', common.mustCall((err) => { | ||
| assert.deepStrictEqual(err, plainObject); | ||
| })); | ||
| ee.emit('error', plainObject); | ||
|
|
||
| const err = new Error('test error'); | ||
| ee.once('error', common.expectsError(err)); | ||
| ee.emit('error', err); |
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
doErroris opposite of thedoErrorin the oldevents.jsimplementation. Logically it is not wrong but it makes it hard to compare the implementations.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.
That seems more like a naming issue. Maybe just name it
shouldEmitErrorinstead ofdoError?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'm fine with a renaming. But you should make it clear if the emit is on the original
EventEmitteror thedomaininstance.