Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/codex/scripts/lib/app-server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
6 changes: 4 additions & 2 deletions plugins/codex/scripts/lib/broker-lifecycle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -112,6 +113,7 @@ async function isBrokerEndpointReady(endpoint) {

export async function ensureBrokerSession(cwd, options = {}) {
const existing = loadBrokerSession(cwd);
const killProcess = options.killProcess ?? terminateProcessTree;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Verify stale broker PIDs before terminating them

When a broker exits without normal session-end cleanup and the OS later reuses the PID retained in broker.json, the failed endpoint check reaches this default and terminateProcessTree kills that PID without confirming it still belongs to the broker. On Windows this can terminate an unrelated process tree, while on Unix a reused process-group ID can terminate an unrelated group. Preserve and verify process identity/start time before killing a persisted PID, or limit unconditional termination to the newly spawned child handle.

Useful? React with 👍 / 👎.

if (existing && (await isBrokerEndpointReady(existing.endpoint))) {
return existing;
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -154,7 +156,7 @@ export async function ensureBrokerSession(cwd, options = {}) {
logFile,
sessionDir,
pid: child.pid ?? null,
killProcess: options.killProcess ?? null
killProcess
});
return null;
}
Expand Down