@@ -97,9 +97,9 @@ export class Telemetry {
9797 this . disabled = ! ! process . env . OBSERVABLE_TELEMETRY_DISABLE ;
9898 this . debug = ! ! process . env . OBSERVABLE_TELEMETRY_DEBUG ;
9999 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" ) ;
103103 }
104104
105105 record ( data : TelemetryData ) {
@@ -122,17 +122,23 @@ export class Telemetry {
122122 return Promise . all ( this . _pending ) ;
123123 }
124124
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 ;
131131 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 ) ;
135140 } ;
141+ process . on ( name , signaled ) ;
136142 }
137143
138144 private async getPersistentId ( name : string , generator = randomUUID ) {
0 commit comments