fix(transcripts): close stream on parse error in readUtterancesFromDir (fixes #98467)#98584
Closed
liuhao1024 wants to merge 1 commit into
Closed
Conversation
Wrap the streaming JSONL loop in try/finally to call lines.close() and stream.destroy() on all exit paths. Previously, if JSON.parse threw inside the for-await loop, the underlying file descriptor remained open because the createReadStream and createInterface references were scoped inside the try block and unreachable from the catch. Fixes openclaw#98467
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What Problem This Solves
TranscriptsStore.readUtterancesFromDircreates acreateReadStreamandreadline.createInterfaceinline inside atryblock for streaming JSONL parsing. IfJSON.parsethrows on a malformed line, the catch block re-throws the error but the stream and readline interface are never cleaned up — the underlying file descriptor remains open until garbage collection.In long-running gateway processes that parse many transcript sessions, leaked file descriptors accumulate and can exhaust the OS fd limit.
Why This Change Was Made
The stream and readline references were created inline inside the
tryblock, making them unreachable fromcatchor any cleanup path. By extracting them to outer-scope variables and adding afinallyblock that callslines.close()andstream.destroy(), the file descriptor is released immediately on all exit paths — normal completion, parse error, or ENOENT.User Impact
Operators running OpenClaw gateways with active transcript sessions will no longer accumulate leaked file descriptors when malformed JSONL lines are encountered. This prevents potential
EMFILEerrors in long-running processes.Evidence
readUtterancesFromDirwhenJSON.parsethrows on malformed JSONLstream.destroyed === true,stream.readable === false, file descriptor released immediatelyReal behavior proof
readUtterancesFromDirwhenJSON.parsethrows on malformed JSONLstream.destroyed === true,stream.readable === false, file descriptor released immediatelyChanges Made
src/transcripts/store.ts: ExtractcreateReadStreamandcreateInterfaceto outer scope variables; addfinallyblock withlines.close()andstream.destroy()to ensure cleanup on all exit pathsAI-assisted (Hermes Agent)