[BUG] RangeError: Invalid string length

Status Fixed / completed
Maintainer reply None cached
Activity 9 comments · opened Apr 27, 2025 · closed May 1, 2025

Environment

  • Platform (select one):
  • [x] Anthropic API
  • [ ] AWS Bedrock
  • [ ] Google Vertex AI
  • [ ] Other: <!-- specify -->
  • Claude CLI version: 0.2.86
  • Operating System: macOS Version 15.4.1
  • Terminal: Ghostty 1.1.3

Bug Description

Whilst claude is processing a request, I often get a dozens of:

RangeError: Invalid string length
    at Socket.<anonymous> (file:///opt/homebrew/lib/node_modules/@anthropic-ai/claude-code/cli.js:501:26748)
    at Socket.emit (node:events:507:28)
    at Socket.emit (node:domain:489:12)
    at addChunk (node:internal/streams/readable:559:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
    at Readable.push (node:internal/streams/readable:390:5)
    at Pipe.onStreamRead (node:internal/stream_base_commons:189:23)

Steps to Reproduce

  1. Give claude code something to do
  2. Wait whilst it thinks
  3. sometimes it blows up with the above error

Expected Behavior

No error

Actual Behavior

The error above

Additional Context

It doesn't seem to matter what I ask claude to do.

View original on GitHub ↗

9 Comments

reverie · 1 year ago

@channingwalton If you can, please share the Claude Code log output from before you hit this error.

channingwalton · 1 year ago

@reverie sorry, but where do I find the log?

reverie · 1 year ago

@channingwalton I mean next time it happens. Sounds like it's happening pretty often for you.

channingwalton · 1 year ago

@reverie There have been several updates since I reported this and can no longer reproduce it, so I'll close for now.

Jamminroot · 1 year ago

Still happens on 1.0.6

jaenster · 1 year ago

It also happens to me from time to time,

slhck · 1 year ago

I am also consistently getting this. The underlying reason seems to be a CLI call that results in an infinite loop, i.e. a shell command that never exits.

See also: https://github.com/anthropics/claude-code/issues/962

IvanBohannon · 1 year ago

I can reproduce this too (running WSL) it crashed when it runs one of my unit tests.

file:///home/ivan/.nvm/versions/node/v24.3.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:626
),D.add(A.uuid)}}async getAllTranscripts(){await this.loadAllSessions();let A=[...this.messages.values()],B=new Set(A.map((Q)=>Q.parentUuid));return A.filter((Q)=>!B.has(Q.uuid)).map((Q)=>this.getTranscript(Q)).filter((Q)=>Q.length)}getTranscript(A){let B=[],Q=A;while(Q)B.unshift(Q),Q=Q.parentUuid?this.messages.get(Q.parentUuid):void 0;return B}async getLastLog(A){let{messages:B}=await Qu1(A);if(B.size===0)return null;let D=Array.from(B.values()).sort((Z,F)=>new Date(F.timestamp).getTime()-new Date(Z.timestamp).getTime())[0];if(!D)return null;let I=[],G=D;while(G)I.unshift(G),G=G.parentUuid?B.get(G.parentUuid):void 0;return I}getAllCheckpoints(A){let B=new Map,Q=A[A.length-1]?.sessionId;if(Q){let D=this.checkpoints.get(Q)?.values()||[];for(let I of D)B.set(I.commit,I)}return Array.from(B.values())}loadAllSessions=O0(async()=>{let A=XF1(th1),B=v1();if(this.didLoad)return this;try{B.statSync(A)}catch{return this}let D=B.readdirSync(A).filter((G)=>G.isFile()&&G.name.endsWith(".jsonl")).map((G)=>rs(A,G.name)),I=await Promise.all(D.sort((G,Z)=>{let F=B.statSync(G),Y=B.statSync(Z);return F.mtime.getTime()-Y.mtime.getTime()}).map(async(G)=>{let Z=$K(P2Q(G,".jsonl"));if(!Z)return{sessionId:Z,sessionMessages:new Set};let F=new Map,Y=new Map,W=new Map;try{await B.stat(G);for(let C of await yj1(G))if(C.type==="user"||C.type==="assistant"||C.type==="attachment"||C.type==="system")F.set(C.uuid,C);else if(C.type==="summary"&&C.leafUuid)Y.set(C.leafUuid,C.summary);else if(C.type==="checkpoint")W.set(C.commit,C)}catch{}return{sessionId:Z,sessionMessages:F,summaries:Y,checkpoints:W}}));for(let{sessionId:G,sessionMessages:Z,summaries:F,checkpoints:Y}of I){if(!G)continue;for(let[W,C]of Z.entries())this.messages.set(W,C);for(let[W,C]of F.entries())this.summaries.set(W,C);this.checkpoints.set(G,Array.from(Y.values()))}return this.didLoad=!0,this})}async function VF1(A){let B=FU0(A);return await aR().insertMessageChain(B),B[B.length-1]?.uuid||null}async function QU0(A){await aR().insertMessageChain(FU0(A),!0)}async function Au1(A){await aR().insertCheckpoint(A)}function _2Q(A){let B=A.find((I)=>I.type==="user");if(!B||B.type!=="user")return"No prompt";let Q=B.message?.content,D="";if(typeof Q==="string")D=Q;else if(Array.isArray(Q))D=Q.find((G)=>G.type==="text")?.text||"No prompt";else D="No prompt";if(D=D.replace(/\n/g," ").trim(),D.length>45)D=D.slice(0,45)+"...";return D}function j2Q(A){return A.map((B)=>{let{isSidechain:Q,parentUuid:D,...I}=B;return I})}function DU0(A,B=0,Q,D){let I=A[A.length-1],G=A[0],Z=_2Q(A),F=new Date(G.timestamp),Y=new Date(I.timestamp),W=D?.map((C)=>({commit:C.commit,timestamp:new Date(C.timestamp),label:C.label}));return{date:I.timestamp,messages:j2Q(A),fullPath:"n/a",value:B,created:F,modified:Y,firstPrompt:Z,messageCount:A.length,isSidechain:G.isSidechain,leafUuid:I.uuid,summary:Q,checkpoints:W}}async function IU0(){let A=await aR().getAllTranscripts(),B=aR().summaries;return A.map((Q,D)=>{let I=Q[Q.length-1],G=I?B.get(I.uuid):void 0,Z=I?aR().getAllCheckpoints(Q):void 0;return DU0(Q,D,G,Z)}).sort((Q,D)=>{return D.modified.getTime()-Q.modified.getTime()})}async function GU0(A,B){await aR().appendEntry({type:"summary",summary:B,leafUuid:A})}async function Bu1(A){let B=new Map,Q=new Map,D=new Map;try{let I=await yj1(A);for(let G of I)if(G.type==="user"||G.type==="assistant"||G.type==="attachment"||G.type==="system")B.set(G.uuid,G);else if(G.type==="summary"&&G.leafUuid)Q.set(G.leafUuid,G.summary);else if(G.type==="checkpoint")D.set(G.commit,G)}catch{}return{messages:B,summaries:Q,checkpoints:D}}async function Qu1(A){let B=rs(XF1(dA()),${A}.jsonl);return Bu1(B)}var y2Q=O0(async(A)=>{let{messages:B,checkpoints:Q}=await Qu1(A);return{messageSet:new Set(B.keys()),checkpointSet:new Set(Q.keys())}},(A)=>A);async function ZU0(A){let B=await aR().getLastLog(A);if(B!==null&&B!==void 0){let Q=B[B.length-1],{summaries:D,checkpoints:I}=await Qu1(A),G=Q?D.get(Q.uuid):void 0;return DU0(B,0,G,Array.from(I.values()))}return null}function FU0(A){return A.filter((B)=>{if(B.type==="progress")return!1;if(B.type==="attachment"&&AU0()!=="ant")return!1;return!0})}var KF1=[],x2Q=100;function f2Q(A){return A.toISOString().replace(/[:.]/g,"-")}var Gu1=f2Q(new Date);function v2Q(){return Iu1(Gz.errors(),Gu1+".txt")}var Du1=!1;function h1(A){if(Du1)return;Du1=!0;try{if(process.env.CLAUDE_CODE_USE_BEDROCK||process.env.CLAUDE_CODE_USE_VERTEX||process.env.DISABLE_ERROR_REPORTING||process.env.CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC)return;let B=A.stack||A.message,Q={error:B,timestamp:new Date().toISOString()};if(KF1.length>=x2Q)KF1.shift();KF1.push(Q),b2Q(v2Q(),{error:B})}catch{}finally{Du1=!1}JF1(A)}function YU0(){return[...KF1]}function Zu1(A){if(!v1().existsSync(A))return[];try{return JSON.parse(v1().readFileSync(A,{encoding:"utf8"}))}catch{return[]}}function b2Q(A,B){return}async function Du(){let A=await IU0();return g2Q(A.filter((B)=>B.messages.length)).map((B,Q)=>({...B,value:Q}))}async function WU0(A){return await ZU0(A)}async function CU0(A){return(await Du())[A]||null}function g2Q(A){return A.sort((B,Q)=>{let D=Q.modified.getTime()-B.modified.getTime();if(D!==0)return D;let I=Q.created.getTime()-B.created.getTime();if(I!==0)return I;return B.created.getTime()-Q.created.getTime()})}function UD(A,B){if(J9(XA.red(MCP server "${A}" ${B})),kQ().cleanupPeriodDays===0)return;try{let Q=Gz.mcpLogs(A),D=B instanceof Error?B.stack||B.message:String(B),I=new Date().toISOString(),G=Iu1(Q,Gu1+".txt");if(!v1().existsSync(Q))v1().mkdirSync(Q);if(!v1().existsSync(G))v1().writeFileSync(G,"[]",{encoding:"utf8",flush:!1});let Z={error:D,timestamp:I,sessionId:PB(),cwd:v1().cwd()},F=Zu1(G);F.push(Z),v1().writeFileSync(G,JSON.stringify(F,null,2),{encoding:"utf8",flush:!1})}catch{}}function v2(A,B){iA(MCP server "${A}": ${B});try{let Q=Gz.mcpLogs(A),D=new Date().toISOString(),I=Iu1(Q,Gu1+".txt");if(!v1().existsSync(Q))v1().mkdirSync(Q);if(!v1().existsSync(I))v1().writeFileSync(I,"[]",{encoding:"utf8",flush:!1});let G={debug:B,timestamp:D,sessionId:PB(),cwd:v1().cwd()},Z=Zu1(I);Z.push(G),v1().writeFileSync(I,JSON.stringify(Z,null,2),{encoding:"utf8",flush:!1})}catch{}}import*as Ju1 from"os";import{PassThrough as u2Q}from"stream";function jj(A){if(A<60000){let I=(A/1000).toFixed(1);return${I.endsWith(".0")?I.slice(0,-2):I}s}let B=Math.floor(A/3600000),Q=Math.floor(A%3600000/60000),D=(A%60000/1000).toFixed(1);if(B>0)return${B}h ${Q}m ${D}s;if(Q>0)return${Q}m ${D}s;return${D}s}function AI(A){let B=A>=1000;return new Intl.NumberFormat("en",{notation:"compact",minimumFractionDigits:B?1:0,maximumFractionDigits:1}).format(A).toLowerCase()}function JU0(A,B={}){let{style:Q="narrow",numeric:D="always",now:I=new Date}=B,G=A.getTime()-I.getTime(),Z=Math.trunc(G/1000),F=[{unit:"year",seconds:31536000,shortUnit:"y"},{unit:"month",seconds:2592000,shortUnit:"mo"},{unit:"week",seconds:604800,shortUnit:"w"},{unit:"day",seconds:86400,shortUnit:"d"},{unit:"hour",seconds:3600,shortUnit:"h"},{unit:"minute",seconds:60,shortUnit:"m"},{unit:"second",seconds:1,shortUnit:"s"}];for(let{unit:W,seconds:C,shortUnit:J}of F)if(Math.abs(Z)>=C){let X=Math.trunc(Z/C);if(Q==="narrow"&&C<86400)return Z<0?${Math.abs(X)}${J} ago:in ${X}${J};return new Intl.RelativeTimeFormat("en",{style:"long",numeric:D}).format(X,W)}if(Q==="narrow")return Z<=0?"0s ago":"in 0s";return new Intl.RelativeTimeFormat("en",{style:Q,numeric:D}).format(0,"second")}function EF1(A,B={}){let{now:Q=new Date,...D}=B;if(A>Q)return JU0(A,{...D,now:Q});return JU0(A,{...D,numeric:"always",now:Q})}function Iu(A,B=!1){if(!A)return;let Q=new Date(A*1000),D=new Date;if((Q.getTime()-D.getTime())/3600000>24){let W=Q.toLocaleDateString("en-US",{weekday:"short"}),C=Intl.DateTimeFormat().resolvedOptions().timeZone;return{resetTimeDisplay:"on "+W+(B? (${C}):""),isWeeklyLimit:!0}}let Z=Q.getMinutes(),F=Q.toLocaleTimeString("en-US",{hour:"numeric",minute:Z===0?void 0:"2-digit",hour12:!0}),Y=Intl.DateTimeFormat().resolvedOptions().timeZone;return{resetTimeDisplay:"at "+F.replace(/ ([AP]M)/i,(W,C)=>C.toLowerCase())+(B? (${Y}):""),isWeeklyLimit:!1}}var qU0=Z1(zU0(),1),UU0=137,wU0=143;function NU0(A){let B=null,Q="";A.on("data",(I)=>{if(B)B.write(I);else Q+=I});let D=()=>Q;return{get:D,asStream(){return B=new u2Q({highWaterMark:10485760}),B.write(D()),Q="",B}}}function $U0(A,B,Q){let D="running",I,G=NU0(A.stdout),Z=NU0(A.stderr),F=(J)=>{if(D="killed",A.pid)qU0.default(A.pid,"SIGKILL")},Y=null,W,C=new Promise((J)=>{let X=()=>F();W=()=>{if(Y)clearTimeout(Y),Y=null;B.removeEventListener("abort",X)},B.addEventListener("abort",X,{once:!0}),new Promise((V)=>{let K=F;F=(E)=>{K(),V(E||UU0)},Y=setTimeout(()=>{F(wU0)},Q),A.on("close",(E,w)=>{V(E!==null&&E!==void 0?E:w==="SIGTERM"?144:1)}),A.on("error",()=>V(1))}).then((V)=>{if(W(),D==="running")D="completed";let K={code:V,stdout:G.get(),stderr:Z.get(),interrupted:V===UU0,backgroundTaskId:I};if(V===wU0)K.stderr=[Command timed out after ${jj(Q)},K.stderr].filter(Boolean).join(" ");J(K)})});return{background:(J)=>{if(D==="running")return I=J,D="backgrounded",W(),{stdoutStream:G.asStream(),stderrStream:Z.asStream()};else return null},kill:()=>F(),result:C}}function LU0(A){return{background:()=>null,kill:()=>{},result:Promise.resolve({code:145,stdout:"",stderr:"Command aborted before execution",interrupted:!0,backgroundTaskId:A})}}var HF1=Z1(wl(),1);import{constants as m2Q}from"fs";import*as MU0 from"os";import*as RU0 from"path";class OU0{profilePath;defaultProfile=(version 1)

RangeError: Invalid string length
at Socket.<anonymous> (file:///home/ivan/.nvm/versions/node/v24.3.0/lib/node_modules/@anthropic-ai/claude-code/cli.js:626:8210)
at Socket.emit (node:events:507:28)
at Socket.emit (node:domain:489:12)
at addChunk (node:internal/streams/readable:559:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
at Readable.push (node:internal/streams/readable:390:5)
at Pipe.onStreamRead (node:internal/stream_base_commons:189:23)

github-actions[bot] · 1 year ago

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.