1'use strict'; 2 3const { 4 TraceSigintWatchdog, 5} = internalBinding('watchdog'); 6 7class SigintWatchdog extends TraceSigintWatchdog { 8 _started = false; 9 _effective = false; 10 _onNewListener = (eve) => { 11 if (eve === 'SIGINT' && this._effective) { 12 super.stop(); 13 this._effective = false; 14 } 15 }; 16 _onRemoveListener = (eve) => { 17 if (eve === 'SIGINT' && process.listenerCount('SIGINT') === 0 && 18 !this._effective) { 19 super.start(); 20 this._effective = true; 21 } 22 }; 23 24 start() { 25 if (this._started) { 26 return; 27 } 28 this._started = true; 29 // Prepend sigint newListener to remove stop watchdog before signal wrap 30 // been activated. Also make sigint removeListener been ran after signal 31 // wrap been stopped. 32 process.prependListener('newListener', this._onNewListener); 33 process.addListener('removeListener', this._onRemoveListener); 34 35 if (process.listenerCount('SIGINT') === 0) { 36 super.start(); 37 this._effective = true; 38 } 39 } 40 41 stop() { 42 if (!this._started) { 43 return; 44 } 45 this._started = false; 46 process.removeListener('newListener', this._onNewListener); 47 process.removeListener('removeListener', this._onRemoveListener); 48 49 if (this._effective) { 50 super.stop(); 51 this._effective = false; 52 } 53 } 54} 55 56 57module.exports = { 58 SigintWatchdog, 59}; 60