Skip to content

Commit 478be42

Browse files
axisrowclaude
andcommitted
fix(broker): only tree-kill the broker pid when its endpoint was live
Hardening follow-up to the respawn-path kill: only pass killProcess when we confirmed the existing broker's endpoint was ready (so the pid still belongs to our broker). For a stale session whose endpoint is not ready, the pid likely points at a dead broker whose pid the OS may have reused for an unrelated process — in that case just drop the socket/session files and let any survivor exit on its own. This narrows the PID-reuse surface on the respawn path while preserving the orphaned-process fix (the live-broker respawn case still tree-kills). Co-Authored-By: Claude <[email protected]>
1 parent 8458741 commit 478be42

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

‎plugins/codex/scripts/lib/broker-lifecycle.mjs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ async function isBrokerEndpointReady(endpoint) {
139139

140140
export async function ensureBrokerSession(cwd, options = {}) {
141141
const existing = loadBrokerSession(cwd);
142-
if (existing && (await isBrokerEndpointReady(existing.endpoint))) {
142+
const existingReady = existing && (await isBrokerEndpointReady(existing.endpoint));
143+
if (existing && existingReady) {
143144
// Reuse the warm broker only when it was spawned with the same model/effort
144145
// override the caller is requesting now. model/effort are baked in at spawn
145146
// (they reach `codex app-server` via `-c` argv), so a differing override
@@ -165,13 +166,19 @@ export async function ensureBrokerSession(cwd, options = {}) {
165166
// Broker may already be gone; the tree-kill below is the fallback.
166167
}
167168
}
169+
// Only tree-kill by pid when we confirmed the broker endpoint was live, so
170+
// the pid still belongs to our broker. A stale session whose endpoint is
171+
// not ready likely points at a dead broker whose pid the OS may have
172+
// reused for an unrelated process — in that case just drop the files and
173+
// let any survivor exit on its own.
174+
const killProcess = existingReady ? (options.killProcess ?? terminateProcessTree) : (options.killProcess ?? null);
168175
teardownBrokerSession({
169176
endpoint: existing.endpoint ?? null,
170177
pidFile: existing.pidFile ?? null,
171178
logFile: existing.logFile ?? null,
172179
sessionDir: existing.sessionDir ?? null,
173180
pid: existing.pid ?? null,
174-
killProcess: options.killProcess ?? terminateProcessTree
181+
killProcess
175182
});
176183
clearBrokerSession(cwd);
177184
}

0 commit comments

Comments
 (0)