-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It could be convenient to trace abnormal exit of the Node.js processes that printing stacktrace on each `process.exit` call with a cli option. This also takes effects on worker threads. PR-URL: #30516 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
1 parent
1bcbc70
commit 8dc4e4e
Showing
6 changed files
with
92 additions
and
0 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
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
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,59 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { promisify } = require('util'); | ||
const execFile = promisify(require('child_process').execFile); | ||
const { Worker, isMainThread, workerData } = require('worker_threads'); | ||
|
||
const variant = process.argv[process.argv.length - 1]; | ||
switch (true) { | ||
case variant === 'main-thread': { | ||
return; | ||
} | ||
case variant === 'main-thread-exit': { | ||
return process.exit(0); | ||
} | ||
case variant.startsWith('worker-thread'): { | ||
const worker = new Worker(__filename, { workerData: variant }); | ||
worker.on('error', common.mustNotCall()); | ||
worker.on('exit', common.mustCall((code) => { | ||
assert.strictEqual(code, 0); | ||
})); | ||
return; | ||
} | ||
case !isMainThread: { | ||
if (workerData === 'worker-thread-exit') { | ||
process.exit(0); | ||
} | ||
return; | ||
} | ||
} | ||
|
||
(async function() { | ||
for (const { execArgv, variant, warnings } of [ | ||
{ execArgv: ['--trace-exit'], variant: 'main-thread-exit', warnings: 1 }, | ||
{ execArgv: [], variant: 'main-thread-exit', warnings: 0 }, | ||
{ execArgv: ['--trace-exit'], variant: 'main-thread', warnings: 0 }, | ||
{ execArgv: [], variant: 'main-thread', warnings: 0 }, | ||
{ execArgv: ['--trace-exit'], variant: 'worker-thread-exit', warnings: 1 }, | ||
{ execArgv: [], variant: 'worker-thread-exit', warnings: 0 }, | ||
{ execArgv: ['--trace-exit'], variant: 'worker-thread', warnings: 0 }, | ||
{ execArgv: [], variant: 'worker-thread', warnings: 0 }, | ||
]) { | ||
const { stdout, stderr } = | ||
await execFile(process.execPath, [...execArgv, __filename, variant]); | ||
assert.strictEqual(stdout, ''); | ||
const actualWarnings = | ||
stderr.match(/WARNING: Exited the environment with code 0/g); | ||
if (warnings === 0) { | ||
assert.strictEqual(actualWarnings, null); | ||
return; | ||
} | ||
assert.strictEqual(actualWarnings.length, warnings); | ||
|
||
if (variant.startsWith('worker')) { | ||
const workerIds = stderr.match(/\(node:\d+, thread:\d+)/g); | ||
assert.strictEqual(workerIds.length, warnings); | ||
} | ||
} | ||
})().then(common.mustCall()); |