Skip to content

Commit

Permalink
lib: properly process JavaScript exceptions on async_hooks fatal error
Browse files Browse the repository at this point in the history
JavaScript exceptions could be arbitrary values.

PR-URL: #38106
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
legendecas authored and Flarna committed Apr 8, 2021
1 parent 6cb314b commit d861324
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ const {
const { async_id_symbol,
trigger_async_id_symbol } = internalBinding('symbols');

// Lazy load of internal/util/inspect;
let inspect;

// Used in AsyncHook and AsyncResource.
const init_symbol = Symbol('init');
const before_symbol = Symbol('before');
Expand Down Expand Up @@ -156,12 +159,17 @@ function executionAsyncResource() {
return lookupPublicResource(resource);
}

function inspectExceptionValue(e) {
inspect ??= require('internal/util/inspect').inspect;
return { message: inspect(e) };
}

// Used to fatally abort the process if a callback throws.
function fatalError(e) {
if (typeof e.stack === 'string') {
if (typeof e?.stack === 'string') {
process._rawDebug(e.stack);
} else {
const o = { message: e };
const o = inspectExceptionValue(e);
ErrorCaptureStackTrace(o, fatalError);
process._rawDebug(o.stack);
}
Expand Down
53 changes: 53 additions & 0 deletions test/parallel/test-async-hooks-fatal-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';
require('../common');
const assert = require('assert');
const childProcess = require('child_process');
const os = require('os');

if (process.argv[2] === 'child') {
child(process.argv[3], process.argv[4]);
} else {
main();
}

function child(type, valueType) {
const { createHook } = require('async_hooks');
const fs = require('fs');

createHook({
[type]() {
if (valueType === 'symbol') {
throw Symbol('foo');
}
// eslint-disable-next-line no-throw-literal
throw null;
}
}).enable();

// Trigger `promiseResolve`.
new Promise((resolve) => resolve())
// Trigger `after`/`destroy`.
.then(() => fs.promises.readFile(__filename, 'utf8'))
// Make process exit with code 0 if no error caught.
.then(() => process.exit(0));
}

function main() {
const types = [ 'init', 'before', 'after', 'destroy', 'promiseResolve' ];
const valueTypes = [
[ 'null', 'Error: null' ],
[ 'symbol', 'Error: Symbol(foo)' ],
];
for (const type of types) {
for (const [valueType, expect] of valueTypes) {
const cp = childProcess.spawnSync(
process.execPath,
[ __filename, 'child', type, valueType ],
{
encoding: 'utf8',
});
assert.strictEqual(cp.status, 1, type);
assert.strictEqual(cp.stderr.trim().split(os.EOL)[0], expect, type);
}
}
}

0 comments on commit d861324

Please sign in to comment.