@@ -180,19 +180,18 @@ function setupKillAndExit() {
180180
181181var ttdSigIntHandler = undefined ;
182182function setupSignalHandlers ( ) {
183- // Load events module in order to access prototype elements on process like
184- // process.addListener.
185- const signalWraps = { } ;
183+ const signalWraps = Object . create ( null ) ;
184+ let Signal ;
186185
187186 function isSignal ( event ) {
188187 return typeof event === 'string' && constants [ event ] !== undefined ;
189188 }
190189
191190 // Detect presence of a listener for the special signal types
192- process . on ( 'newListener' , function ( type , listener ) {
193- if ( isSignal ( type ) &&
194- ! signalWraps . hasOwnProperty ( type ) ) {
195- const Signal = process . binding ( 'signal_wrap' ) . Signal ;
191+ process . on ( 'newListener' , function ( type ) {
192+ if ( isSignal ( type ) && signalWraps [ type ] === undefined ) {
193+ if ( Signal === undefined )
194+ Signal = process . binding ( 'signal_wrap' ) . Signal ;
196195 const wrap = new Signal ( ) ;
197196
198197 wrap . unref ( ) ;
@@ -203,7 +202,7 @@ function setupSignalHandlers() {
203202 // -- ttdSigIntHandler will emit event after it runs
204203 wrap . onsignal = function ( ) { } ; //ENABLE_TTD
205204 } else {
206- wrap . onsignal = function ( ) { process . emit ( type , type ) ; } ;
205+ wrap . onsignal = process . emit . bind ( process , type , type ) ;
207206 }
208207
209208 const signum = constants [ type ] ;
@@ -217,8 +216,8 @@ function setupSignalHandlers() {
217216 }
218217 } ) ;
219218
220- process . on ( 'removeListener' , function ( type , listener ) {
221- if ( signalWraps . hasOwnProperty ( type ) && this . listenerCount ( type ) === 0 ) {
219+ process . on ( 'removeListener' , function ( type ) {
220+ if ( signalWraps [ type ] !== undefined && this . listenerCount ( type ) === 0 ) {
222221 signalWraps [ type ] . close ( ) ;
223222 delete signalWraps [ type ] ;
224223 }
@@ -227,23 +226,30 @@ function setupSignalHandlers() {
227226 //ENABLE_TTD
228227 if ( global . enabledDiagnosticsTrace ) {
229228 ( function ( ) {
229+ const type = 'SIGINT' ;
230230 var Signal = process . binding ( 'signal_wrap' ) . Signal ;
231231 var wrap = new Signal ( ) ;
232232
233233 wrap . unref ( ) ;
234234
235235 var handler = require ( 'trace_mgr' ) . onSigIntHandler ;
236+ const userHandler = process . emit . bind ( process , type , type ) ;
236237 wrap . onsignal = function ( ) {
237- handler ( signalWraps . hasOwnProperty ( 'SIGINT' ) ) ;
238- process . emit ( 'SIGINT' ) ;
238+ // If signalWraps[type] exists, then the user script
239+ // has set a handler for sigint
240+ // If there is no user script handler, then
241+ // handler here will terminate the process
242+ handler ( signalWraps [ type ] !== undefined ) ;
243+ // Otherwise, we should trigger the same function
244+ // that would be triggered if TTD were not present
245+ return userHandler ( ) ;
239246 } ;
240247
241- const signum = constants . SIGINT ;
248+ const signum = constants [ type ] ;
242249 const err = wrap . start ( signum ) ;
243250 if ( err ) {
244251 wrap . close ( ) ;
245- const errnoException = require ( 'util' ) . _errnoException ;
246- throw errnoException ( err , 'uv_signal_start' ) ;
252+ throw util . _errnoException ( err , 'uv_signal_start' ) ;
247253 }
248254
249255 if ( ! ttdSigIntHandler ) {
@@ -264,14 +270,7 @@ function setupChannel() {
264270 // Make sure it's not accidentally inherited by child processes.
265271 delete process . env . NODE_CHANNEL_FD ;
266272
267- const cp = require ( 'child_process' ) ;
268-
269- // Load tcp_wrap to avoid situation where we might immediately receive
270- // a message.
271- // FIXME is this really necessary?
272- process . binding ( 'tcp_wrap' ) ;
273-
274- cp . _forkChild ( fd ) ;
273+ require ( 'child_process' ) . _forkChild ( fd ) ;
275274 assert ( process . send ) ;
276275 }
277276}
0 commit comments