-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix test broker leaks, state races, and signal-masked command failures #541
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
apedroheringer
wants to merge
1
commit into
openai:main
Choose a base branch
from
apedroheringer:fix/broker-teardown-state-locking
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
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,88 @@ | ||
| import fs from "node:fs"; | ||
|
|
||
| const DEFAULT_TIMEOUT_MS = 5000; | ||
| const DEFAULT_STALE_MS = 30000; | ||
| const RETRY_DELAY_MS = 25; | ||
|
|
||
| function sleepSync(ms) { | ||
| Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); | ||
| } | ||
|
|
||
| function sleep(ms) { | ||
| return new Promise((resolve) => setTimeout(resolve, ms)); | ||
| } | ||
|
|
||
| function tryAcquire(lockDir) { | ||
| try { | ||
| fs.mkdirSync(lockDir); | ||
| return true; | ||
| } catch (error) { | ||
| if (error?.code === "EEXIST") { | ||
| return false; | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| function reclaimIfStale(lockDir, staleMs) { | ||
| try { | ||
| const age = Date.now() - fs.statSync(lockDir).mtimeMs; | ||
| if (age > staleMs) { | ||
| fs.rmdirSync(lockDir); | ||
| } | ||
| } catch { | ||
| // Lock released (or reclaimed by someone else) between stat and rmdir. | ||
| } | ||
| } | ||
|
|
||
| export function acquireLockSync(lockDir, options = {}) { | ||
| const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS; | ||
| const staleMs = options.staleMs ?? DEFAULT_STALE_MS; | ||
| const deadline = Date.now() + timeoutMs; | ||
| while (!tryAcquire(lockDir)) { | ||
| reclaimIfStale(lockDir, staleMs); | ||
| if (Date.now() >= deadline) { | ||
| throw new Error(`Timed out waiting for lock: ${lockDir}`); | ||
| } | ||
| sleepSync(RETRY_DELAY_MS); | ||
| } | ||
| } | ||
|
|
||
| export async function acquireLock(lockDir, options = {}) { | ||
| const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS; | ||
| const staleMs = options.staleMs ?? DEFAULT_STALE_MS; | ||
| const deadline = Date.now() + timeoutMs; | ||
| while (!tryAcquire(lockDir)) { | ||
| reclaimIfStale(lockDir, staleMs); | ||
| if (Date.now() >= deadline) { | ||
| throw new Error(`Timed out waiting for lock: ${lockDir}`); | ||
| } | ||
| await sleep(RETRY_DELAY_MS); | ||
| } | ||
| } | ||
|
|
||
| export function releaseLock(lockDir) { | ||
| try { | ||
| fs.rmdirSync(lockDir); | ||
| } catch { | ||
| // Already released or reclaimed as stale; nothing left to do. | ||
| } | ||
| } | ||
|
|
||
| export function withLockSync(lockDir, fn, options = {}) { | ||
| acquireLockSync(lockDir, options); | ||
| try { | ||
| return fn(); | ||
| } finally { | ||
| releaseLock(lockDir); | ||
| } | ||
| } | ||
|
|
||
| export async function withLock(lockDir, fn, options = {}) { | ||
| await acquireLock(lockDir, options); | ||
| try { | ||
| return await fn(); | ||
| } finally { | ||
| releaseLock(lockDir); | ||
| } | ||
| } |
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
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,39 @@ | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
|
|
||
| import { buildEnv, installFakeCodex } from "./fake-codex-fixture.mjs"; | ||
| import { makeTempDir } from "./helpers.mjs"; | ||
| import { | ||
| ensureBrokerSession, | ||
| loadBrokerSession, | ||
| sendBrokerShutdown | ||
| } from "../plugins/codex/scripts/lib/broker-lifecycle.mjs"; | ||
|
|
||
| test("concurrent ensureBrokerSession calls share a single broker", async () => { | ||
| const workspace = makeTempDir(); | ||
| const binDir = makeTempDir(); | ||
| installFakeCodex(binDir); | ||
| const env = buildEnv(binDir); | ||
|
|
||
| let first = null; | ||
| try { | ||
| const [left, right] = await Promise.all([ | ||
| ensureBrokerSession(workspace, { env }), | ||
| ensureBrokerSession(workspace, { env }) | ||
| ]); | ||
| first = left ?? right; | ||
|
|
||
| assert.ok(left, "first ensureBrokerSession returned no session"); | ||
| assert.ok(right, "second ensureBrokerSession returned no session"); | ||
| assert.equal(left.endpoint, right.endpoint); | ||
| assert.equal(left.pid, right.pid); | ||
|
|
||
| const persisted = loadBrokerSession(workspace); | ||
| assert.ok(persisted); | ||
| assert.equal(persisted.endpoint, left.endpoint); | ||
| } finally { | ||
| if (first?.endpoint) { | ||
| await sendBrokerShutdown(first.endpoint); | ||
| } | ||
| } | ||
| }); |
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
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
Oops, something went wrong.
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 environments where the detached broker has exited but remains as a zombie under PID 1,
process.kill(pid, 0)still succeeds, so this new teardown path reports a leak that cannot be terminated. I hit this withnpm test: all tests passed, then this hook failed withLeaked broker process ..., andpsshowed that PID as[MainThread] <defunct>. That makes the suite fail nondeterministically depending on how quickly the container init reaps orphaned detached brokers; the leak check needs to distinguish defunct processes from live brokers before throwing.Useful? React with 👍 / 👎.