mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
lib: add trace-sigint APIs
PR-URL: https://github.com/nodejs/node/pull/59040 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
@@ -739,6 +739,16 @@ fs.access('file/that/does/not/exist', (err) => {
|
||||
});
|
||||
```
|
||||
|
||||
## `util.setTraceSigInt(enable)`
|
||||
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* `enable` {boolean}
|
||||
|
||||
Enable or disable printing a stack trace on `SIGINT`. The API is only available on the main thread.
|
||||
|
||||
## `util.inherits(constructor, superConstructor)`
|
||||
|
||||
<!-- YAML
|
||||
|
||||
@@ -393,10 +393,7 @@ function setupStacktracePrinterOnSigint() {
|
||||
if (!getOptionValue('--trace-sigint')) {
|
||||
return;
|
||||
}
|
||||
const { SigintWatchdog } = require('internal/watchdog');
|
||||
|
||||
const watchdog = new SigintWatchdog();
|
||||
watchdog.start();
|
||||
require('internal/util/trace_sigint').setTraceSigInt(true);
|
||||
}
|
||||
|
||||
function initializeReport() {
|
||||
|
||||
29
lib/internal/util/trace_sigint.js
Normal file
29
lib/internal/util/trace_sigint.js
Normal file
@@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
const { isMainThread } = require('worker_threads');
|
||||
const {
|
||||
ERR_WORKER_UNSUPPORTED_OPERATION,
|
||||
} = require('internal/errors').codes;
|
||||
|
||||
let sigintWatchdog;
|
||||
function getSigintWatchdog() {
|
||||
if (!sigintWatchdog) {
|
||||
const { SigintWatchdog } = require('internal/watchdog');
|
||||
sigintWatchdog = new SigintWatchdog();
|
||||
}
|
||||
return sigintWatchdog;
|
||||
}
|
||||
|
||||
function setTraceSigInt(enable) {
|
||||
if (!isMainThread)
|
||||
throw new ERR_WORKER_UNSUPPORTED_OPERATION('Calling util.setTraceSigInt');
|
||||
if (enable) {
|
||||
getSigintWatchdog().start();
|
||||
} else {
|
||||
getSigintWatchdog().stop();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
setTraceSigInt,
|
||||
};
|
||||
@@ -535,3 +535,9 @@ defineLazyProperties(
|
||||
'internal/util/diff',
|
||||
['diff'],
|
||||
);
|
||||
|
||||
defineLazyProperties(
|
||||
module.exports,
|
||||
'internal/util/trace_sigint',
|
||||
['setTraceSigInt'],
|
||||
);
|
||||
|
||||
20
test/parallel/test-trace-sigint-in-worker.js
Normal file
20
test/parallel/test-trace-sigint-in-worker.js
Normal file
@@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
|
||||
const assert = require('assert');
|
||||
const util = require('util');
|
||||
const { Worker, workerData } = require('worker_threads');
|
||||
|
||||
if (workerData?.isWorker) {
|
||||
assert.throws(() => {
|
||||
util.setTraceSigInt(true);
|
||||
}, {
|
||||
code: 'ERR_WORKER_UNSUPPORTED_OPERATION',
|
||||
});
|
||||
} else {
|
||||
const w = new Worker(__filename, { workerData: { isWorker: true } });
|
||||
w.on('exit', common.mustCall((code) => {
|
||||
assert.strictEqual(code, 0);
|
||||
}));
|
||||
}
|
||||
32
test/pseudo-tty/test-start-trace-sigint.js
Normal file
32
test/pseudo-tty/test-start-trace-sigint.js
Normal file
@@ -0,0 +1,32 @@
|
||||
'use strict';
|
||||
|
||||
const { mustCall } = require('../common');
|
||||
const childProcess = require('child_process');
|
||||
const assert = require('assert');
|
||||
const util = require('util');
|
||||
|
||||
if (process.env.CHILD === 'true') {
|
||||
main();
|
||||
} else {
|
||||
// Use inherited stdio child process to prevent test tools from determining
|
||||
// the case as crashed from SIGINT
|
||||
const cp = childProcess.spawn(
|
||||
process.execPath,
|
||||
[__filename],
|
||||
{
|
||||
env: { ...process.env, CHILD: 'true' },
|
||||
stdio: 'inherit',
|
||||
});
|
||||
cp.on('exit', mustCall((code, signal) => {
|
||||
assert.strictEqual(signal, 'SIGINT');
|
||||
assert.strictEqual(code, null);
|
||||
}));
|
||||
}
|
||||
|
||||
function main() {
|
||||
util.setTraceSigInt(true);
|
||||
// Deactivate colors even if the tty does support colors.
|
||||
process.env.NODE_DISABLE_COLORS = '1';
|
||||
process.kill(process.pid, 'SIGINT');
|
||||
while (true);
|
||||
}
|
||||
11
test/pseudo-tty/test-start-trace-sigint.out
Normal file
11
test/pseudo-tty/test-start-trace-sigint.out
Normal file
@@ -0,0 +1,11 @@
|
||||
KEYBOARD_INTERRUPT: Script execution was interrupted by `SIGINT`
|
||||
at main (*/test-start-trace-sigint.js:*)
|
||||
at */test-start-trace-sigint.js:*
|
||||
at *
|
||||
at *
|
||||
at *
|
||||
at *
|
||||
at *
|
||||
at *
|
||||
at *
|
||||
at *
|
||||
32
test/pseudo-tty/test-stop-trace-sigint.js
Normal file
32
test/pseudo-tty/test-stop-trace-sigint.js
Normal file
@@ -0,0 +1,32 @@
|
||||
'use strict';
|
||||
|
||||
const { mustCall } = require('../common');
|
||||
const childProcess = require('child_process');
|
||||
const assert = require('assert');
|
||||
const util = require('util');
|
||||
|
||||
if (process.env.CHILD === 'true') {
|
||||
main();
|
||||
} else {
|
||||
// Use inherited stdio child process to prevent test tools from determining
|
||||
// the case as crashed from SIGINT
|
||||
const cp = childProcess.spawn(
|
||||
process.execPath,
|
||||
['--trace-sigint', __filename],
|
||||
{
|
||||
env: { ...process.env, CHILD: 'true' },
|
||||
stdio: 'inherit',
|
||||
});
|
||||
cp.on('exit', mustCall((code, signal) => {
|
||||
assert.strictEqual(signal, 'SIGINT');
|
||||
assert.strictEqual(code, null);
|
||||
}));
|
||||
}
|
||||
|
||||
function main() {
|
||||
util.setTraceSigInt(false);
|
||||
// Deactivate colors even if the tty does support colors.
|
||||
process.env.NODE_DISABLE_COLORS = '1';
|
||||
process.kill(process.pid, 'SIGINT');
|
||||
while (true);
|
||||
}
|
||||
0
test/pseudo-tty/test-stop-trace-sigint.out
Normal file
0
test/pseudo-tty/test-stop-trace-sigint.out
Normal file
Reference in New Issue
Block a user