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

async_hooks: fatalError(e) function checking error Object #38077

Closed
PhakornKiong opened this issue Apr 4, 2021 · 8 comments
Closed

async_hooks: fatalError(e) function checking error Object #38077

PhakornKiong opened this issue Apr 4, 2021 · 8 comments
Labels
async_hooks Issues and PRs related to the async hooks subsystem.

Comments

@PhakornKiong
Copy link
Contributor

While going through the test coverage, I noticed that the else branch is actually not covered in the test case here

function fatalError(e) {
if (typeof e.stack === 'string') {
process._rawDebug(e.stack);
} else {
const o = { message: e };
ErrorCaptureStackTrace(o, fatalError);
process._rawDebug(o.stack);
}
const { getOptionValue } = require('internal/options');
if (getOptionValue('--abort-on-uncaught-exception')) {
process.abort();
}
process.exit(1);
}

Will the else case ever happened? According to the Node.js docs here, error.stack is always a string. So why would we ever do a check on that portion and have an else branch?

From what I understand, fatalError(e) is only used to capture error when the callback from the registered hooks failed and it doesn't seem necessary in this case.

Let me know what you think.

PhakornKiong added a commit to PhakornKiong/node that referenced this issue Apr 4, 2021
The if-else is check if e.stack is string
according to Node.js docs, e.stack is always
a string, so there is no need for this check

Refs: nodejs#38077
@Ayase-252
Copy link
Member

Ayase-252 commented Apr 4, 2021

FYI: this branch was brought with initial implementation #12892

I don't know the exact reason. May it need view from author?

cc @AndreasMadsen

@Linkgoron
Copy link
Member

Linkgoron commented Apr 4, 2021

I think that this was the reasoning : #11883 (comment), to filter "real" Errors

@Ayase-252 Ayase-252 added the async_hooks Issues and PRs related to the async hooks subsystem. label Apr 4, 2021
@AndreasMadsen
Copy link
Member

cc @nodejs/async_hooks as I'm unlikely to look at this.

@Flarna
Copy link
Member

Flarna commented Apr 4, 2021

According to the Node.js docs here, error.stack is always a string

That's true for Error objects but in Javascript you can throw also non errors, e.g. throw 23 or even throw undefined.

@PhakornKiong
Copy link
Contributor Author

True enough on throwing non-error object. However my question is that whether this if-else is necessary? Based on #11883, it seems to be trying to capture error from v8?

I'm not entirely sure of the intention here.

@AndreasMadsen
Copy link
Member

@Flarna @Darkripper214 A fatal error here is just any error thrown in an async_hooks callback. So yes, throw 23 is a possibility.

@Flarna
Copy link
Member

Flarna commented Apr 6, 2021

@Darkripper214 The if path handles anything thrown which has a stack property of type string - which should cover all thrown Error objects. One may think that using instanceof Error would be better but there could be handwritten error types slipping through then.

The else path takes care about all the rest (e.g. throw 23) which has no stack. Then ErrorCaptureStackTrace is called to get a stacktrace pointing to the location where this was catched. Clearly not as good as getting the stack from the throw location but better then nothing - at least users know that it happened within an async hook.

If you remove the else branch and for whatever reason someone throws a non Error in one of the async hooks the node process would just exit without any indication why. Currently you get a stacktrace pointing to async hooks.

But I think we should change if (typeof e.stack === 'string') to if (typeof e?.stack === 'string') otherwise throw null results in an exception in fatalError.

@PhakornKiong
Copy link
Contributor Author

@Flarna Thanks for the clarification. So I was misunderstanding the functionality of ErrorCaptureStackTrace here.

I think this had turned into an issue for the throw null case. I will open a new issue with new PR to fix this and update the test to cover the else case properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
async_hooks Issues and PRs related to the async hooks subsystem.
Projects
None yet
Development

No branches or pull requests

5 participants