[PR #6408] fix: address memory leaks and cross-process race conditions #11886

Closed
opened 2026-02-16 18:16:49 -05:00 by yindo · 0 comments
Owner

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

State: closed
Merged: No


Summary

This PR fixes several critical issues that could cause 70GB+ memory usage and data corruption when running multiple opencode instances.

Memory Leaks Fixed

Component Issue Fix
State.recordsByKey Keys never deleted on dispose, only entries cleared Added recordsByKey.delete(key) in dispose()
MessageV2.filterCompacted Loads all messages without limit Added MAX_MESSAGES_IN_MEMORY (500) hard limit
SessionProcessor Tool outputs stored without size limit Added truncateOutput() to cap at 1MB
LSPClient.diagnostics Accumulates diagnostics for all files forever Added LRU eviction (max 1000 files)
ACPSessionManager Sessions never cleaned up Added LRU eviction (max 100 sessions) with remove/clear methods
Pty.buffer Grows unbounded when no subscribers Added MAX_BUFFER_SIZE (1MB) cap

Cross-Process Race Conditions Fixed

Issue Fix
In-memory Lock only works within single process Added FileLock for cross-process coordination
Multiple instances competing for same files Storage now uses both in-memory Lock AND FileLock

FileLock Implementation

  • Uses .lock files with PID, timestamp, hostname
  • Stale lock detection (30s threshold + process alive check)
  • Automatic cleanup of stale locks
  • Graceful fallback if lock acquisition times out

Files Changed

  • packages/opencode/src/util/file-lock.ts (new) - Cross-process file locking
  • packages/opencode/src/storage/storage.ts - Added FileLock to all operations
  • packages/opencode/src/project/state.ts - Fixed recordsByKey leak
  • packages/opencode/src/session/message-v2.ts - Added message limit
  • packages/opencode/src/session/processor.ts - Added tool output truncation
  • packages/opencode/src/lsp/client.ts - Added LRU diagnostics cache
  • packages/opencode/src/acp/session.ts - Added session cleanup
  • packages/opencode/src/pty/index.ts - Added buffer size limit

Testing

  • Tested with multiple concurrent opencode instances
  • Verified file locking prevents data corruption
  • Memory usage stays bounded in long sessions
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/6408 **State:** closed **Merged:** No --- ## Summary This PR fixes several critical issues that could cause **70GB+ memory usage** and **data corruption** when running multiple opencode instances. ## Memory Leaks Fixed | Component | Issue | Fix | |-----------|-------|-----| | `State.recordsByKey` | Keys never deleted on dispose, only entries cleared | Added `recordsByKey.delete(key)` in dispose() | | `MessageV2.filterCompacted` | Loads all messages without limit | Added MAX_MESSAGES_IN_MEMORY (500) hard limit | | `SessionProcessor` | Tool outputs stored without size limit | Added truncateOutput() to cap at 1MB | | `LSPClient.diagnostics` | Accumulates diagnostics for all files forever | Added LRU eviction (max 1000 files) | | `ACPSessionManager` | Sessions never cleaned up | Added LRU eviction (max 100 sessions) with remove/clear methods | | `Pty.buffer` | Grows unbounded when no subscribers | Added MAX_BUFFER_SIZE (1MB) cap | ## Cross-Process Race Conditions Fixed | Issue | Fix | |-------|-----| | In-memory `Lock` only works within single process | Added `FileLock` for cross-process coordination | | Multiple instances competing for same files | Storage now uses both in-memory Lock AND FileLock | ### FileLock Implementation - Uses `.lock` files with PID, timestamp, hostname - Stale lock detection (30s threshold + process alive check) - Automatic cleanup of stale locks - Graceful fallback if lock acquisition times out ## Files Changed - `packages/opencode/src/util/file-lock.ts` (new) - Cross-process file locking - `packages/opencode/src/storage/storage.ts` - Added FileLock to all operations - `packages/opencode/src/project/state.ts` - Fixed recordsByKey leak - `packages/opencode/src/session/message-v2.ts` - Added message limit - `packages/opencode/src/session/processor.ts` - Added tool output truncation - `packages/opencode/src/lsp/client.ts` - Added LRU diagnostics cache - `packages/opencode/src/acp/session.ts` - Added session cleanup - `packages/opencode/src/pty/index.ts` - Added buffer size limit ## Testing - Tested with multiple concurrent opencode instances - Verified file locking prevents data corruption - Memory usage stays bounded in long sessions
yindo added the pull-request label 2026-02-16 18:16:49 -05:00
yindo closed this issue 2026-02-16 18:16:49 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#11886