claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

claude code corrupts ~/.claude.json on mac while running 5 concurrent sessions

Open qfennessy opened this issue 5 months ago • 11 comments

Bug Description Claude configuration file at /Users/quentin/.claude.json is corrupted: Unexpected end of JSON input

Claude configuration file at /Users/quentin/.claude.json is corrupted The corrupted file has been backed up to: /Users/quentin/.claude.json.corrupted.1751911760442 A backup file exists at: /Users/quentin/.claude.json.backup You can manually restore it by running: cp "/Users/quentin/.claude.json.backup" "/Users/quentin/.claude.json"

Environment Info

  • Platform: darwin
  • Terminal: iTerm.app
  • Version: 1.0.43
  • Feedback ID: b11b25e6-c041-475e-9b26-bcd1cc971646

Errors

[{"error":"Error: Command failed: security find-generic-password -a $USER -w -s \"Claude Code\"\nsecurity: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.\n\n    at genericNodeError (node:internal/errors:984:15)\n    at wrappedFn (node:internal/errors:538:14)\n    at checkExecSyncError (node:child_process:891:11)\n    at execSync (node:child_process:963:15)\n    at ED (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:702:3921)\n    at file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:623:9038\n    at Q (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:526:13327)\n    at hJ (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:623:8184)\n    at S_ (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:623:7265)\n    at NB (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:623:11432)","timestamp":"2025-07-07T14:46:12.617Z"},{"error":"AbortError: The operation was aborted\n    at abortChildProcess (node:child_process:725:27)\n    at EventTarget.onAbortListener (node:child_process:795:7)\n    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:831:20)\n    at EventTarget.dispatchEvent (node:internal/event_target:766:26)\n    at abortSignal (node:internal/abort_controller:370:10)\n    at AbortController.abort (node:internal/abort_controller:392:5)\n    at Timeout._onTimeout (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:2584:7702)\n    at listOnTimeout (node:internal/timers:581:17)\n    at process.processTimers (node:internal/timers:519:7)","timestamp":"2025-07-07T14:46:18.346Z"},{"error":"SyntaxError: Unexpected non-whitespace character after JSON at position 63\n    at JSON.parse (<anonymous>)\n    at file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:618:9952\n    at Q (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:526:13327)\n    at $w2 (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1350:10306)\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)","timestamp":"2025-07-07T14:52:43.088Z"},{"error":"MaxFileReadTokenExceededError: File content (29511 tokens) exceeds maximum allowed tokens (25000). Please use offset and limit parameters to read specific portions of the file, or use the GrepTool to search for specific content.\n    at uz6 (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1428:432)\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n    at async Object.call (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1423:4919)\n    at async hq6 (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1933:11470)\n    at async Jz1 (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1933:8191)\n    at async Gz1 (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1913:775)\n    at async gq6 (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1933:7335)\n    at async fq6 (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1933:6820)\n    at async C$ (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1933:5217)\n    at async C$ (file:///Users/quentin/.nvm/versions/node/v20.19.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:1933:5939)","timestamp":"2025-07-07T15:12:23.684Z"}]

qfennessy avatar Jul 07 '25 18:07 qfennessy

same issue

You can manually restore it by running: cp "/Users/quentin/.claude.json.backup" "/Users/quentin/.claude.json"

not work

terryso avatar Jul 09 '25 01:07 terryso

This popped up for us last week - really bad. Causing a lot of api key creation too for our team is which is less than ideal.

tomnewton avatar Jul 14 '25 16:07 tomnewton

Same. Haven't confirmed the cause, but I've got a stack of 35 corrupted ~/.claude.json config files piled up over 3 weeks.

mintmcqueen avatar Jul 17 '25 14:07 mintmcqueen

The same issue just randomly appeared for me in the last two days on both macOS and Linux. This is particularly infuriating because ~/.claude.json seems to be the only place to store global mcp server configurations where they will stick. They do not work in .mcp.json or settings.json

Related: #1788

noops888 avatar Jul 24 '25 10:07 noops888

Same happened to me yesterday around the same time the update for agents was pushed. IIRC, I ended a session and restarted a few minutes later and had to log back in. I tried to continue my previous session but there weren't any sessions to resume. Noticed all of my MCP servers were missing too so I tried to restore config from backup but it was just a copy of the corrupted one. Now I'm getting random messages it's still corrupted.

