-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: properly process JavaScript exceptions on async_hooks fatal error
JavaScript exceptions could be arbitrary values. PR-URL: #38106 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
1 parent
6cb314b
commit d861324
Showing
2 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |