fix(runtime): clean up stale brokers and lock state updates
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
terminateProcessTreeand pass that default through stale-session and failed-start cleanup. - Add a small cross-process state lock around the full state transaction so concurrent
upsertJobcalls 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.mjsnode --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.mjsgit diff --cached --check
Note: local npm run build reaches tsc but fails on generated app-server type drift unrelated to this change (requestAttestation / experimentalRawEvents).
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.
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.
Hitting the same broker-leak symptoms on macOS (Claude Code with
codex@openai-codexv1.0.4):app-server-broker.mjsplusnode codex app-serverand the Rustcodex app-serversurvive pastSessionEnd, 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.