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

[BUG] Claude Code Silent Crash Due to Memory Exhaustion from Unbounded Command Output

Open pm0code opened this issue 1 week ago • 1 comments

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

Environment

  • OS: Windows 11 x64
  • Claude Code Version: Latest (as of 2025-12-21)
  • Node.js: Installed via npm
  • Shell: Git Bash (C:\Program Files\Git\bin\bash.exe)
  • Terminal: PowerShell 7.x

Summary

Claude Code process terminates silently (no error message, no graceful shutdown) when executing commands that generate very large output volumes, likely due to memory exhaustion.

Steps to Reproduce

  1. Start Claude Code session in a large project directory
  2. Execute multiple Bash tool calls in parallel that generate large output
  3. Specifically, commands like: Get-ChildItem -Path -Recurse -Filter '*.log' | Where-Object { $_.LastWriteTime -gt (Get-Date).AddHours(-2) } | Sort-Object LastWriteTime -Descending | Select-Object -First 10
  4. When directory contains many files with glob patterns in names, PowerShell generates massive error output

Expected Behavior

  • Claude Code should handle large command outputs gracefully
  • Output should be truncated at a reasonable limit
  • Process should not crash
  • User should receive error message if output limit exceeded

Actual Behavior

  • Process terminates silently with no error message
  • Debug log shows normal operation, then abruptly stops mid-execution
  • No exception logged
  • No graceful shutdown
  • Session state lost

Evidence

Last Debug Log Entries (Anonymized)

2025-12-21T18:36:55.995Z [DEBUG] Session environment not yet supported on Windows 2025-12-21T18:36:56.609Z [DEBUG] Getting matching hook commands for PostToolUse with query: Bash 2025-12-21T18:36:56.609Z [DEBUG] Found 0 hook matchers in settings 2025-12-21T18:36:56.609Z [DEBUG] Matched 0 unique hooks for query "Bash" (0 before deduplication) 2025-12-21T18:36:56.623Z [DEBUG] LSP Diagnostics: getLSPDiagnosticAttachments called 2025-12-21T18:36:56.623Z [DEBUG] LSP Diagnostics: Checking registry - 0 pending 2025-12-21T18:36:56.623Z [DEBUG] Hooks: Found 0 total hooks in registry 2025-12-21T18:36:56.623Z [DEBUG] Hooks: checkForNewResponses returning 0 responses 2025-12-21T18:36:57.040Z [DEBUG] Stream started - received first chunk 2025-12-21T18:36:58.545Z [DEBUG] Stream started - received first chunk 2025-12-21T18:37:01.254Z [DEBUG] executePreToolHooks called for tool: Read 2025-12-21T18:37:01.254Z [DEBUG] executePreToolHooks called for tool: Read 2025-12-21T18:37:01.260Z [DEBUG] Getting matching hook commands for PreToolUse with query: Read 2025-12-21T18:37:01.260Z [DEBUG] Found 0 hook matchers in settings 2025-12-21T18:37:01.260Z [DEBUG] Matched 0 unique hooks for query "Read" (0 before deduplication) 2025-12-21T18:37:01.260Z [DEBUG] Getting matching hook commands for PreToolUse with query: Read 2025-12-21T18:37:01.260Z [DEBUG] Found 0 hook matchers in settings 2025-12-21T18:37:01.260Z [DEBUG] Matched 0 unique hooks for query "Read" (0 before deduplication) [END - No further entries]

Bash Command Output Before Crash

Function results showed massive output truncation: ... [77594 characters truncated] ...

Repeated error output pattern: extglob.LastWriteTime: The term 'extglob.LastWriteTime' is not recognized... [repeated thousands of times] extglob.Name: The term 'extglob.Name' is not recognized... [repeated thousands of times]

Root Cause Analysis

