Skip to content

fix(session-tracking): lock shared sessions.json read-modify-write - #506

Open
codeaholicguy wants to merge 2 commits into
mainfrom
cursor/fix-sessions-json-rmw-lock-48a7
Open

codeaholicguy wants to merge 2 commits into
mainfrom
cursor/fix-sessions-json-rmw-lock-48a7

Conversation

@codeaholicguy

Copy link
Copy Markdown
Owner

Closes #494

Root cause

TLC on feature-tla-analysis violated Inv_NoLostSessionsJsonEntry at depth 7. The Pi extension and the Codex SessionStart hook both did unlocked read-modify-write on one sessions.json. Two processes in the same window could drop each other's pid row. Codex was worse: in-place writeFileSync plus treating parse errors as {} could wipe every other live pid when another hook was mid-write.

Fix

Pi logic lives in packages/pi-session-tracker/extensions/session-registry.ts and drives the extension handlers. Codex uses the same rules inlined in hooks/codex/codex-session-mapping.cjs. packages/cli/assets/codex/codex-session-mapping.cjs is kept byte-identical to that hooks file (copy on change; we can add a CI diff later if we want).

Both paths now:

  • Serialize updates with an O_EXCL lockfile (sessions.json.lock, 30s stale reclaim, same idea as devkitd autostart).
  • Write via temp file + rename (atomic replace).
  • Create the registry directory before taking the lock.
  • Under the lock, if existing JSON is unreadable: rename to sessions.json.corrupt-<timestamp>, console.warn, continue from {}.
  • If the lock is not acquired within 10s: warn once, skip this update only, never throw. Codex still exits 0. Pi handlers ignore the boolean.

No overlap with open PR #489 (rust/crates/devkitd/src/durable.rs) or #481 (server.rs agent lookup / unregister).

Test plan

nvm use 24 && npm install && npm run build
cd packages/pi-session-tracker && npm test
cd packages/cli && npx vitest run tests/assets/codex/codex-session-mapping-registry.test.ts
cd rust && cargo test --workspace && cargo clippy --workspace --all-targets -- -D warnings && cargo fmt --check

Regression: concurrent Pi workers and parallel Codex hook subprocesses; corrupt JSON quarantine plus fresh write; lock held so timeout skips without writing (AI_DEVKIT_SESSION_REGISTRY_LOCK_WAIT_MS=100 in tests).

Risks

Under heavy contention a single start or shutdown may wait up to 10s before giving up that one update. A crashed lock holder is reclaimed after 30s. Quarantine drops unreadable bytes on disk (backed up as sessions.json.corrupt-*).

Behavior changes

  • Concurrent Pi and Codex writers no longer lose each other's pid entries on sessions.json.
  • Codex mapping writes are atomic (temp + rename), matching Pi.
  • Corrupt sessions.json is quarantined and replaced with a fresh map that includes the current pid, instead of staying broken forever.
  • On lock timeout, one mapping update is skipped with a warning; the Codex hook still exits 0 and does not block session start.

Skills / friction

Skills: AGENTS.md test layout, TDD (concurrent and corrupt regressions), humanizer on this description.

Friction: pre-commit oxfmt; hook subprocess tests need the correct relative path to the CLI asset; first draft forgot mkdir before lock create on empty registry dirs; cloud VM git push over https failed with no credentials.

Open in Web Open in Cursor 

…ocesses

Serialize Pi and Codex writers with an O_EXCL lockfile (stale reclaim),
use temp+rename for Codex, and skip writes when JSON parse fails so
concurrent hooks cannot clobber live pid entries.
…timeout

Quarantine unreadable registry files under the lock, warn, and continue
from an empty map. On lock wait timeout, log, skip one update, and never
throw (Codex exits 0). Keep cli Codex asset identical to hooks/codex.

Co-authored-by: Hoang Nguyen <codeaholicguy@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(session-tracking): unlocked read-modify-write on shared sessions.json loses live PID entries (pi tracker + codex hook) (Inv_NoLostSessionsJsonEntry)

1 participant