Skip to content

Commit be9ec91

Browse files
Fix: close scope on unexpected process exit to prevent resource leaks
When the codex app-server process exits unexpectedly (not via stopSession), the onSuccess and onFailure handlers deleted the session from the map but never closed context.scope. Since the session was removed, stopSession could never be called for it, permanently leaking the scope and its resources. Changed both handlers from Effect.sync to Effect.suspend so they can return a Scope.close effect after performing the sync cleanup work, matching the cleanup behavior in stopSession. Co-authored-by: Julius Marminge <juliusmarminge@users.noreply.github.com>
1 parent 870d694 commit be9ec91

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

apps/server/src/codexAppServerManager.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,9 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
575575
yield* Effect.forkScoped(
576576
Effect.matchCauseEffect(context.child.exitCode, {
577577
onFailure: (cause) =>
578-
Effect.sync(() => {
578+
Effect.suspend(() => {
579579
if (context.stopping) {
580-
return;
580+
return Effect.void;
581581
}
582582

583583
const message = messageFromCodexProcessCause(cause);
@@ -588,11 +588,12 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
588588
});
589589
this.emitErrorEvent(context, "process/error", message);
590590
this.sessions.delete(context.session.sessionId);
591+
return Scope.close(context.scope, Exit.void).pipe(Effect.ignore);
591592
}),
592593
onSuccess: (code) =>
593-
Effect.sync(() => {
594+
Effect.suspend(() => {
594595
if (context.stopping) {
595-
return;
596+
return Effect.void;
596597
}
597598

598599
const message = `codex app-server exited (code=${code}).`;
@@ -603,6 +604,7 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
603604
});
604605
this.emitLifecycleEvent(context, "session/exited", message);
605606
this.sessions.delete(context.session.sessionId);
607+
return Scope.close(context.scope, Exit.void).pipe(Effect.ignore);
606608
}),
607609
}),
608610
);

0 commit comments

Comments
 (0)