Primary Cause: Memory exhaustion from unbounded command output

  1. PowerShell commands with file globbing on directories with special characters in filenames generated massive error output
  2. Claude Code captured all output into memory buffers
  3. Output exceeded available memory (estimated 77,594+ characters in single command, multiple commands in parallel)
  4. Node.js process hit memory limit
  5. Windows terminated process (or Node.js crashed with unhandled OOM)
  6. No exception handler caught the termination → silent crash

Contributing Factors:

  • No output size limits enforced
  • No memory monitoring/warnings
  • Parallel execution of multiple high-output commands
  • PowerShell error output not suppressed properly

Impact

  • Severity: High
  • User Experience: Critical - complete session loss, no warning, no recovery
  • Data Loss: All unsaved conversation context lost
  • Frequency: Reproducible when executing commands with large output

Suggested Fix

Short-term (Mitigation)

  1. Output size limits: Truncate command output at reasonable threshold (e.g., 50KB per command)
  2. Memory monitoring: Track process memory usage
  3. Graceful degradation: Show warning when output exceeds threshold
  4. Error suppression: Better handling of stderr in Bash tool

Long-term (Prevention)

  1. Streaming output: Don't buffer entire output in memory
  2. Back-pressure handling: Pause command execution if output buffer fills
  3. Process monitoring: Catch OOM before crash, save session state
  4. Output pagination: Automatically paginate large results
  5. Exception handling: Global unhandled exception/rejection handlers to save state before exit

Code Example (Conceptual)

// Add to Bash tool execution const MAX_OUTPUT_SIZE = 50 * 1024; // 50KB let outputBuffer = ''; let truncated = false;

childProcess.stdout.on('data', (chunk) => { if (outputBuffer.length + chunk.length > MAX_OUTPUT_SIZE) { truncated = true; const remaining = MAX_OUTPUT_SIZE - outputBuffer.length; outputBuffer += chunk.slice(0, remaining); outputBuffer += '\n\n[Output truncated - exceeded 50KB limit]'; childProcess.kill(); // Stop command } else { outputBuffer += chunk; } });

// Add global memory monitoring setInterval(() => { const memUsage = process.memoryUsage(); if (memUsage.heapUsed > MEMORY_WARNING_THRESHOLD) { logger.warn('High memory usage detected', memUsage); // Potentially notify user, save session state } }, 5000);

Workaround for Users

  • Avoid commands with unbounded output (use -First, -Head, | head, etc.)
  • Add -ErrorAction SilentlyContinue to PowerShell commands
  • Use 2>/dev/null for bash commands to suppress stderr
  • Limit recursive directory searches to specific file counts

Additional Notes

  • No Windows Event Log entries for the crash (silent termination)
  • Process exit code unknown (not logged)
  • Session was active for ~3 minutes before crash
  • Crash occurred during normal tool execution, not during startup/shutdown

Filed: 2025-12-21 Priority: High Category: Stability / Memory Management Affects: Windows users with large command outputs

What Should Happen?

Expected Behavior

  • Claude Code should handle large command outputs gracefully
  • Output should be truncated at a reasonable limit
  • Process should not crash
  • User should receive error message if output limit exceeded

Error Messages/Logs

please see the body

Steps to Reproduce

  1. Start Claude Code session in a large project directory
  2. Execute multiple Bash tool calls in parallel that generate large output
  3. Specifically, commands like: Get-ChildItem -Path -Recurse -Filter '*.log' | Where-Object { $_.LastWriteTime -gt (Get-Date).AddHours(-2) } | Sort-Object LastWriteTime -Descending | Select-Object -First 10
  4. When directory contains many files with glob patterns in names, PowerShell generates massive error output

Claude Model

Sonnet (default)

Is this a regression?

I don't know

Last Working Version

No response

Claude Code Version

2.0.75

Platform

Anthropic API

Operating System

Windows

Terminal/Shell

PowerShell

Additional Information

No response

pm0code avatar Dec 21 '25 18:12 pm0code