Skip to content

Commit d3eadac

Browse files
committed
lib: add trace-sigint APIs
1 parent 049664b commit d3eadac

File tree

7 files changed

+100
-5
lines changed

7 files changed

+100
-5
lines changed

doc/api/process.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4059,6 +4059,10 @@ This property refers to the value of underlying file descriptor of
40594059
`process.stderr`. The value is fixed at `2`. In [`Worker`][] threads,
40604060
this field does not exist.
40614061
4062+
## `process.startTraceSigInt`
4063+
4064+
Prints a stack trace on `SIGINT`.
4065+
40624066
## `process.stdin`
40634067
40644068
* {Stream}
@@ -4168,6 +4172,10 @@ false
41684172
41694173
See the [TTY][] documentation for more information.
41704174
4175+
## `process.stopTraceSigInt`
4176+
4177+
Stop Printing a stack trace on `SIGINT`.
4178+
41714179
## `process.throwDeprecation`
41724180
41734181
<!-- YAML

lib/internal/process/pre_execution.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,14 +382,27 @@ function setupCodeCoverage() {
382382
}
383383
}
384384

385+
386+
let sigintWatchdog;
387+
function getSigintWatchdog() {
388+
if (!sigintWatchdog) {
389+
const { SigintWatchdog } = require('internal/watchdog');
390+
sigintWatchdog = new SigintWatchdog();
391+
}
392+
return sigintWatchdog;
393+
}
394+
385395
function setupStacktracePrinterOnSigint() {
396+
process.startTraceSigInt = function() {
397+
getSigintWatchdog().start();
398+
};
399+
process.stopTraceSigInt = function() {
400+
getSigintWatchdog().stop();
401+
};
386402
if (!getOptionValue('--trace-sigint')) {
387403
return;
388404
}
389-
const { SigintWatchdog } = require('internal/watchdog');
390-
391-
const watchdog = new SigintWatchdog();
392-
watchdog.start();
405+
process.startTraceSigInt();
393406
}
394407

395408
function initializeReport() {

lib/internal/watchdog.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class SigintWatchdog extends TraceSigintWatchdog {
5353
}
5454
}
5555

56-
5756
module.exports = {
5857
SigintWatchdog,
5958
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const { mustCall } = require('../common');
4+
const childProcess = require('child_process');
5+
const assert = require('assert');
6+
7+
if (!process.env.CHILD) {
8+
const cp = childProcess.spawn(
9+
process.execPath,
10+
[__filename],
11+
{
12+
env: { ...process.env, CHILD: '1' },
13+
});
14+
let data;
15+
cp.stderr.on('data', (chunk) => {
16+
data = data ? data + chunk : chunk;
17+
});
18+
cp.stderr.on('end', () => {
19+
console.log(data);
20+
assert.strictEqual(/SIGINT/.test(data.toString()), true);
21+
});
22+
cp.on('exit', mustCall((code, signal) => {
23+
assert.strictEqual(signal, 'SIGINT');
24+
assert.strictEqual(code, null);
25+
}));
26+
} else {
27+
process.startTraceSigInt();
28+
// Deactivate colors even if the tty does support colors.
29+
process.env.NODE_DISABLE_COLORS = '1';
30+
process.kill(process.pid, 'SIGINT');
31+
while (true);
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
KEYBOARD_INTERRUPT: Script execution was interrupted by `SIGINT`
2+
at main (*/test-start-trace-sigint.js:*)
3+
at */test-start-trace-sigint.js:*
4+
at *
5+
at *
6+
at *
7+
at *
8+
at *
9+
at *
10+
at *
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const { mustCall } = require('../common');
4+
const childProcess = require('child_process');
5+
const assert = require('assert');
6+
7+
if (!process.env.CHILD) {
8+
{
9+
const cp = childProcess.spawn(
10+
process.execPath,
11+
['--trace-sigint', __filename],
12+
{
13+
env: { ...process.env, CHILD: '1' },
14+
});
15+
let data;
16+
cp.stderr.on('data', (chunk) => {
17+
data = data ? data + chunk : chunk;
18+
});
19+
cp.stderr.on('end', () => {
20+
assert.strictEqual(data === undefined, true);
21+
});
22+
cp.on('exit', mustCall((code, signal) => {
23+
assert.strictEqual(signal, 'SIGINT');
24+
assert.strictEqual(code, null);
25+
}));
26+
}
27+
} else {
28+
process.stopTraceSigInt();
29+
// Deactivate colors even if the tty does support colors.
30+
process.env.NODE_DISABLE_COLORS = '1';
31+
process.kill(process.pid, 'SIGINT');
32+
while (true);
33+
}

test/pseudo-tty/test-stop-trace-sigint.out

Whitespace-only changes.

0 commit comments

Comments
 (0)