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: simplify fatalError #14051

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
108 changes: 54 additions & 54 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,13 @@ async_wrap.setupHooks({ init,
destroy: emitDestroyN });

// Used to fatally abort the process if a callback throws.
function fatalError(e) {
Copy link
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
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
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
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
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
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
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
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
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
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?

if (typeof e.stack === 'string') {
process._rawDebug(e.stack);
} else {
const o = { message: e };
Error.captureStackTrace(o, fatalError);
process._rawDebug(o.stack);
}
if (process.execArgv.some(
(e) => /^--abort[_-]on[_-]uncaught[_-]exception$/.test(e))) {
process.abort();
}
process.exit(1);
// This is JavaScript version of the ClearFatalExceptionHandlers function
// in C++.
function clearFatalExceptionHandlers(e) {
process.domain = undefined;
// Don't use removeAllListeners in case there is a removeListener listener
// that would revert it.
process._events.uncaughtException = undefined;
}


Expand Down Expand Up @@ -334,7 +328,14 @@ function emitInitS(asyncId, type, triggerAsyncId, resource) {
if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < 0)
throw new RangeError('triggerAsyncId must be an unsigned integer');

init(asyncId, type, triggerAsyncId, resource);
// Remove prepear for a fatal exception in case an error occurred
var didThrow = true;
try {
init(asyncId, type, triggerAsyncId, resource);
didThrow = false;
} finally {
if (didThrow) clearFatalExceptionHandlers();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure it's correct to move this line from the N variant to the S variant?

}

// Isn't null if hooks were added/removed while the hooks were running.
if (tmp_active_hooks_array !== null) {
Expand All @@ -345,15 +346,10 @@ function emitInitS(asyncId, type, triggerAsyncId, resource) {

function emitBeforeN(asyncId) {
processing_hook = 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);
}
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);
Copy link
Contributor

Choose a reason for hiding this comment

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

Allowing an exception to bubble then be handled by a Promise or similar means that it's possible that not all hook callbacks execute, but users that have those unexecuted callbacks won't know that it hasn't been called. Consider the following:

const map = new Map();
createHook({
  before(id) { map.set(id, /* some object */) },
  after(id) { map.delete(id) },
}).enable();

You've just introduced a vector for a memory leak.

Copy link
Contributor

Choose a reason for hiding this comment

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

BTW If you're touching this you could use for...of (supposed to be tiny bit faster on TF&I)
Ref: #13419 (comment)

}
} catch (e) {
fatalError(e);
}
processing_hook = false;

Expand All @@ -371,31 +367,37 @@ function emitBeforeS(asyncId, triggerAsyncId = asyncId) {

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

pushAsyncIds(asyncId, triggerAsyncId);

if (async_hook_fields[kBefore] === 0)
return;
emitBeforeN(asyncId);

// Remove prepear for a fatal exception in case an error occurred
Copy link
Member

Choose a reason for hiding this comment

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

s/prepear/prepare

var didThrow = true;
try {
emitBeforeN(asyncId);
didThrow = false;
} finally {
if (didThrow) clearFatalExceptionHandlers();
}
}


// Called from native. The asyncId stack handling is taken care of there before
// this is called.
function emitAfterN(asyncId) {
processing_hook = 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);
}
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);
}
} catch (e) {
fatalError(e);
}
processing_hook = false;

Expand All @@ -409,8 +411,16 @@ function emitAfterN(asyncId) {
// kIdStackIndex. But what happens if the user doesn't have both before and
// after callbacks.
function emitAfterS(asyncId) {
if (async_hook_fields[kAfter] > 0)
emitAfterN(asyncId);
if (async_hook_fields[kAfter] > 0) {
// Remove prepear for a fatal exception in case an error occurred
Copy link
Member

Choose a reason for hiding this comment

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

s/prepear/prepare

Copy link
Contributor

Choose a reason for hiding this comment

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

@AndreasMadsen is the comment a TODO? Otherwise I'm not sure I understand what It means.

var didThrow = true;
try {
emitAfterN(asyncId);
didThrow = false;
} finally {
if (didThrow) clearFatalExceptionHandlers();
}
}

popAsyncIds(asyncId);
}
Expand All @@ -427,15 +437,10 @@ function emitDestroyS(asyncId) {

function emitDestroyN(asyncId) {
processing_hook = 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);
}
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);
}
processing_hook = false;

