Skip to content

fix(memory): add TTL, LRU eviction, and ring buffers to prevent memory leaks#12259

Open
Stranmor wants to merge 1 commit intoanomalyco:devfrom
Stranmor:memory-fixes
Open

fix(memory): add TTL, LRU eviction, and ring buffers to prevent memory leaks#12259
Stranmor wants to merge 1 commit intoanomalyco:devfrom
Stranmor:memory-fixes

Conversation

@Stranmor
Copy link

@Stranmor Stranmor commented Feb 5, 2026

Summary

Addresses memory leak issues reported in #10913, #9140, #5363.

This PR adds cleanup mechanisms to prevent unbounded memory growth in long-running sessions:

Changes

Component Fix Details
ACPSessionManager TTL + LRU 1h TTL, 50 session limit, 5min cleanup interval
Instance cache TTL + LRU 30min idle TTL, 10 instance limit, auto-dispose
LSP clients Max limit 20 client limit, oldest-first eviction
PTY buffers RingBuffer O(1) append instead of string concatenation

Technical Details

ACPSessionManager (acp/session.ts)

  • Added SessionEntry wrapper with lastAccess timestamp
  • Periodic cleanup every 5 minutes removes sessions idle >1 hour
  • Hard limit of 50 sessions with LRU eviction

Instance cache (project/instance.ts)

  • Added CacheEntry wrapper with lastAccess tracking
  • evictIdleInstances() called on new instance creation
  • Properly disposes State when evicting idle instances

LSP clients (lsp/index.ts)

  • Added MAX_LSP_CLIENTS = 20 limit
  • Evicts oldest client when limit reached
  • Graceful shutdown of evicted clients

PTY buffers (pty/index.ts)

  • New RingBuffer class with chunk-based storage
  • Avoids repeated large string allocations
  • O(1) append, automatic trimming to limit

Testing

  • TypeScript compiles without errors in changed files
  • Manual testing with long-running sessions
  • Memory profiling before/after

Related Issues

…y leaks

- ACPSessionManager: add 1h TTL + 50 session limit with periodic cleanup
- Instance cache: add 30min idle TTL + 10 instance LRU limit
- LSP clients: add 20 client limit with oldest-first eviction
- PTY buffers: replace string concatenation with RingBuffer class

Addresses issues anomalyco#10913, anomalyco#9140, anomalyco#5363
@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2026

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

Based on my search results, I found 3 potentially related PRs that address similar memory leak issues:

  1. PR fix(memory): resolve 6 memory leak issues in long-running sessions #12053 - "fix(memory): resolve 6 memory leak issues in long-running sessions"

    • Directly addresses the same category of problems (memory leaks in long-running sessions)
  2. PR fix(opencode): resolve multiple memory leaks in long-running sessions #10914 - "fix(opencode): resolve multiple memory leaks in long-running sessions"

    • Also targets memory leaks in long-running sessions with similar scope
  3. PR feat(util): Add bounded LRU cache utility #9141 - "feat(util): Add bounded LRU cache utility"

These PRs likely overlap with the memory leak fixes in PR #12259. You should review PR #12053 in particular, as it appears to address the same core issue of resolving memory leaks in long-running sessions.

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.

fix: multiple memory leaks in long-running sessions

1 participant