Skip to content

fix: terminate broker process when ensureBrokerSession tears down (fixes #753) - #762

Open
Soumya95 wants to merge 1 commit into
openai:mainfrom
Soumya95:fix/kill-broker-on-teardown
Open

Soumya95 wants to merge 1 commit into
openai:mainfrom
Soumya95:fix/kill-broker-on-teardown

Conversation

@Soumya95

Copy link
Copy Markdown

Summary

Fixes #753: the only production caller of ensureBrokerSession() passed no killProcess, so teardownBrokerSession() unlinked the pid file, log file, and session dir without terminating anything. The broker process survived, and deleting pidFile destroyed the last handle anyone had on it — a permanently unreachable orphan.

Changes

  • plugins/codex/scripts/lib/app-server.mjs — pass killProcess: terminateProcessTree at the only production call site (CodexAppServerClient.connect), matching what session-lifecycle-hook.mjs already does.
  • plugins/codex/scripts/lib/broker-lifecycle.mjs — additionally default killProcess to terminateProcessTree inside ensureBrokerSession() so no future caller can omit it. This is the "better still" option from the issue: teardownBrokerSession() already ignores a missing process, so the default is safe.

This covers both teardown branches that previously leaked:

  1. The existing-but-unresponsive broker branch (150ms endpoint check).
  2. The freshly-spawned-but-slow broker branch (2s readiness timeout) — reproduce the timeout and the new child is now terminated instead of stranded.

Verification

  • node --test tests/process.test.mjs tests/commands.test.mjs — 10/10 pass.
  • No circular import; broker-lifecycle.mjs now imports terminateProcessTree from ./process.mjs, which has no reverse dependency.

@Soumya95
Soumya95 requested a review from a team September 14, 2026 07:21

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 599b7cb45f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".


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 👍 / 👎.

@Soumya95
Soumya95 force-pushed the fix/kill-broker-on-teardown branch from ff71e37 to 599b7cb Compare September 14, 2026 07:36

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ff71e37813

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

status: result.status,
failureMessage: result.error?.message ?? result.stderr
});
const failureMessage = result.error?.message ?? result.stderr ?? parsed.parseError ?? "";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Treat empty stderr as absent before using the parse error

When an adversarial review returns malformed or no structured output without an app-server error notification, cleanCodexStderr() supplies ""; because nullish coalescing treats that as present, parsed.parseError is never selected. The failed job consequently persists a null errorMessage, and its summary falls back to the raw output's first line—often just {—instead of the actionable parse failure. Use the first non-empty diagnostic rather than the first non-nullish value.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ensureBrokerSession() deletes a live broker's state without killing it — the only production caller passes no killProcess

1 participant