Expand All @@ -460,18 +465,13 @@ function emitDestroyN(asyncId) {
// exceptions.
function init(asyncId, type, triggerAsyncId, resource) {
processing_hook = 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][init_symbol] === 'function') {
active_hooks_array[i][init_symbol](
asyncId, type, triggerAsyncId,
resource
);
}
for (var i = 0; i < active_hooks_array.length; i++) {
if (typeof active_hooks_array[i][init_symbol] === 'function') {
active_hooks_array[i][init_symbol](
asyncId, type, triggerAsyncId,
resource
);
}
} catch (e) {
fatalError(e);
}
processing_hook = false;
}
Expand Down
10 changes: 7 additions & 3 deletions test/async-hooks/test-callback-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const async_hooks = require('async_hooks');
const initHooks = require('./init-hooks');

const arg = process.argv[2];
if (arg) {
process.on('uncaughtException', common.mustNotCall());
}

switch (arg) {
case 'test_init_callback':
initHooks({
Expand Down Expand Up @@ -50,7 +54,7 @@ assert.ok(!arg);
console.time('end case 1');
const child = spawnSync(process.execPath, [__filename, 'test_init_callback']);
assert.ifError(child.error);
const test_init_first_line = child.stderr.toString().split(/[\r\n]+/g)[0];
const test_init_first_line = child.stderr.toString().split(/[\r\n]+/g)[3];
assert.strictEqual(test_init_first_line, 'Error: test_init_callback');
assert.strictEqual(child.status, 1);
console.timeEnd('end case 1');
Expand All @@ -61,7 +65,7 @@ assert.ok(!arg);
console.time('end case 2');
const child = spawnSync(process.execPath, [__filename, 'test_callback']);
assert.ifError(child.error);
const test_callback_first_line = child.stderr.toString().split(/[\r\n]+/g)[0];
const test_callback_first_line = child.stderr.toString().split(/[\r\n]+/g)[3];
assert.strictEqual(test_callback_first_line, 'Error: test_callback');
assert.strictEqual(child.status, 1);
console.timeEnd('end case 2');
Expand Down Expand Up @@ -115,6 +119,6 @@ assert.ok(!arg);
assert.strictEqual(stdout, '');
const firstLineStderr = stderr.split(/[\r\n]+/g)[0].trim();
assert.strictEqual(firstLineStderr, 'Error: test_callback_abort');
console.timeEnd('end case 3');
});
console.timeEnd('end case 3');
}
4 changes: 2 additions & 2 deletions test/async-hooks/test-emit-before-after.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ switch (process.argv[2]) {
}

const c1 = spawnSync(process.execPath, [__filename, 'test_invalid_async_id']);
assert.strictEqual(c1.stderr.toString().split(/[\r\n]+/g)[0],
assert.strictEqual(c1.stderr.toString().split(/[\r\n]+/g)[3],
'Error: before(): asyncId or triggerAsyncId is less than ' +
'zero (asyncId: -1, triggerAsyncId: -1)');
assert.strictEqual(c1.status, 1);

const c2 = spawnSync(process.execPath, [__filename, 'test_invalid_trigger_id']);
assert.strictEqual(c2.stderr.toString().split(/[\r\n]+/g)[0],
assert.strictEqual(c2.stderr.toString().split(/[\r\n]+/g)[3],
'Error: before(): asyncId or triggerAsyncId is less than ' +
'zero (asyncId: 1, triggerAsyncId: -1)');
assert.strictEqual(c2.status, 1);
Expand Down