Skip to content

Conversation

@ztripez
Copy link

@ztripez ztripez commented Jan 24, 2026

Fixes #5363

What does this PR do?

Fixes memory leaks in session handling that cause unbounded memory growth during long conversations.

Key fixes:

  1. Provider caches - Replace unbounded Maps with LRU eviction (100/50 entry limits) to prevent SDK object accumulation
  2. Prompt loop clone - Remove unnecessary deep clone of entire message history on every turn, use selective shallow copying instead
  3. Session cleanup - Add cleanup for FileTime and ACP session state when sessions end
  4. Bus subscriptions - Convert permanent subscriptions to use Instance.state() disposal pattern to prevent listener leaks
  5. Compaction output deletion - Actually delete tool output text from storage instead of just marking it compacted (critical fix - output was staying in JSON files)
  6. Pending request timeouts - Add 5-minute timeout to permission/question requests to prevent hanging promises in abandoned sessions
  7. Processor map cleanup - Clear toolcalls map when handling aborted tools to prevent accumulation
  8. String concatenation - Use array accumulation + join instead of += for streaming text/reasoning/bash output to avoid creating intermediate string objects

How did you verify your code works?

  • Passes existing typecheck
  • Changes follow existing patterns in codebase (Instance.state disposal, LRU pattern already used elsewhere)
  • Most critical fix (5 compaction) is straightforward - replaces output string with placeholder before saving instead of after
  • Timeout pattern (6) matches existing timeout implementations in other tools
  • String accumulation (8) is standard optimization, uses join() only when needed for updates

Testing recommendations:

  • Long sessions with many tool calls to verify compaction reduces storage size
  • Monitor memory usage over time with session lifecycle
  • Verify abandoned sessions clean up pending requests after 5 minutes

- add LRU eviction to provider caches
- remove deep clone in prompt loop
- add session cleanup for FileTime and ACP
- fix bus subscriptions to use Instance.state disposal
- delete tool output on compaction instead of just marking
- add timeout to pending permission/question requests
- clear processor maps on error
- use array accumulation instead of string concat for streaming
@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

Potential Related PRs Found

Here are PRs that may be related to or duplicate aspects of PR #10392:

  1. feat(util): Add bounded LRU cache utility #9141 - feat(util): Add bounded LRU cache utility

  2. fix(memory): Fix timeout, interval, and subscription cleanup #9148 - fix(memory): Fix timeout, interval, and subscription cleanup

  3. fix(disposal): Complete Instance disposal chain for plugins and caches #9149 - fix(disposal): Complete Instance disposal chain for plugins and caches

    • Related to the Instance.state() disposal pattern mentioned throughout this PR
  4. fix: clean up pending permissions when session is deleted #7154 - fix: clean up pending permissions when session is deleted

  5. fix(core): add dispose functions to prevent subscription memory leaks #7032 - fix(core): add dispose functions to prevent subscription memory leaks

    • Related to subscription leak prevention and disposal pattern
  6. fix(memory): Fix event listener leaks in TUI and Slack bot #9147 - fix(memory): Fix event listener leaks in TUI and Slack bot

These PRs may contain overlapping solutions or address the same underlying issues. You may want to review them to ensure no duplicate effort or check if this PR should incorporate/reference those fixes.

@Thejuampi
Copy link

tested this but no luck for me , that is after a few hours on a java project with jdtls LSP
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opencode eating 70gb of memory?

2 participants