sfearl1 avatar Jul 26 '25 06:07 sfearl1

same on ubuntu 24.04 Claude configuration file at /home/{user}/.claude.json is corrupted: Unexpected end of JSON input

I am usnsure of the reason. I don't think there were multiple claude session executing (could have been open but should have been idle)

cp "/home/{user}/.claude.json.backup" "/home/{user}/.claude.json" ro selecting "reset with default settings" does not help

Image

claude doctor also opens the above screen and can't reset it with default configuration. No error messages after selecting that option, the same screen just shows up next time.

config file appears empty

EDIT: It could have been out of disk space. Unable to verify right now, but I will update if reappears.

torronen avatar Aug 19 '25 18:08 torronen

Image

same all break all the time

i looked into this file, such a bad design to put all the context and every single data in a json file ...

why is the config mixed with cache, and prompts and everything else? insane

louis030195 avatar Oct 02 '25 22:10 louis030195

I complete broke mine, and the backup is broken also - I tried removing claude, and the files, but when i reinstall claude the original (standard) file does not come back ?

lulzkiller666 avatar Oct 09 '25 22:10 lulzkiller666

This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.

github-actions[bot] avatar Dec 07 '25 10:12 github-actions[bot]

This isn't stale! Happens for me consistently, too.

brennon avatar Dec 11 '25 23:12 brennon

Additional Evidence: Linux/WSL2 + Infinite Loop Symptom

I can confirm this issue occurs on Linux/WSL2 as well, with an additional symptom not yet documented: lock contention can cause the TUI to enter an infinite event loop.

Environment

  • Platform: Linux (WSL2)
  • OS Version: Linux 6.6.87.2-microsoft-standard-WSL2
  • Claude Code Version: 2.0.72
  • Session ID: 6f257eef-bc05-4588-aeac-7a818b3845d1

Reproduction Scenario

Setup: 4 concurrent Claude Code sessions running in different project directories (legitimate parallel workflow - PDF conversion, issue tracking, etc.)

Trigger Sequence:

  1. One session experienced a connection error: Error streaming, falling back to non-streaming mode: Connection error.
  2. Shortly after, lock acquisition began failing:
    [ERROR] Failed to save config with lock: Error: Lock file is already being held
    
    (This error appeared 5+ times in quick succession)
  3. The TUI then entered a runaway event loop

Runaway Loop Symptoms

The session began cycling through these operations at ~6.6 ops/second (vs normal ~1.5/sec):

  • LSP Diagnostics checking
  • Hook command matching (UserPromptSubmit with query: undefined)
  • FileHistory snapshots for every message
  • Atomic writes to ~/.claude.json

Debug log evidence:

  • Log grew to 6.3 MB / 76,519 lines in just 14 minutes
  • 5,593 FileHistory: Making snapshot entries
  • Constant temp file creation: .claude.json.tmp.<PID>.<timestamp>

Impact

  • TUI completely unresponsive (ESC did not interrupt)
  • Required kill -9 to recover
  • CPU usage spiked to 24%+ on that process
  • Session state lost

Connection to Settings Corruption

I believe this same lock contention mechanism may explain settings file corruption I documented in a separate issue (#14522). During a previous incident with the same multi-session setup, the permissions array in settings.local.json contained fragmented bash commands split across multiple JSON entries:

"Bash(for archive in /home/user/projects/*/Archives/2025-12-0[2-8]_*.md ...)",
"Bash(do if [ -f $archive ])",
"Bash(then echo '=== $(basename $archive) ===' head -50 $archive)",

This looks like a partial JSON write that got interrupted by lock contention, leaving a corrupted permissions array.

Suggested Fix

In addition to the file locking improvements discussed in this thread, the event loop should have:

  1. Lock acquisition timeout - Don't spin forever if lock is held
  2. Backoff strategy - Exponential backoff on lock contention
  3. Loop detection - Detect rapid cycling and break out gracefully
  4. Graceful degradation - Continue with stale state rather than infinite loop

Happy to provide the full debug log (sanitized) if helpful.

pentafive avatar Dec 19 '25 11:12 pentafive