[PR #10392] fix(opencode): memory leaks in session handling #13431

Open
opened 2026-02-16 18:18:17 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/10392

State: open
Merged: No


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
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/10392 **State:** open **Merged:** No --- 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
yindo added the pull-request label 2026-02-16 18:18:17 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13431