[PR #7039] fix: add cleanup mechanisms to prevent memory leaks #12223

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

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

State: closed
Merged: No


Summary

This PR adds cleanup mechanisms to prevent memory leaks in several areas of the codebase:

  • ACPSessionManager: Add remove(), size(), and clear() methods to properly clean up session state when sessions end
  • Rpc: Add timeout mechanism (30s default) for pending RPC calls to prevent orphaned requests from accumulating
  • FileTime: Add clearSession() to clean up per-session file read timestamps when sessions end
  • MCP OAuth: Add TTL (10 minutes) for pending OAuth transports to clean up abandoned authentication flows

Changes

File Change
src/acp/session.ts Add remove(), size(), clear() methods
src/util/rpc.ts Add timeout, dispose(), pendingCount() methods
src/file/time.ts Add clearSession(), sessionCount() methods
src/mcp/index.ts Add TTL-based cleanup for pendingOAuthTransports
test/acp/session.test.ts Tests for session cleanup
test/util/rpc.test.ts Tests for RPC timeout and cleanup
test/file/time.test.ts Tests for FileTime session cleanup

Testing

  • 12 new tests added covering all cleanup mechanisms
  • All tests pass
  • Typecheck passes
  • Build passes

Related Issues

  • #4315 - Memory stays unbounded: ACP session map + compacted tool outputs
  • #5363 - opencode eating 70gb of memory?

Note

This PR is complementary to #7032 which addresses Bus subscription cleanup. Together they should significantly reduce memory accumulation during long-running sessions.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/7039 **State:** closed **Merged:** No --- ## Summary This PR adds cleanup mechanisms to prevent memory leaks in several areas of the codebase: - **ACPSessionManager**: Add `remove()`, `size()`, and `clear()` methods to properly clean up session state when sessions end - **Rpc**: Add timeout mechanism (30s default) for pending RPC calls to prevent orphaned requests from accumulating - **FileTime**: Add `clearSession()` to clean up per-session file read timestamps when sessions end - **MCP OAuth**: Add TTL (10 minutes) for pending OAuth transports to clean up abandoned authentication flows ## Changes | File | Change | |------|--------| | `src/acp/session.ts` | Add `remove()`, `size()`, `clear()` methods | | `src/util/rpc.ts` | Add timeout, `dispose()`, `pendingCount()` methods | | `src/file/time.ts` | Add `clearSession()`, `sessionCount()` methods | | `src/mcp/index.ts` | Add TTL-based cleanup for `pendingOAuthTransports` | | `test/acp/session.test.ts` | Tests for session cleanup | | `test/util/rpc.test.ts` | Tests for RPC timeout and cleanup | | `test/file/time.test.ts` | Tests for FileTime session cleanup | ## Testing - 12 new tests added covering all cleanup mechanisms - All tests pass - Typecheck passes - Build passes ## Related Issues - #4315 - Memory stays unbounded: ACP session map + compacted tool outputs - #5363 - opencode eating 70gb of memory? ## Note This PR is complementary to #7032 which addresses Bus subscription cleanup. Together they should significantly reduce memory accumulation during long-running sessions.
yindo added the pull-request label 2026-02-16 18:17:09 -05:00
yindo closed this issue 2026-02-16 18:17:09 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12223