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

fix(state): serialize concurrent updateState/saveState with file lock and atomic write

Open hsms4710-pixel opened this issue 4 months ago • 0 comments

Summary

The per-workspace job index (state.json) was persisted via an unguarded read-modify-write (loadState → mutate → saveState) with no lock, no atomic rename, and no compare-and-swap. When two companion invocations registered or updated jobs on the same workspace root concurrently, the last writer won and every other writer's job entry was silently dropped from the index. Worse, saveState treated "job missing from the retained list" as "job pruned" and deleted the losing jobs' .json payload and .log files — so the lost work wasn't just unindexed, its artifacts were destroyed.

Changes

  • File locking (withStateLock): O_EXCL lock-file creation with PID-based stale-lock detection (reclaims locks from crashed processes) and a 10 s timeout. Serializes concurrent updateState / saveState calls across processes.
  • Atomic updateState: the entire loadState → mutate → saveState cycle now runs inside the lock, so each caller sees the latest committed state instead of a stale snapshot.
  • saveState split: saveStateLocked (internal, caller holds lock) + saveState (exported, acquires lock automatically).
  • Atomic write: state.json is written to a temp file then fs.renameSync'd, so concurrent readers never see a partially-written file.
  • session-lifecycle-hook.mjs: migrated cleanupSessionJobs from the stale loadState + saveState pattern to updateState, closing the same race class for the SessionEnd cleanup path.

Reproduction

Verified the bug is reproducible on main before the fix: 5 concurrent upsertJob calls (via real child processes) left only 1 job in the index — the other 4 were silently dropped and their .json files deleted.

After the fix: all 5 jobs and their artifacts are preserved.

Testing

node --test tests/state.test.mjs tests/state-concurrency.test.mjs → 8/8 pass (3 existing + 5 new):

  1. Concurrent upsertJob from 5 child processes — all jobs and .json files preserved
  2. saveState with stale snapshot — no temp files remain (atomic write)
  3. Stale lock from dead PID — automatically reclaimed
  4. Atomic write — state.json is valid JSON, no temp files left
  5. Prune behavior — still correctly caps at MAX_JOBS (50) with locking enabled

Existing state.test.mjs tests (3) pass unchanged — no regression.

Relationship to #439

#439 ships "safe, self-contained" concurrency hardening for the same file (state.mjs): it prevents saveState from deleting active (queued/running) jobs' files and adds atomic writes. Its description explicitly defers the state-transaction lock:

"It does not attempt the larger refactor the pattern calls for (a state-transaction lock and a broker-acquisition lease) — that's a design change better done as its own PR."

This PR implements that deferred state-transaction lock. The two approaches are complementary: #439 prevents artifact deletion as a mitigation, while this PR prevents the stale-snapshot race at its root. If #439 merges first, this PR will need a rebase (both touch saveState).

Closes #458

hsms4710-pixel avatar Jul 09 '26 09:07 hsms4710-pixel