Skip to content

Commit 25ada4d

Browse files
committed
Exit the process only when there are no other listeners
1 parent 1960aae commit 25ada4d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

packages/agents-core/src/tracing/provider.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
210230
let GLOBAL_TRACE_PROVIDER: TraceProvider | undefined = undefined;
211231
export function getGlobalTraceProvider(): TraceProvider {
212232
if (!GLOBAL_TRACE_PROVIDER) {

0 commit comments

Comments
 (0)