Memory leak: LSP client diagnostics and files maps not cleared on shutdown
Problem
When an LSP client shuts down, the diagnostics Map and files object are not cleared in the shutdown() method of LSPClient. This can cause:
- Memory retention - Diagnostics data for closed files remains in memory
- Stale data - If the same LSP server is reconnected, old diagnostics may persist
- Memory growth - Over multiple LSP reconnections, maps accumulate entries
Code Location
packages/opencode/src/lsp/client.ts - The shutdown() method closes the connection but doesn't clear internal state.
Proposed Solution
Add cleanup calls in the shutdown() method:
diagnostics.clear()
for (const path in files) delete files[path]
Impact
- Minimal change (2 lines)
- Ensures clean state on LSP shutdown
- Prevents memory growth from accumulated diagnostics
Relates to #5363
This issue might be a duplicate of or closely related to existing memory management issues. Please check:
- #9140: Unbounded caches cause memory growth over time
- #5363: opencode eating 70gb of memory?
- #7261: v1.1.6 still has memory bloat - heap not released + MCP orphan processes
- #8947: [FEATURE]: TUI whole and LSP memory limits as part of configuration
These issues all address memory retention and cleanup problems in OpenCode, including LSP-related memory leaks. Your proposed fix for clearing diagnostics and files maps in LSPClient.shutdown() is directly addressing part of the root cause identified in #9140 regarding unbounded caches.
Feel free to ignore if your specific case differs from these.