codex-plugin-cc icon indicating copy to clipboard operation
codex-plugin-cc copied to clipboard

fix(runtime): clean up stale brokers and lock state updates

Open 60ke opened this issue 6 months ago • 3 comments

Problem

Stale broker sessions could remove their socket and metadata without terminating the recorded broker process when no test-only killProcess override was supplied. State updates also used an unlocked load/mutate/save sequence, so concurrent workers could overwrite each other and drop job records.

Changes

  • Default broker teardown to terminateProcessTree and pass that default through stale-session and failed-start cleanup.
  • Add a small cross-process state lock around the full state transaction so concurrent upsertJob calls preserve each other.
  • Add regression coverage for stale broker termination and concurrent job state writes.

Testing

  • node --test tests/broker-lifecycle.test.mjs tests/state.test.mjs
  • node --test tests/broker-endpoint.test.mjs tests/bump-version.test.mjs tests/commands.test.mjs tests/git.test.mjs tests/process.test.mjs tests/render.test.mjs
  • git diff --cached --check

Note: local npm run build reaches tsc but fails on generated app-server type drift unrelated to this change (requestAttestation / experimentalRawEvents).

60ke avatar May 22 '26 09:05 60ke

Good catch. The timeout needs to be longer than the stale-lock recovery window, otherwise a fresh orphaned lock can make every writer fail before recovery is allowed. I updated the PR so STATE_LOCK_TIMEOUT_MS is derived from STATE_LOCK_STALE_MS + 5000, then reran node --test tests/broker-lifecycle.test.mjs tests/state.test.mjs and git diff --check.

60ke avatar May 22 '26 09:05 60ke

Hitting the same broker-leak symptoms on macOS (Claude Code with codex@openai-codex v1.0.4): app-server-broker.mjs plus node codex app-server and the Rust codex app-server survive past SessionEnd, reparent to launchd (PPID 1), and accumulate across sessions.

Pelton avatar May 22 '26 22:05 Pelton

Hitting the same broker-leak symptoms on macOS (Claude Code with codex@openai-codex v1.0.4): app-server-broker.mjs plus node codex app-server and the Rust codex app-server survive past SessionEnd, reparent to launchd (PPID 1), and accumulate across sessions.

Thanks for the macOS details. The latest patch keeps the SessionEnd/stale cleanup behavior aimed at the broker leak, but now validates the persisted pid before signalling: the live command has to match this broker for the same endpoint/pid file. That should still reap the leaked broker process group while avoiding PID-reuse kills of unrelated workloads.

60ke avatar May 23 '26 04:05 60ke