@@ -188,16 +188,28 @@ export class TraceProvider {
188188 // Handle CTRL+C (SIGINT)
189189 process . on ( 'SIGINT' , async ( ) => {
190190 await cleanup ( ) ;
191+ if ( ! hasOtherListenersForSignals ( 'SIGINT' ) ) {
192+ // Only when there are no other listeners, exit the process on this SDK side
193+ process . exit ( 130 ) ;
194+ }
191195 } ) ;
192196
193197 // Handle termination (SIGTERM)
194198 process . on ( 'SIGTERM' , async ( ) => {
195199 await cleanup ( ) ;
200+ if ( ! hasOtherListenersForSignals ( 'SIGTERM' ) ) {
201+ // Only when there are no other listeners, exit the process on this SDK side
202+ process . exit ( 0 ) ;
203+ }
196204 } ) ;
197205
198206 process . on ( 'unhandledRejection' , async ( reason , promise ) => {
199207 logger . error ( 'Unhandled rejection' , reason , promise ) ;
200208 await cleanup ( ) ;
209+ if ( ! hasOtherListenersForEvents ( 'unhandledRejection' ) ) {
210+ // Only when there are no other listeners, exit the process on this SDK side
211+ process . exit ( 1 ) ;
212+ }
201213 } ) ;
202214 }
203215 }
@@ -207,6 +219,14 @@ export class TraceProvider {
207219 }
208220}
209221
222+ function hasOtherListenersForSignals ( event : 'SIGINT' | 'SIGTERM' ) : boolean {
223+ return process . listeners ( event ) . length > 1 ;
224+ }
225+
226+ function hasOtherListenersForEvents ( event : 'unhandledRejection' ) : boolean {
227+ return process . listeners ( event ) . length > 1 ;
228+ }
229+
210230let GLOBAL_TRACE_PROVIDER : TraceProvider | undefined = undefined ;
211231export function getGlobalTraceProvider ( ) : TraceProvider {
212232 if ( ! GLOBAL_TRACE_PROVIDER ) {
0 commit comments