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 <[email protected]>
This commit is contained in:
Cursor AgentandJulius Marminge committed 2026-03-06 00:24:24 +00:00
1 parent 870d6943ed
commit be9ec914b2
1 file changed
+6 -4
+6 -4
View File
@@ -575,9 +575,9 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
yield* Effect.forkScoped(
Effect.matchCauseEffect(context.child.exitCode, {
onFailure: (cause) =>
Effect.sync(() => {
Effect.suspend(() => {
if (context.stopping) {
return;
return Effect.void;
}
const message = messageFromCodexProcessCause(cause);
@@ -588,11 +588,12 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
});
this.emitErrorEvent(context, "process/error", message);
this.sessions.delete(context.session.sessionId);
return Scope.close(context.scope, Exit.void).pipe(Effect.ignore);
}),
onSuccess: (code) =>
Effect.sync(() => {
Effect.suspend(() => {
if (context.stopping) {
return;
return Effect.void;
}
const message = `codex app-server exited (code=${code}).`;
@@ -603,6 +604,7 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
});
this.emitLifecycleEvent(context, "session/exited", message);
this.sessions.delete(context.session.sessionId);
return Scope.close(context.scope, Exit.void).pipe(Effect.ignore);
}),
}),
);