@@ -31,12 +31,29 @@ interface Terminable {
31
31
terminate ( this : Terminable ) : any
32
32
}
33
33
34
- // Terminates the workers, empties the workers array, and exits.
34
+ // Terminates the workers, empties the workers array, and possibly exits.
35
35
const onSignal = ( workers : Terminable [ ] , signal : string ) => {
36
36
// worker.terminate() might return a Promise or might be synchronous. This async helper function
37
37
// creates a consistent interface.
38
38
const terminate = async ( worker : Terminable ) => worker . terminate ( )
39
- Promise . all ( workers . map ( worker => terminate ( worker ) . catch ( ( ) => { } ) ) ) . then ( ( ) => process . exit ( 1 ) )
39
+ Promise . all ( workers . map ( worker => terminate ( worker ) . catch ( ( ) => { } ) ) ) . then ( ( ) => {
40
+ // Adding a signal listener suppresses the default signal handling behavior. That default
41
+ // behavior must be replicated here, but only if the default behavior isn't intentionally
42
+ // suppressed by another signal listener. Unfortunately there is no robust way to determine
43
+ // whether the default behavior was intentionally suppressed, so a heuristic is used. (Note: The
44
+ // 'exit' event is not suitable for terminating workers because it is not emitted when the
45
+ // default signal handler terminates the process.)
46
+ if ( process . listenerCount ( signal ) > 1 ) {
47
+ // Assume that one of the other signal listeners will take care of calling process.exit().
48
+ // This assumption breaks down if all of the other listeners are making the same assumption.
49
+ return
50
+ }
51
+ // Right now this is the only signal listener, so assume that this listener is to blame for
52
+ // inhibiting the default signal handler. (This assumption fails if the number of listeners
53
+ // changes during signal handling. This can happen if a listener was added by process.once().)
54
+ // Mimic the default behavior, which is to exit with a non-0 code.
55
+ process . exit ( 1 )
56
+ } )
40
57
workers . length = 0
41
58
}
42
59
0 commit comments