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

domain: fix error emit handling #17588

Closed
wants to merge 4 commits into from

Conversation

apapirovski
Copy link
Member

There are a few errors in the logic of the new domain-specific emit and a bit too much fanciness with the falling through try-catch logic. Fixed it all up and created a regression test.

Refs: #17403

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

domain, events, test

@apapirovski apapirovski added domain Issues and PRs related to the domain subsystem. events Issues and PRs related to the events subsystem / EventEmitter. test Issues and PRs related to the tests. labels Dec 10, 2017
@nodejs-github-bot nodejs-github-bot added the domain Issues and PRs related to the domain subsystem. label Dec 10, 2017
@apapirovski
Copy link
Member Author

apapirovski commented Dec 10, 2017

@AndreasMadsen
Copy link
Member

@apapirovski Thanks. Could you elaborate on what the bugs are?

@apapirovski
Copy link
Member Author

apapirovski commented Dec 10, 2017

@AndreasMadsen see the added test case which fails with the old implementation. Basically, error isn't emitted on the original EventEmitter under any circumstances.

@AndreasMadsen
Copy link
Member

@apapirovski Could you describe that in the commit message?

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.
@apapirovski
Copy link
Member Author

@AndreasMadsen Done.

Copy link
Member

@AndreasMadsen AndreasMadsen left a comment

Choose a reason for hiding this comment

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

This is a good start. But let's try to stay closer to the original implementation.

lib/domain.js Outdated
domain.emit('error', args[1]);
return false;
}
const doError = type === 'error' &&
Copy link
Member

Choose a reason for hiding this comment

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

This doError is opposite of the doError in the old events.js implementation. Logically it is not wrong but it makes it hard to compare the implementations.

Copy link
Member Author

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 shouldEmitError instead of doError?

Copy link
Member

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 EventEmitter or the domain instance.

lib/domain.js Outdated
}

if (type === 'error') {
const er = args.length > 1 && args[1] ?
Copy link
Member

Choose a reason for hiding this comment

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

args[1] || new errors.Error('ERR_UNHANDLED_ERROR') should be enough. But I think it would be better to stay grammatically closer to the old implementation.

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;
}

Copy link
Member Author

@apapirovski apapirovski Dec 11, 2017

Choose a reason for hiding this comment

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

That triggers a slow path in V8. We need the if check or a ternary. This version does the same in less lines and we don't need lazyErrors, which is the main reason we didn't just use a ternary before.

Copy link
Member

@BridgeAR BridgeAR left a comment

Choose a reason for hiding this comment

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

LGTM but it would be nice if my comment would be addressed (even though that code was not touched in this PR).

lib/domain.js Outdated
if (type === 'error') {
const er = args.length > 1 && args[1] ?
args[1] : new errors.Error('ERR_UNHANDLED_ERROR');

if (typeof er === 'object' && er !== null) {
Copy link
Member

Choose a reason for hiding this comment

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

er can not be null anymore. It is either truthy and in that case it is not null or it is an error and in that case it is also not null. I wonder if the object check is actually necessary either. Since er is always a truthy value, assigning properties to it will never fail (even though it would of course be weird to try to add properties to e.g. a number, but trying would not hurt either and the average case would be faster).

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

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

That could be solved with a comment as well, right?

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

@apapirovski apapirovski Dec 12, 2017

Choose a reason for hiding this comment

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

Although, something to think about: typeof isn't very expensive in V8 these days and setting 3 props on numbers or strings is, I think, a lot more expensive. So from a perf perspective, it might be better to keep this check.

Copy link
Member

@BridgeAR BridgeAR Dec 12, 2017

Choose a reason for hiding this comment

The 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...

Copy link
Member Author

@apapirovski apapirovski Dec 12, 2017

Choose a reason for hiding this comment

The 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 domain related code is that performance sensitive anyway... there are much worse offenders.

I'll remove the null check tho.

@apapirovski
Copy link
Member Author

Landed in 04ae486

@apapirovski apapirovski deleted the fix-domain-emit-error branch December 13, 2017 14:38
apapirovski added a commit that referenced this pull request Dec 13, 2017
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.

PR-URL: #17588
Refs: #17403
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
@MylesBorins
Copy link
Contributor

I'm setting the dont-land label for 9.x as #17403 didn't land.

Please feel free to backport

apapirovski added a commit to apapirovski/node that referenced this pull request Jan 31, 2018
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.

PR-URL: nodejs#17588
Refs: nodejs#17403
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this pull request Feb 20, 2018
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.

Backport-PR-URL: #18487
PR-URL: #17588
Refs: #17403
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
@MylesBorins MylesBorins mentioned this pull request Feb 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain Issues and PRs related to the domain subsystem. events Issues and PRs related to the events subsystem / EventEmitter. test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants