@@ -97,9 +97,9 @@ export class Telemetry {
97
97
this . disabled = ! ! process . env . OBSERVABLE_TELEMETRY_DISABLE ;
98
98
this . debug = ! ! process . env . OBSERVABLE_TELEMETRY_DEBUG ;
99
99
this . endpoint = new URL ( "/cli" , getOrigin ( process . env ) ) ;
100
- process . on ( "SIGHUP" , this . handleSignal ( 1 ) ) ;
101
- process . on ( "SIGINT" , this . handleSignal ( 2 ) ) ;
102
- process . on ( "SIGTERM" , this . handleSignal ( 15 ) ) ;
100
+ this . handleSignal ( "SIGHUP" ) ;
101
+ this . handleSignal ( "SIGINT" ) ;
102
+ this . handleSignal ( "SIGTERM" ) ;
103
103
}
104
104
105
105
record ( data : TelemetryData ) {
@@ -122,17 +122,23 @@ export class Telemetry {
122
122
return Promise . all ( this . _pending ) ;
123
123
}
124
124
125
- private handleSignal ( value : number ) {
126
- const code = 128 + value ;
127
- return async ( signal : NodeJS . Signals ) => {
128
- const { process } = this . effects ;
129
- // Give ourselves 1s to record a signal event and flush.
130
- const deadline = setTimeout ( ( ) => process . exit ( code ) , 1000 ) ;
125
+ private handleSignal ( name : string ) {
126
+ const { process } = this . effects ;
127
+ let exiting = false ;
128
+ const signaled = async ( signal : NodeJS . Signals ) => {
129
+ if ( exiting ) return ; // already exiting
130
+ exiting = true ;
131
131
this . record ( { event : "signal" , signal} ) ;
132
- await this . pending ;
133
- clearTimeout ( deadline ) ;
134
- process . exit ( code ) ;
132
+ try {
133
+ // Allow one second to record a signal event and flush.
134
+ await Promise . race ( [ this . pending , new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ] ) ;
135
+ } catch {
136
+ // ignore error
137
+ }
138
+ process . off ( name , signaled ) ; // don’t handle our own kill
139
+ process . kill ( process . pid , signal ) ;
135
140
} ;
141
+ process . on ( name , signaled ) ;
136
142
}
137
143
138
144
private async getPersistentId ( name : string , generator = randomUUID ) {
0 commit comments