Skip to content

Commit bdc1aa2

Browse files
committed
lib: properly processing JavaScript exceptions on async_hooks fatal error
JavaScript exceptions could be arbitrary values.
1 parent a9332e8 commit bdc1aa2

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/internal/async_hooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function executionAsyncResource() {
158158

159159
// Used to fatally abort the process if a callback throws.
160160
function fatalError(e) {
161-
if (typeof e.stack === 'string') {
161+
if (e !== null && typeof e === 'object' && typeof e.stack === 'string') {
162162
process._rawDebug(e.stack);
163163
} else {
164164
const o = { message: e };
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const childProcess = require('child_process');
5+
6+
if (process.argv[2] === 'child') {
7+
child(process.argv[3]);
8+
} else {
9+
main();
10+
}
11+
12+
function child(type) {
13+
const { createHook } = require('async_hooks');
14+
const fs = require('fs');
15+
16+
createHook({
17+
[type]() {
18+
// eslint-disable-next-line no-throw-literal
19+
throw null;
20+
}
21+
}).enable();
22+
23+
// Trigger `promiseResolve`.
24+
new Promise((resolve) => resolve())
25+
// Trigger `after`/`destroy`.
26+
.then(() => fs.promises.readFile(__filename, 'utf8'))
27+
// Make process exit with code 0 if no error caught.
28+
.then(() => process.exit(0));
29+
}
30+
31+
function main() {
32+
const types = [ 'init', 'before', 'after', 'destroy', 'promiseResolve' ];
33+
for (const type of types) {
34+
const cp = childProcess.spawnSync(process.execPath,
35+
[ __filename, 'child', type ],
36+
{
37+
encoding: 'utf8',
38+
});
39+
assert.strictEqual(cp.status, 1, type);
40+
assert.strictEqual(cp.stderr.trim().split('\n')[0], 'Error: null', type);
41+
}
42+
}

0 commit comments

Comments
 (0)