Race condition: .claude.json corrupted by concurrent writes from multiple sessions
Bug Description
.claude.json gets repeatedly corrupted when multiple Claude Code sessions run concurrently (or are rapidly restarted). The file is truncated mid-write, producing JSON Parse error: Unexpected EOF.
Evidence
Over a ~40 minute window (Feb 26, 14:54–15:35), 165 corrupted backup files were generated in ~/.claude/backups/. The corruption cascades — each recovery attempt gets corrupted again by a competing process, producing progressively smaller truncated files:
-rw-r--r-- 11873 Feb 26 14:15 .claude.json.corrupted.1772095551270 # full config
-rw-r--r-- 469 Feb 26 14:16 .claude.json.corrupted.1772095565563 # partially recovered
-rw-r--r-- 157 Feb 26 14:16 .claude.json.corrupted.1772095562170 # barely anything
-rw-r--r-- 77 Feb 26 14:16 .claude.json.corrupted.1772095562117 # just opening brace
Files as small as 77 bytes (just {"numStartups":1,"installMethod":"native","autoUpdates":false}) show the file being read mid-write by another process.
Impact
- Token waste: Each corrupted restart triggers re-initialization (feature flag fetches, re-auth, session setup), burning tokens on repeated failed startups
- Config loss: The final state was a fresh config (
numStartups: 1) replacing a real config (numStartups: 72) with all project history, theme settings, and tool usage data - User had to manually restore from
.claude.json.backup
Root Cause
The file write to .claude.json is not atomic. When multiple processes write simultaneously, one truncates the file while another is mid-read or mid-write, producing invalid JSON.
Suggested Fix
Use atomic file writes: write to a temporary file (e.g., .claude.json.tmp), then rename() it to .claude.json. On most filesystems, rename() is atomic and prevents partial reads. Additionally, consider using file locking (flock / platform equivalent) to serialize access.
Environment
- Windows 11 (via Git Bash)
- Claude Code (native install)
- Trigger: multiple sessions or rapid restarts from same directory
Reproduction
- Open 2+ Claude Code sessions in the same directory simultaneously
- Use both actively (so both write config updates)
- Observe corruption errors on next startup
This issue has 10 comments on GitHub. Read the full discussion on GitHub ↗