Skip to content

Commit

Permalink
lib: properly processing JavaScript exceptions on async_hooks fatal e…
Browse files Browse the repository at this point in the history
…rror

JavaScript exceptions could be arbitrary values.
  • Loading branch information
legendecas committed Apr 6, 2021
1 parent a9332e8 commit bdc1aa2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function executionAsyncResource() {

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

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

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

createHook({
[type]() {
// 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' ];
for (const type of types) {
const cp = childProcess.spawnSync(process.execPath,
[ __filename, 'child', type ],
{
encoding: 'utf8',
});
assert.strictEqual(cp.status, 1, type);
assert.strictEqual(cp.stderr.trim().split('\n')[0], 'Error: null', type);
}
}

0 comments on commit bdc1aa2

Please sign in to comment.