-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(broker): self-terminate on idle to reap orphaned shared brokers (#450) #457
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
HoneyTyagii
wants to merge
1
commit into
openai:main
Choose a base branch
from
HoneyTyagii:fix/broker-idle-timeout
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.
+202
−3
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
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,136 @@ | ||
| import fs from "node:fs"; | ||
| import net from "node:net"; | ||
|
Comment on lines
+1
to
+2
|
||
| import path from "node:path"; | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| 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 { createBrokerEndpoint, parseBrokerEndpoint } from "../plugins/codex/scripts/lib/broker-endpoint.mjs"; | ||
| import { 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"); | ||
|
|
||
| function spawnBroker({ cwd, endpoint, env, idleTimeoutMs }) { | ||
| const args = [BROKER_SCRIPT, "serve", "--endpoint", endpoint, "--cwd", cwd]; | ||
| if (idleTimeoutMs !== undefined) { | ||
| args.push("--idle-timeout", String(idleTimeoutMs)); | ||
| } | ||
| return spawn(process.execPath, args, { | ||
| cwd, | ||
| env, | ||
| stdio: ["ignore", "pipe", "pipe"] | ||
| }); | ||
| } | ||
|
|
||
| function waitForExit(child, { timeoutMs = 5000 } = {}) { | ||
| return new Promise((resolve, reject) => { | ||
| if (child.exitCode !== null || child.signalCode !== null) { | ||
| resolve({ code: child.exitCode, signal: child.signalCode }); | ||
| return; | ||
| } | ||
| const timer = setTimeout(() => { | ||
| child.removeListener("exit", onExit); | ||
| reject(new Error("Timed out waiting for broker process to exit.")); | ||
| }, timeoutMs); | ||
| function onExit(code, signal) { | ||
| clearTimeout(timer); | ||
| resolve({ code, signal }); | ||
| } | ||
| child.once("exit", onExit); | ||
| }); | ||
| } | ||
|
|
||
| function connectClient(endpoint) { | ||
| const target = parseBrokerEndpoint(endpoint); | ||
| return new Promise((resolve, reject) => { | ||
| const socket = net.createConnection({ path: target.path }); | ||
| socket.once("connect", () => resolve(socket)); | ||
| socket.once("error", reject); | ||
| }); | ||
| } | ||
|
|
||
| function delay(ms) { | ||
| return new Promise((resolve) => setTimeout(resolve, ms)); | ||
| } | ||
|
|
||
| test("broker self-terminates after the idle timeout when no client is connected", async () => { | ||
| const binDir = makeTempDir(); | ||
| installFakeCodex(binDir); | ||
| const sessionDir = makeTempDir("cxc-"); | ||
| const endpoint = createBrokerEndpoint(sessionDir); | ||
| const child = spawnBroker({ cwd: sessionDir, endpoint, env: buildEnv(binDir), idleTimeoutMs: 300 }); | ||
|
|
||
| try { | ||
| const ready = await waitForBrokerEndpoint(endpoint, 3000); | ||
| assert.equal(ready, true, "broker should accept connections before it times out"); | ||
|
|
||
| const start = Date.now(); | ||
| const result = await waitForExit(child, { timeoutMs: 5000 }); | ||
| const elapsed = Date.now() - start; | ||
|
|
||
| assert.equal(result.code, 0, "broker should exit cleanly on idle timeout"); | ||
| assert.ok(elapsed >= 200, `broker exited too early (${elapsed}ms)`); | ||
| } finally { | ||
| if (child.exitCode === null && child.signalCode === null) { | ||
| child.kill(); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test("broker stays alive while a client is connected and exits after it disconnects", async () => { | ||
| const binDir = makeTempDir(); | ||
| installFakeCodex(binDir); | ||
| const sessionDir = makeTempDir("cxc-"); | ||
| const endpoint = createBrokerEndpoint(sessionDir); | ||
| const child = spawnBroker({ cwd: sessionDir, endpoint, env: buildEnv(binDir), idleTimeoutMs: 300 }); | ||
|
|
||
| let socket = null; | ||
| try { | ||
| const ready = await waitForBrokerEndpoint(endpoint, 3000); | ||
| assert.equal(ready, true); | ||
|
|
||
| socket = await connectClient(endpoint); | ||
|
|
||
| // Hold the connection open well past the idle timeout; the broker must not | ||
| // self-terminate while a client is still connected. | ||
| await delay(900); | ||
| assert.equal(child.exitCode, null, "broker must stay alive while a client is connected"); | ||
|
|
||
| socket.end(); | ||
| socket = null; | ||
|
|
||
| const result = await waitForExit(child, { timeoutMs: 5000 }); | ||
| assert.equal(result.code, 0, "broker should exit once the client disconnects and it goes idle"); | ||
| } finally { | ||
| if (socket) { | ||
| socket.destroy(); | ||
| } | ||
| if (child.exitCode === null && child.signalCode === null) { | ||
| child.kill(); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test("broker with the idle timeout disabled keeps running while idle", async () => { | ||
| const binDir = makeTempDir(); | ||
| installFakeCodex(binDir); | ||
| const sessionDir = makeTempDir("cxc-"); | ||
| const endpoint = createBrokerEndpoint(sessionDir); | ||
| const child = spawnBroker({ cwd: sessionDir, endpoint, env: buildEnv(binDir), idleTimeoutMs: 0 }); | ||
|
|
||
| try { | ||
| const ready = await waitForBrokerEndpoint(endpoint, 3000); | ||
| assert.equal(ready, true); | ||
|
|
||
| await delay(700); | ||
| assert.equal(child.exitCode, null, "broker must not self-terminate when the idle timeout is disabled"); | ||
| } finally { | ||
| if (child.exitCode === null && child.signalCode === null) { | ||
| child.kill(); | ||
| } | ||
| } | ||
| }); | ||
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.
When callers set
--idle-timeout -1orCODEX_COMPANION_BROKER_IDLE_TIMEOUT_MS=-1to follow the new<= 0disable contract, this branch replaces the value with the 30-minute default. SincearmIdleTimer()only disables whenidleTimeoutMs <= 0, those callers still get an idle shutdown instead of preserving externally managed lifecycle; return a non-positive value for parsed negatives instead of the default.Useful? React with 👍 / 👎.