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

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

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

State: open
Merged: No


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

  • Fixes #10913 (memory leaks in long-running sessions)
  • Addresses #9140 (unbounded caches)
  • Addresses #5363 (high memory usage)
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/12259 **State:** open **Merged:** No --- ## 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 - [x] TypeScript compiles without errors in changed files - [ ] Manual testing with long-running sessions - [ ] Memory profiling before/after ## Related Issues - Fixes #10913 (memory leaks in long-running sessions) - Addresses #9140 (unbounded caches) - Addresses #5363 (high memory usage)
yindo added the pull-request label 2026-02-16 18:18:57 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14138