Skip to content

Commit 17077d8

Browse files
authored
Fix #175 by removing system.exit calls in the built-in handlers (#184)
1 parent e5e8c8a commit 17077d8

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

.changeset/tame-hotels-hope.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openai/agents-core': patch
3+
---
4+
5+
Fix #175 by removing internal system.exit calls

.github/workflows/test.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,27 @@ on:
88
jobs:
99
test:
1010
runs-on: ubuntu-latest
11+
env:
12+
NODE_OPTIONS: "--max_old_space_size=4096"
1113
strategy:
1214
matrix:
1315
# https://nodejs.org/en/about/previous-releases
1416
node-version: [20, 22, 24]
1517
steps:
1618
- name: Checkout repository
1719
uses: actions/checkout@v4
18-
- name: Setup Node.js
19-
uses: actions/setup-node@v4
20-
with:
21-
node-version: ${{ matrix.node-version }}
2220
- name: Install pnpm
2321
uses: pnpm/action-setup@v4
2422
with:
2523
version: 10.13.1
26-
run_install: true
24+
run_install: false
25+
- name: Setup Node.js
26+
uses: actions/setup-node@v4
27+
with:
28+
node-version: ${{ matrix.node-version }}
29+
cache: 'pnpm'
30+
- name: Install dependencies
31+
run: pnpm install
2732
- name: Build all packages
2833
run: pnpm build
2934
- name: Run linter

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,28 @@ export class TraceProvider {
188188
// Handle CTRL+C (SIGINT)
189189
process.on('SIGINT', async () => {
190190
await cleanup();
191-
process.exit(130);
191+
if (!hasOtherListenersForSignals('SIGINT')) {
192+
// Only when there are no other listeners, exit the process on this SDK side
193+
process.exit(130);
194+
}
192195
});
193196

194197
// Handle termination (SIGTERM)
195198
process.on('SIGTERM', async () => {
196199
await cleanup();
197-
process.exit(0);
200+
if (!hasOtherListenersForSignals('SIGTERM')) {
201+
// Only when there are no other listeners, exit the process on this SDK side
202+
process.exit(0);
203+
}
198204
});
199205

200206
process.on('unhandledRejection', async (reason, promise) => {
201207
logger.error('Unhandled rejection', reason, promise);
202208
await cleanup();
203-
process.exit(1);
209+
if (!hasOtherListenersForEvents('unhandledRejection')) {
210+
// Only when there are no other listeners, exit the process on this SDK side
211+
process.exit(1);
212+
}
204213
});
205214
}
206215
}
@@ -210,6 +219,14 @@ export class TraceProvider {
210219
}
211220
}
212221

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+
213230
let GLOBAL_TRACE_PROVIDER: TraceProvider | undefined = undefined;
214231
export function getGlobalTraceProvider(): TraceProvider {
215232
if (!GLOBAL_TRACE_PROVIDER) {

0 commit comments

Comments
 (0)