Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[squash] use try finally, for better stack traces
  • Loading branch information
AndreasMadsen committed Jul 3, 2017
commit 278b1636c91ef48424cad46fe4c43201c5f780cc
29 changes: 18 additions & 11 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ async_wrap.setupHooks({ init,
// Used to fatally abort the process if a callback throws.
// This is JavaScript version of the ClearFatalExceptionHandlers function
// in C++.
function fatalError(e) {
Copy link
Copy Markdown
Contributor

@refack refack Jul 3, 2017

Choose a reason for hiding this comment

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

Why is everyone trying to kill this 😨
I like it as a way to bail from a callback or a Promise.catch
Ref: #13839 (comment)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Just because it is useful for Promise.catch doesn't mean that is useful here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Probably. I'll copy it to test/common/

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Letting an error bubble with try {} finally {} have always been the standard way of handling throws.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Except what async_hooks is doing isn't standard. This is a guard to prevent an abort from C++ if it detects stack corruption that can likely result from the user try catching/Promise wrapping the call, and to get useful core dumps. It also doesn't follow how this is handled in C++ (https://github.com/nodejs/node/blob/v8.1.3/src/env-inl.h#L144-L160). I'm a big -1 on this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

By "exporting FatalException" do you mean expose it to JS?

Copy link
Copy Markdown
Contributor

@trevnorris trevnorris Jul 3, 2017

Choose a reason for hiding this comment

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

If anything I'd like to export the code in Environment::AsyncHooks::pop_ids() and use that as the uniform way to exit the process.

EDIT: Except it should show a JS stack instead of a C++ stack.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

By "exporting FatalException" do you mean expose it to JS?

Yes. At the very least I would like a unified way of making a fatal exception. Currently, there are a couple of different implementations floating around in nodecore. It makes things like --abort-on-uncaught-exception difficult to test.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@AndreasMadsen

At the very least I would like a unified way of making a fatal exception.

Sounds good to me.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So area this is currently a WIP?

function clearFatalExceptionHandlers(e) {
process.domain = undefined;
process._events.uncaughtException = undefined;
throw e;
}


Expand Down Expand Up @@ -338,15 +337,17 @@ function emitInitS(asyncId, type, triggerAsyncId, resource) {

function emitBeforeN(asyncId) {
processing_hook = true;
var didThrow = true;
// Use a single try/catch for all hook to avoid setting up one per iteration.
try {
for (var i = 0; i < active_hooks_array.length; i++) {
if (typeof active_hooks_array[i][before_symbol] === 'function') {
active_hooks_array[i][before_symbol](asyncId);
}
}
} catch (e) {
fatalError(e);
didThrow = false;
} finally {
if (didThrow) clearFatalExceptionHandlers();
}
processing_hook = false;

Expand All @@ -364,10 +365,11 @@ function emitBeforeS(asyncId, triggerAsyncId = asyncId) {

// Validate the ids.
if (asyncId < 0 || triggerAsyncId < 0) {
fatalError(new Error(
clearFatalExceptionHandlers();
throw new Error(
'before(): asyncId or triggerAsyncId is less than zero ' +
`(asyncId: ${asyncId}, triggerAsyncId: ${triggerAsyncId})`
));
);
}

pushAsyncIds(asyncId, triggerAsyncId);
Expand All @@ -382,15 +384,17 @@ function emitBeforeS(asyncId, triggerAsyncId = asyncId) {
// this is called.
function emitAfterN(asyncId) {
processing_hook = true;
var didThrow = true;
// Use a single try/catch for all hook to avoid setting up one per iteration.
try {
for (var i = 0; i < active_hooks_array.length; i++) {
if (typeof active_hooks_array[i][after_symbol] === 'function') {
active_hooks_array[i][after_symbol](asyncId);
}
}
didThrow = false;
} catch (e) {
fatalError(e);
if (didThrow) clearFatalExceptionHandlers();
}
processing_hook = false;

Expand Down Expand Up @@ -422,15 +426,17 @@ function emitDestroyS(asyncId) {

function emitDestroyN(asyncId) {
processing_hook = true;
var didThrow = true;
// Use a single try/catch for all hook to avoid setting up one per iteration.
try {
for (var i = 0; i < active_hooks_array.length; i++) {
if (typeof active_hooks_array[i][destroy_symbol] === 'function') {
active_hooks_array[i][destroy_symbol](asyncId);
}
}
} catch (e) {
fatalError(e);
didThrow = false;
} finally {
if (didThrow) clearFatalExceptionHandlers();
}
processing_hook = false;

Expand All @@ -455,6 +461,7 @@ function emitDestroyN(asyncId) {
// exceptions.
function init(asyncId, type, triggerAsyncId, resource) {
processing_hook = true;
var didThrow = true;
// Use a single try/catch for all hook to avoid setting up one per iteration.
try {
for (var i = 0; i < active_hooks_array.length; i++) {
Expand All @@ -465,8 +472,8 @@ function init(asyncId, type, triggerAsyncId, resource) {
);
}
}
} catch (e) {
fatalError(e);
} finally {
if (didThrow) clearFatalExceptionHandlers();
}
processing_hook = false;
}
Expand Down