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

lib: properly processing JavaScript exceptions on async_hooks fatal error #38106

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
13 changes: 11 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,18 @@ function executionAsyncResource() {
return lookupPublicResource(resource);
}

function inspectExceptionValue(e) {
inspect ??= require('internal/util/inspect').inspect;
const o = { message: inspect(e) };
return o;
legendecas marked this conversation as resolved.
Show resolved Hide resolved
}

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