From 599b7cb45f66f166bf389bba21eadb8f1368c469 Mon Sep 17 00:00:00 2001 From: Soumya95 Date: Mon, 14 Sep 2026 12:51:33 +0530 Subject: [PATCH] fix: terminate broker process when ensureBrokerSession tears down (#753) --- plugins/codex/scripts/lib/app-server.mjs | 2 +- plugins/codex/scripts/lib/broker-lifecycle.mjs | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/plugins/codex/scripts/lib/app-server.mjs b/plugins/codex/scripts/lib/app-server.mjs index 72b30a764..9bcfb1f7a 100644 --- a/plugins/codex/scripts/lib/app-server.mjs +++ b/plugins/codex/scripts/lib/app-server.mjs @@ -341,7 +341,7 @@ export class CodexAppServerClient { brokerEndpoint = loadBrokerSession(cwd)?.endpoint ?? null; } if (!brokerEndpoint && !options.reuseExistingBroker) { - const brokerSession = await ensureBrokerSession(cwd, { env: options.env }); + const brokerSession = await ensureBrokerSession(cwd, { env: options.env, killProcess: terminateProcessTree }); brokerEndpoint = brokerSession?.endpoint ?? null; } } diff --git a/plugins/codex/scripts/lib/broker-lifecycle.mjs b/plugins/codex/scripts/lib/broker-lifecycle.mjs index ef763819c..591ae8433 100644 --- a/plugins/codex/scripts/lib/broker-lifecycle.mjs +++ b/plugins/codex/scripts/lib/broker-lifecycle.mjs @@ -7,6 +7,7 @@ import { spawn } from "node:child_process"; import { fileURLToPath } from "node:url"; import { createBrokerEndpoint, parseBrokerEndpoint } from "./broker-endpoint.mjs"; import { resolveStateDir } from "./state.mjs"; +import { terminateProcessTree } from "./process.mjs"; export const PID_FILE_ENV = "CODEX_COMPANION_APP_SERVER_PID_FILE"; export const LOG_FILE_ENV = "CODEX_COMPANION_APP_SERVER_LOG_FILE"; @@ -112,6 +113,7 @@ async function isBrokerEndpointReady(endpoint) { export async function ensureBrokerSession(cwd, options = {}) { const existing = loadBrokerSession(cwd); + const killProcess = options.killProcess ?? terminateProcessTree; if (existing && (await isBrokerEndpointReady(existing.endpoint))) { return existing; } @@ -123,7 +125,7 @@ export async function ensureBrokerSession(cwd, options = {}) { logFile: existing.logFile ?? null, sessionDir: existing.sessionDir ?? null, pid: existing.pid ?? null, - killProcess: options.killProcess ?? null + killProcess }); clearBrokerSession(cwd); } @@ -154,7 +156,7 @@ export async function ensureBrokerSession(cwd, options = {}) { logFile, sessionDir, pid: child.pid ?? null, - killProcess: options.killProcess ?? null + killProcess }); return null; }