Skip to content

Commit 5198f54

Browse files
authored
start listening to close events before running the job (#1191)
1 parent 3a0cf84 commit 5198f54

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

agents/src/ipc/job_proc_lazy_main.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ const startJob = (
126126
}, 10000);
127127

128128
try {
129+
const closePromise = once(closeEvent, 'close').then((close) => {
130+
logger.debug('shutting down');
131+
shutdown = true;
132+
safeSend({ case: 'exiting', value: { reason: close[1] } });
133+
});
134+
129135
// Run the job function within the AsyncLocalStorage context
130136
await runWithJobContextAsync(ctx, async () => {
131137
const { tracer, traceTypes } = await import('../telemetry/index.js');
@@ -138,15 +144,15 @@ const startJob = (
138144
},
139145
{ name: 'job_entrypoint' },
140146
);
141-
}).finally(() => {
142-
clearTimeout(unconnectedTimeout);
143-
});
144-
145-
await once(closeEvent, 'close').then((close) => {
146-
logger.debug('shutting down');
147-
shutdown = true;
148-
safeSend({ case: 'exiting', value: { reason: close[1] } });
149-
});
147+
})
148+
.then(async () => {
149+
if (!shutdown) {
150+
await closePromise;
151+
}
152+
})
153+
.finally(async () => {
154+
clearTimeout(unconnectedTimeout);
155+
});
150156
} catch (error) {
151157
logger.error({ error }, 'error in entry function');
152158
shutdown = true;

0 commit comments

Comments
 (0)