forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-async-hooks-stack-overflow.js
More file actions
47 lines (38 loc) · 1.46 KB
/
Copy pathtest-async-hooks-stack-overflow.js
File metadata and controls
47 lines (38 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'use strict';
// This test verifies that when a stack overflow occurs with async_hooks
// enabled, the uncaughtException handler is still called instead of the
// process crashing with exit code 7.
const common = require('../common');
const assert = require('assert');
const { spawnSync } = require('child_process');
if (process.argv[2] === 'child') {
const { createHook } = require('async_hooks');
let handlerCalled = false;
function recursive() {
// Create a promise to trigger async_hooks init callback
new Promise(() => {});
return recursive();
}
createHook({ init() {} }).enable();
process.on('uncaughtException', common.mustCall((err) => {
assert.strictEqual(err.name, 'RangeError');
assert.match(err.message, /Maximum call stack size exceeded/);
// Ensure handler is only called once
assert.strictEqual(handlerCalled, false);
handlerCalled = true;
}));
setImmediate(recursive);
} else {
// Parent process - spawn the child and check exit code
const result = spawnSync(
process.execPath,
[__filename, 'child'],
{ encoding: 'utf8', timeout: 30000 }
);
// Should exit with code 0 (handler was called and handled the exception)
// Previously would exit with code 7 (kExceptionInFatalExceptionHandler)
assert.strictEqual(result.status, 0,
`Expected exit code 0, got ${result.status}.\n` +
`stdout: ${result.stdout}\n` +
`stderr: ${result.stderr}`);
}