@@ -176,43 +176,56 @@ function createBootstrapSpanIfRequested() {
176
176
177
177
function installProcessExitHandlers ( ) {
178
178
if ( hasOptedIn ( 'DASH0_FLUSH_ON_SIGTERM_SIGINT' ) ) {
179
+ printDebugStdout ( 'Installing process exit handler for SIGTERM/SIGINT.' ) ;
179
180
[ 'SIGTERM' , 'SIGINT' ] . forEach ( signal => {
180
- process . once ( signal , onProcessExit . bind ( null , true ) ) ;
181
+ process . once ( signal , onProcessExit . bind ( null , signal ) ) ;
181
182
} ) ;
182
183
}
183
184
184
185
if ( ! hasOptedOut ( 'DASH0_FLUSH_ON_EMPTY_EVENT_LOOP' ) ) {
185
- process . once ( 'beforeExit' , onProcessExit . bind ( null , false ) ) ;
186
+ printDebugStdout ( 'Installing process exit handler.' ) ;
187
+ process . once ( 'beforeExit' , onProcessExit . bind ( null ) ) ;
186
188
}
187
189
}
188
190
189
- async function onProcessExit ( callProcessExit : boolean ) {
190
- await executePromiseWithTimeout ( gracefulSdkShutdown ( callProcessExit ) , 500 , callProcessExit ) ;
191
+ async function onProcessExit ( signal ?: string ) {
192
+ if ( signal ) {
193
+ printDebugStdout ( 'Running process exit handler for signal:' , signal ) ;
194
+ } else {
195
+ printDebugStdout ( 'Running process exit handler.' ) ;
196
+ }
197
+ await executePromiseWithTimeout ( gracefulSdkShutdown ( signal ) , 500 , signal ) ;
191
198
}
192
199
193
- async function gracefulSdkShutdown ( callProcessExit : boolean ) {
200
+ async function gracefulSdkShutdown ( signal ?: string ) {
194
201
try {
195
202
if ( sdkShutdownHasBeenCalled ) {
196
- if ( callProcessExit ) {
197
- process . exit ( 0 ) ;
203
+ printDebugStdout ( 'Ignoring repeated request for graceful SDK shutdown.' ) ;
204
+ if ( signal ) {
205
+ // re-raise the signal to exit the process
206
+ printDebugStdout ( 'Re-raising signal' , signal ) ;
207
+ process . kill ( process . pid , signal ) ;
198
208
}
199
209
return ;
200
210
}
201
211
212
+ printDebugStdout ( 'Triggering graceful SDK shutdown.' ) ;
202
213
sdkShutdownHasBeenCalled = true ;
203
214
await sdk . shutdown ( ) ;
204
215
205
216
printDebugStdout ( 'OpenTelemetry SDK has been shut down successfully.' ) ;
206
217
} catch ( err ) {
207
218
console . error ( logPrefix , 'Error shutting down the OpenTelemetry SDK:' , err ) ;
208
219
} finally {
209
- if ( callProcessExit ) {
210
- process . exit ( 0 ) ;
220
+ if ( signal ) {
221
+ // re-raise the signal to exit the process
222
+ printDebugStdout ( 'Re-raising signal' , signal ) ;
223
+ process . kill ( process . pid , signal ) ;
211
224
}
212
225
}
213
226
}
214
227
215
- function executePromiseWithTimeout ( promise : Promise < any > , timeoutMillis : number , callProcessExit : boolean ) {
228
+ function executePromiseWithTimeout ( promise : Promise < any > , timeoutMillis : number , signal ?: string ) {
216
229
let setTimeoutId : NodeJS . Timeout ;
217
230
const timeoutPromise = new Promise ( resolve => {
218
231
setTimeoutId = setTimeout ( ( ) => {
@@ -228,8 +241,10 @@ function executePromiseWithTimeout(promise: Promise<any>, timeoutMillis: number,
228
241
if ( setTimeoutId ) {
229
242
clearTimeout ( setTimeoutId ) ;
230
243
}
231
- if ( callProcessExit ) {
232
- process . exit ( 0 ) ;
244
+ if ( signal ) {
245
+ // re-raise the signal to exit the process
246
+ printDebugStdout ( 'Re-raising signal' , signal ) ;
247
+ process . kill ( process . pid , signal ) ;
233
248
}
234
249
} ) ;
235
250
}
0 commit comments