-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Stop orphaned Codex companion brokers #490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ckfchampion
wants to merge
1
commit into
openai:main
Choose a base branch
from
ckfchampion:fix/orphaned-brokers-upstream
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| # Changelog | ||
|
|
||
| ## Unreleased | ||
|
|
||
| - Stop an orphaned shared Codex app-server broker after 15 minutes with no connected clients. Active foreground and background jobs keep their broker connection open, and the timeout can be configured with `CODEX_COMPANION_BROKER_IDLE_TIMEOUT_MS` (`0` disables the safety timer). | ||
| - Close the broker listener before asynchronous child cleanup and safely reject reconnects already queued during shutdown. | ||
|
|
||
| ## 1.0.0 | ||
|
|
||
| - Initial version of the Codex plugin for Claude Code |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import fs from "node:fs"; | ||
| import net from "node:net"; | ||
| import path from "node:path"; | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { once } from "node:events"; | ||
| import { spawn } from "node:child_process"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| import { buildEnv, installFakeCodex } from "./fake-codex-fixture.mjs"; | ||
| import { makeTempDir } from "./helpers.mjs"; | ||
| import { sendBrokerShutdown, waitForBrokerEndpoint } from "../plugins/codex/scripts/lib/broker-lifecycle.mjs"; | ||
|
|
||
| const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); | ||
| const BROKER_SCRIPT = path.join(ROOT, "plugins", "codex", "scripts", "app-server-broker.mjs"); | ||
| const IDLE_TIMEOUT_ENV = "CODEX_COMPANION_BROKER_IDLE_TIMEOUT_MS"; | ||
|
|
||
| function spawnTestBroker({ idleTimeoutMs }) { | ||
| const workspace = makeTempDir(); | ||
| const binDir = makeTempDir(); | ||
| const sessionDir = makeTempDir("codex-plugin-broker-"); | ||
| const socketPath = path.join(sessionDir, "broker.sock"); | ||
| const endpoint = `unix:${socketPath}`; | ||
| const pidFile = path.join(sessionDir, "broker.pid"); | ||
| installFakeCodex(binDir); | ||
|
|
||
| const child = spawn( | ||
| process.execPath, | ||
| [BROKER_SCRIPT, "serve", "--endpoint", endpoint, "--cwd", workspace, "--pid-file", pidFile], | ||
| { | ||
| cwd: workspace, | ||
| env: { | ||
| ...buildEnv(binDir), | ||
| [IDLE_TIMEOUT_ENV]: String(idleTimeoutMs) | ||
| }, | ||
| stdio: ["ignore", "pipe", "pipe"] | ||
| } | ||
| ); | ||
|
|
||
| return { child, endpoint, pidFile, socketPath }; | ||
| } | ||
|
|
||
| async function waitForExit(child, timeoutMs = 3000) { | ||
| if (child.exitCode != null || child.signalCode != null) { | ||
| return; | ||
| } | ||
| await Promise.race([ | ||
| once(child, "exit"), | ||
| new Promise((_, reject) => setTimeout(() => reject(new Error("Timed out waiting for broker to exit.")), timeoutMs)) | ||
| ]); | ||
| } | ||
|
|
||
| function terminateBroker(child) { | ||
| if (child.exitCode == null && child.signalCode == null) { | ||
| child.kill("SIGTERM"); | ||
| } | ||
| } | ||
|
|
||
| test("broker exits and removes runtime files after its last client stays disconnected", async (t) => { | ||
| const broker = spawnTestBroker({ idleTimeoutMs: 150 }); | ||
| t.after(() => terminateBroker(broker.child)); | ||
|
|
||
| assert.equal(await waitForBrokerEndpoint(broker.endpoint), true); | ||
| await waitForExit(broker.child); | ||
|
|
||
| assert.equal(broker.child.exitCode, 0); | ||
| assert.equal(fs.existsSync(broker.socketPath), false); | ||
| assert.equal(fs.existsSync(broker.pidFile), false); | ||
| }); | ||
|
|
||
| test("broker idle shutdown waits until a connected client disconnects", async (t) => { | ||
| const broker = spawnTestBroker({ idleTimeoutMs: 150 }); | ||
| t.after(() => terminateBroker(broker.child)); | ||
|
|
||
| assert.equal(await waitForBrokerEndpoint(broker.endpoint), true); | ||
| const socket = net.createConnection({ path: broker.socketPath }); | ||
| await once(socket, "connect"); | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 350)); | ||
| assert.equal(broker.child.exitCode, null); | ||
| assert.equal(broker.child.signalCode, null); | ||
|
|
||
| socket.end(); | ||
| await once(socket, "close"); | ||
| await waitForExit(broker.child); | ||
| assert.equal(broker.child.exitCode, 0); | ||
| }); | ||
|
|
||
| test("broker idle timeout can be disabled", async (t) => { | ||
| const broker = spawnTestBroker({ idleTimeoutMs: 0 }); | ||
| t.after(() => terminateBroker(broker.child)); | ||
|
|
||
| assert.equal(await waitForBrokerEndpoint(broker.endpoint), true); | ||
| await new Promise((resolve) => setTimeout(resolve, 350)); | ||
| assert.equal(broker.child.exitCode, null); | ||
| assert.equal(broker.child.signalCode, null); | ||
|
|
||
| terminateBroker(broker.child); | ||
| await waitForExit(broker.child); | ||
| }); | ||
|
|
||
| test("broker shuts down cleanly while new clients race to connect", async (t) => { | ||
| const broker = spawnTestBroker({ idleTimeoutMs: 0 }); | ||
| t.after(() => terminateBroker(broker.child)); | ||
|
|
||
| assert.equal(await waitForBrokerEndpoint(broker.endpoint), true); | ||
| const reconnects = Array.from( | ||
| { length: 50 }, | ||
| () => | ||
| new Promise((resolve) => { | ||
| const socket = net.createConnection({ path: broker.socketPath }); | ||
| const timer = setTimeout(() => socket.destroy(), 1000); | ||
| const finish = () => { | ||
| clearTimeout(timer); | ||
| resolve(); | ||
| }; | ||
| socket.on("connect", () => socket.end()); | ||
| socket.on("error", finish); | ||
| socket.on("close", finish); | ||
| }) | ||
| ); | ||
|
|
||
| await Promise.all([sendBrokerShutdown(broker.endpoint), ...reconnects]); | ||
| await waitForExit(broker.child); | ||
| assert.equal(broker.child.exitCode, 0); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the idle-timeout path, the broker exits after removing only the socket/pid runtime files, leaving the saved broker session in
broker.json. After the normal idle timeout,loadBrokerSession(cwd)still points at the deleted socket;/codex:setupthen callsgetCodexAuthStatus()withreuseExistingBroker: true, attempts that dead endpoint, and reports the workspace as not ready withconnect ENOENTinstead of starting or reporting a fresh direct runtime. Clear the broker session when this self-shutdown path runs, or make the setup/status path validate and discard stale sessions.Useful? React with 👍 / 👎.