Skip to content

lib: add trace-sigint APIs #59040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -4059,6 +4059,10 @@ This property refers to the value of underlying file descriptor of
`process.stderr`. The value is fixed at `2`. In [`Worker`][] threads,
this field does not exist.

## `process.startTraceSigInt`

Prints a stack trace on `SIGINT`.

## `process.stdin`

* {Stream}
Expand Down Expand Up @@ -4168,6 +4172,10 @@ false

See the [TTY][] documentation for more information.

## `process.stopTraceSigInt`

Stop Printing a stack trace on `SIGINT`.

## `process.throwDeprecation`

<!-- YAML
Expand Down
21 changes: 17 additions & 4 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,14 +382,27 @@ function setupCodeCoverage() {
}
}


let sigintWatchdog;
function getSigintWatchdog() {
if (!sigintWatchdog) {
const { SigintWatchdog } = require('internal/watchdog');
sigintWatchdog = new SigintWatchdog();
}
return sigintWatchdog;
}

function setupStacktracePrinterOnSigint() {
process.startTraceSigInt = function() {
getSigintWatchdog().start();
};
process.stopTraceSigInt = function() {
getSigintWatchdog().stop();
};
if (!getOptionValue('--trace-sigint')) {
return;
}
const { SigintWatchdog } = require('internal/watchdog');

const watchdog = new SigintWatchdog();
watchdog.start();
process.startTraceSigInt();
}

function initializeReport() {
Expand Down
1 change: 0 additions & 1 deletion lib/internal/watchdog.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class SigintWatchdog extends TraceSigintWatchdog {
}
}


module.exports = {
SigintWatchdog,
};
32 changes: 32 additions & 0 deletions test/parallel/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');

if (!process.env.CHILD) {
const cp = childProcess.spawn(
process.execPath,
[__filename],
{
env: { ...process.env, CHILD: '1' },
});
let data;
cp.stderr.on('data', (chunk) => {
data = data ? data + chunk : chunk;
});
cp.stderr.on('end', () => {
console.log(data);
assert.strictEqual(/SIGINT/.test(data.toString()), true);
});
cp.on('exit', mustCall((code, signal) => {
assert.strictEqual(signal, 'SIGINT');
assert.strictEqual(code, null);
}));
} else {
process.startTraceSigInt();
// Deactivate colors even if the tty does support colors.
process.env.NODE_DISABLE_COLORS = '1';
process.kill(process.pid, 'SIGINT');
while (true);
}
33 changes: 33 additions & 0 deletions test/parallel/test-stop-trace-sigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const { mustCall } = require('../common');
const childProcess = require('child_process');
const assert = require('assert');

if (!process.env.CHILD) {
{
const cp = childProcess.spawn(
process.execPath,
['--trace-sigint', __filename],
{
env: { ...process.env, CHILD: '1' },
});
let data;
cp.stderr.on('data', (chunk) => {
data = data ? data + chunk : chunk;
});
cp.stderr.on('end', () => {
assert.strictEqual(data === undefined, true);
});
cp.on('exit', mustCall((code, signal) => {
assert.strictEqual(signal, 'SIGINT');
assert.strictEqual(code, null);
}));
}
} else {
process.stopTraceSigInt();
// Deactivate colors even if the tty does support colors.
process.env.NODE_DISABLE_COLORS = '1';
process.kill(process.pid, 'SIGINT');
while (true);
}
Loading