Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
29 changes: 29 additions & 0 deletions lib/internal/util/trace_sigint.js
Original file line number Diff line number Diff line change
@@ -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,
};
6 changes: 6 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,9 @@ defineLazyProperties(
'internal/util/diff',
['diff'],
);

defineLazyProperties(
module.exports,
'internal/util/trace_sigint',
['setTraceSigInt'],
);
20 changes: 20 additions & 0 deletions test/parallel/test-trace-sigint-in-worker.js
Original file line number Diff line number Diff line change
@@ -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 changes: 32 additions & 0 deletions test/pseudo-tty/test-start-trace-sigint.js
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions test/pseudo-tty/test-start-trace-sigint.out
Original file line number Diff line number Diff line change
@@ -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 changes: 32 additions & 0 deletions test/pseudo-tty/test-stop-trace-sigint.js
Original file line number Diff line number Diff line change
@@ -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);
}
Empty file.
Loading