Memory leak: Instance disposal doesn't clean up plugins or cached resources #6636

Open
opened 2026-02-16 18:04:49 -05:00 by yindo · 1 comment
Owner

Originally created by @sauerdaniel on GitHub (Jan 17, 2026).

Originally assigned to: @rekram1-node on GitHub.

Problem

When Instance.dispose() is called, several resources are not properly cleaned up:

1. Plugins Not Disposed

Plugins may hold resources (connections, file handles, caches) that need cleanup, but there's no disposal hook in the Hooks interface:

interface Hooks {
  // No dispose() method exists
}

2. Cached Instances Not Disposed

The instance cache uses a standard Map without disposal hooks. When instances are evicted (due to memory pressure or explicit removal), their state is not properly cleaned up.

3. No Bootstrap Cleanup

When the process exits, plugins are not given a chance to clean up their resources.

Code Locations

  • packages/plugin/src/index.ts - Hooks interface
  • packages/opencode/src/plugin/index.ts - Plugin loading
  • packages/opencode/src/project/instance.ts - Instance cache
  • packages/opencode/src/project/bootstrap.ts - Bootstrap process

Impact

  • Plugin resources leak on instance disposal
  • Cache eviction doesn't trigger cleanup
  • Process exit leaves orphaned resources

Relates to #5363
Depends on #9140 (LRU cache utility needed for onEvict callback)

Expected Behavior

  • Plugins should have a dispose() hook
  • Cache eviction should trigger disposal
  • Bootstrap should clean up plugins on exit
Originally created by @sauerdaniel on GitHub (Jan 17, 2026). Originally assigned to: @rekram1-node on GitHub. ## Problem When `Instance.dispose()` is called, several resources are not properly cleaned up: ### 1. Plugins Not Disposed Plugins may hold resources (connections, file handles, caches) that need cleanup, but there's no disposal hook in the Hooks interface: ```typescript interface Hooks { // No dispose() method exists } ``` ### 2. Cached Instances Not Disposed The instance cache uses a standard Map without disposal hooks. When instances are evicted (due to memory pressure or explicit removal), their state is not properly cleaned up. ### 3. No Bootstrap Cleanup When the process exits, plugins are not given a chance to clean up their resources. ### Code Locations - `packages/plugin/src/index.ts` - Hooks interface - `packages/opencode/src/plugin/index.ts` - Plugin loading - `packages/opencode/src/project/instance.ts` - Instance cache - `packages/opencode/src/project/bootstrap.ts` - Bootstrap process ### Impact - Plugin resources leak on instance disposal - Cache eviction doesn't trigger cleanup - Process exit leaves orphaned resources Relates to #5363 Depends on #9140 (LRU cache utility needed for onEvict callback) ## Expected Behavior - Plugins should have a `dispose()` hook - Cache eviction should trigger disposal - Bootstrap should clean up plugins on exit
yindo added the perf label 2026-02-16 18:04:49 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Jan 17, 2026):

This issue might be a duplicate or closely related to other memory leak issues in the repository. Please check:

  • #9140: Unbounded caches cause memory growth over time
  • #9156: Memory leak: Uncleaned timeouts, intervals, and subscriptions
  • #9155: Memory leak: TUI and Slack bot event listeners not cleaned up
  • #9154: Memory leak: ACP session event streams never aborted
  • #9153: Memory leak: MCP OAuth transports not closed on retry or cancellation
  • #9143: LSP client diagnostics and files maps not cleared on shutdown
  • #9151: General memory leak report

These issues appear to be related components of a larger memory management problem (#5363). They may be addressed separately or as part of a broader effort to eliminate memory leaks.

@github-actions[bot] commented on GitHub (Jan 17, 2026): This issue might be a duplicate or closely related to other memory leak issues in the repository. Please check: - #9140: Unbounded caches cause memory growth over time - #9156: Memory leak: Uncleaned timeouts, intervals, and subscriptions - #9155: Memory leak: TUI and Slack bot event listeners not cleaned up - #9154: Memory leak: ACP session event streams never aborted - #9153: Memory leak: MCP OAuth transports not closed on retry or cancellation - #9143: LSP client diagnostics and files maps not cleared on shutdown - #9151: General memory leak report These issues appear to be related components of a larger memory management problem (#5363). They may be addressed separately or as part of a broader effort to eliminate memory leaks.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#6636