RangeError on Large Output
Summary
Claude Code CLI crashes with RangeError: Invalid string length when examining databases that return large amounts of output data.
Environment
- Claude Code CLI Version: Latest (based on Node.js v20.18.1)
- Platform: Linux 6.14.0-24-generic
- Node.js Version: v20.18.1
Bug Description
When running database examination commands that produce large output (particularly database status checks), Claude Code crashes with a JavaScript string length error instead of gracefully handling the large data.
Error Details
RangeError: Invalid string length
at Socket.<anonymous> (file:///home/sadara/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:780:8787)
at Socket.emit (node:events:530:35)
at Socket.emit (node:domain:489:12)
at addChunk (node:internal/streams/readable:561:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
at Readable.push (node:internal/streams/readable:392:5)
at Pipe.onStreamRead (node:internal/stream_base_commons:191:23)
Steps to Reproduce
- Have a Python script that queries databases and outputs large amounts of data
- Run the script through Claude Code CLI
- When the output exceeds JavaScript's maximum string length (~2^28 characters), the CLI crashes
Expected Behavior
Claude Code should handle large outputs gracefully by either:
- Streaming the output in chunks
- Truncating very large outputs with a warning
- Implementing pagination for large datasets
- Providing a configuration option for maximum output size
Actual Behavior
Claude Code crashes with RangeError: Invalid string length and terminates the session.
Impact
- Severity: High
- Frequency: Reproducible when working with database operations that return large datasets
- Workaround: Manually limit output in user code (not ideal)
Technical Details
The error occurs in the CLI's output handling mechanism when trying to create a string that exceeds JavaScript's maximum string length limit. This appears to be in the socket data handling at line 780 of cli.js.
Suggested Fixes
- Implement streaming output: Instead of buffering all output into a single string, stream it in manageable chunks
- Add output size limits: Implement configurable limits on output size with graceful truncation
- Add pagination: For very large outputs, implement pagination or "show more" functionality
- Error handling: Catch
RangeErrorexceptions and provide a user-friendly message instead of crashing
Files Affected
/home/sadara/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js:780- Output handling/buffering mechanism in Claude Code CLI
Test Case
Create a Python script that outputs a very large string (>500MB) to reproduce the issue:
# This will crash Claude Code CLI
print("x" * (500 * 1024 * 1024)) # 500MB string
Additional Context
This issue was discovered while working with database management scripts that query multiple databases and return comprehensive status information. The crash log was saved to claude-crashlog.txt in the working directory.
Priority
This should be considered a high-priority bug as it affects the reliability of Claude Code when working with data-intensive applications, which is a common use case for development tools.
11 Comments
Hi! How exactly did you pass the long string into Claude Code:
-pmode?Would love to see a screenshot of the crash, including the content right before it crashed.
Since yesterday, I’ve had the same thing happen a few times. I didn’t use a long string in Claude’s code, just a regular prompt.
Prompt example:
I'm also seeing this on
v1.0.65, heres a more complete debug log:I have same issue. In my case Claude Code decided to run a bash command (compiled code it produced) and because of a bug it produced roughly 1543765 lines:
It needs to filter the socket and at least close the socket if it exceeds certain limits.
I've had this happen as well on version 1.0.68 on MacOS with NodeJS v22.18.0.
generate_logs.py.txt. (This is the python script to help with formatting)
Can confirm also seeing this in v1.0.68
Any output generator that generates more than 0x1fffffe8 characters to stdout overflows the string concat buffer capturing stdout around like 823 in cli.js
You can run this generator below . Save it to generate_logs.py, and run this prompt in CC
That should recreate the failure. It'll generate a lot of 'pseudo-log' messages until it overflows the buffer and crashes.
`
#!/usr/bin/env python3
import sys
import random
import string
from datetime import datetime, timedelta
def generate_log_entry(timestamp, message_length=100):
levels = ['INFO', 'WARN', 'ERROR', 'DEBUG']
components = ['auth', 'database', 'api', 'cache', 'queue', 'scheduler', 'worker']
level = random.choice(levels)
component = random.choice(components)
random_message = ''.join(random.choices(string.ascii_letters + string.digits + ' .,-!?', k=message_length))
return f"[{timestamp.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]}] {level:5} {component:10} - {random_message}\n"
def generate_logs(target_size_mb=500):
target_size_bytes = target_size_mb 1024 1024
current_size = 0
base_time = datetime.now() - timedelta(days=1)
entry_count = 0
batch_size = 1000
batch_data = []
while current_size < target_size_bytes:
for _ in range(batch_size):
timestamp = base_time + timedelta(milliseconds=entry_count * 100)
log_entry = generate_log_entry(timestamp)
batch_data.append(log_entry)
current_size += len(log_entry)
entry_count += 1
if current_size >= target_size_bytes:
break
sys.stdout.write(''.join(batch_data))
sys.stdout.flush()
batch_data.clear()
if __name__ == "__main__":
target_size = 500
if len(sys.argv) > 1:
try:
target_size = int(sys.argv[1])
except ValueError:
print(f"Invalid size argument: {sys.argv[1]}. Using default 500MB.", file=sys.stderr)
generate_logs(target_size)
`
Found 3 possible duplicate issues:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
I also encountered the same issue.
Environment
Description
The error occurred during test execution (
flutter test).The CLI crashed with the following error:
Steps to reproduce
claude --continueRangeError: Invalid string lengthAdditional context
It seems the CLI is buffering the entire stdout/stderr as a string, which overflows when the output is too large (millions of lines in my case).
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.
This issue has been automatically closed due to 60 days of inactivity. If you're still experiencing this issue, please open a new issue with updated information.
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.