Stack overflow crash from corrupted JSONL session files during concurrent writes

Resolved 💬 3 comments Opened Jan 26, 2026 by kokevidaurre Closed Jan 30, 2026

Description

Claude Code crashes with RangeError: Maximum call stack size exceeded when session JSONL files become corrupted due to concurrent write operations. The crash affects all terminal tabs simultaneously since they share the ~/.claude/projects/ directory.

Error

Exception in PromiseRejectCallback:
file:///.../@anthropic-ai/claude-code/cli.js:386
RangeError: Maximum call stack size exceeded

Root Cause Analysis

Investigated the crash and found multiple corrupted JSONL files in ~/.claude/projects/:

Error parsing line in .../7dd62e7b-....jsonl: SyntaxError: Unexpected token 'i', "ing/eng-cr"... is not valid JSON
Error parsing line in .../ad5c2bf3-....jsonl: SyntaxError: Unexpected token 'p', "pbi/gUEKpQ"... is not valid JSON

Corruption Pattern

Examining the corrupted files revealed out-of-order timestamps proving a race condition:

Line 495: "timestamp":"2026-01-07T15:15:13.853Z"  ← valid JSON
Line 496: "timestamp":"2026-01-07T15:15:13.309Z"  ← CORRUPTED (earlier timestamp, truncated mid-write)
Line 497: "timestamp":"2026-01-07T15:15:31.356Z"  ← valid JSON

Line 496 contains a truncated base64 signature (pbi/gUEKpQ...) from the middle of a thinking block - the write was interrupted and the next write started on a new line.

Why It Happens

  1. Multiple Claude Code processes (main sessions + subagents) append to JSONL files concurrently
  2. Node.js file append operations are not atomic
  3. When writes interleave, one gets truncated mid-stream
  4. On next startup, JSON parser hits invalid data and something causes infinite recursion → stack overflow

Steps to Reproduce

  1. Run multiple Claude Code sessions on the same project directory
  2. Use heavy subagent workloads (Task tool spawning many agents)
  3. Eventually a write collision corrupts a JSONL file
  4. Restart Claude Code → crash

Environment

  • Claude Code version: 2.1.14
  • Node.js: v22.20.0
  • Platform: macOS (Darwin 25.1.0)

Suggested Fixes

  1. Use file locking for JSONL appends (e.g., proper-lockfile or fs-ext)
  2. Graceful error handling - skip corrupted lines instead of stack overflow
  3. Atomic writes - write to temp file, then rename
  4. Per-process session files - avoid shared append patterns

Workaround

Remove corrupted session files:

# Find and remove corrupted JSONL files
find ~/.claude/projects -name "*.jsonl" -type f -exec sh -c '
  head -1 "$1" | jq . >/dev/null 2>&1 || echo "$1"
  tail -1 "$1" | jq . >/dev/null 2>&1 || echo "$1"
' _ {} \; | sort -u | xargs rm -f

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