File tree Expand file tree Collapse file tree 5 files changed +93
-5
lines changed Expand file tree Collapse file tree 5 files changed +93
-5
lines changed Original file line number Diff line number Diff line change @@ -4059,6 +4059,16 @@ This property refers to the value of underlying file descriptor of
40594059` process .stderr ` . The value is fixed at ` 2 ` . In [` Worker ` ][] threads,
40604060this field does not exist.
40614061
4062+ ## ` process .setTraceSigInt (enable)`
4063+
4064+ <!-- YAML
4065+ added: REPLACEME
4066+ -->
4067+
4068+ * ` enable` {boolean}
4069+
4070+ Enable or disable printing a stack trace on ` SIGINT ` . The API is only available on the main thread.
4071+
40624072## ` process .stdin `
40634073
40644074* {Stream}
Original file line number Diff line number Diff line change @@ -382,14 +382,28 @@ 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+
385395function setupStacktracePrinterOnSigint ( ) {
396+ process . setTraceSigInt = function ( enable ) {
397+ if ( enable ) {
398+ getSigintWatchdog ( ) . start ( ) ;
399+ } else {
400+ getSigintWatchdog ( ) . stop ( ) ;
401+ }
402+ } ;
386403 if ( ! getOptionValue ( '--trace-sigint' ) ) {
387404 return ;
388405 }
389- const { SigintWatchdog } = require ( 'internal/watchdog' ) ;
390-
391- const watchdog = new SigintWatchdog ( ) ;
392- watchdog . start ( ) ;
406+ process . setTraceSigInt ( true ) ;
393407}
394408
395409function initializeReport ( ) {
Original file line number Diff line number Diff line change @@ -53,7 +53,6 @@ class SigintWatchdog extends TraceSigintWatchdog {
5353 }
5454}
5555
56-
5756module . exports = {
5857 SigintWatchdog,
5958} ;
Original file line number Diff line number Diff line change 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 ( / S I G I N T / . 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 . setTraceSigInt ( true ) ;
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+ }
Original file line number Diff line number Diff line change 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 . setTraceSigInt ( false ) ;
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+ }
You can’t perform that action at this time.
0 commit comments